Skip to content

Commit becf5d4

Browse files
docs(AnalyticalTable): add recipe on how to show less columns of smaller screens (#574)
Closes #562
1 parent f3fec41 commit becf5d4

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

packages/main/src/components/AnalyticalTable/AnalyticalTable.mdx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,37 @@ React.useEffect(() => {
7777

7878
For more details on this behavior you can double check the [react-table docs](https://github.com/tannerlinsley/react-table/blob/master/docs/faq.md#how-do-i-stop-my-table-state-from-automatically-resetting-when-my-data-changes).
7979

80-
<Stories />
80+
### How can I show a subset of all columns on mobile devices?
81+
82+
83+
In case you want to use the `AnalyticalTable` on mobile devices as well, it might be helpful to reduce the amount of columns
84+
for the sake of better readability. You can achieve this by using the `useViewportRange` hook in combination with a local React state:
85+
86+
```jsx
87+
import React, { useState, useEffect } from 'react';
88+
import { AnalyticalTable } from '@ui5/webcomponents-react/lib/AnalyticalTable';
89+
import { useViewportRange } from '@ui5/webcomponents-react-base/lib/hooks';
8190

91+
const columns = [{}, {}, {}, {}, ...]; /* your full set of columns*/
92+
const data = []; /* your data array */
93+
94+
export const ResponsiveTable = () => {
95+
const [responsiveColumns, setResponsiveColumns] = useState(columns);
96+
const currentRange = useViewportRange('StdExt');
97+
useEffect(() => {
98+
if (currentRange === 'Phone') {
99+
setResponsiveColumns(columns.slice(0, 2));
100+
} else if (currentRange === 'Tablet') {
101+
setResponsiveColumns(columns.slice(0, 3));
102+
} else {
103+
setResponsiveColumns(columns);
104+
}
105+
}, [currentRange]);
106+
return <AnalyticalTable columns={responsiveColumns} data={data} title="ResponsiveTable" />;
107+
};
108+
```
109+
110+
With the help of that effect, the table will now show either 2 columns on a mobile phone, 3 columns on a tablet device and all columns on Desktop devices.
111+
This even works if you resize the browser window!
112+
113+
<Stories />

0 commit comments

Comments
 (0)