Refresh data without losing current grouping?
Hi. I have data that updates every second and I'm trying to show it in a table that has grouping enabled, however every time I update the data the grouping is lost. I know you can provide a promise to the data prop but this method would cause me to need to re-implement a lot of functionality within material-table (which page I'm on, what the search is etc), which really isn't an option.
Any advice on this would be appreciated.
I know this is super old, but couldn't this be handled with proper state management? If you hooked into onColumnDragged... is what I was going to suggest, but when I tested that out just now, it was never called when I dragged a column header to the grouping box. It only fired when changing the column order, which is really too bad. Otherwise you might have been able to preserve the grouping in your own local state, or even longer with session or local storage.
What we really need is onChangeGroupOrder and/or onDragEnd added to the propTypes exports:
https://github.com/mbrn/material-table/blob/ea14e98067d9cd6a93d1bdb6b45bc2531b30dd21/src/prop-types.js#L9
Then I think we could do what you're describing. Even if you never reply, I hope this helps somebody out there in TV Land and maybe brings some attention. MT is such a great product, it just needs a bit of wind in its development sails.
@Vindil would you mind providing a demo of your issue? I was not able to reproduce it..
I'm not sure how you're implementing this, but you can check out a live demo here
Drag any header to group, then let it run.. Data is added every 1500ms.
import React, { useState, useEffect } from "react";
import { render } from "react-dom";
import MaterialTable from "material-table";
import tableIcons from "./TableIcons.js";
const rando = max => Math.floor(Math.random() * max);
const words = ["Paper", "Rock", "Scissors"];
const newRandomRow = () => ({
id: rando(300),
name: words[rando(words.length)],
})
const columns = [
{ title: "Id", field: "id" },
{ title: "Name", field: "name" }
];
const App = () => {
const [data, setData] = useState([{ ...newRandomRow() }]);
const addRow = () => {
const dataCopy = [...data, newRandomRow()];
setData(dataCopy);
};
useEffect(() => {
const ivl = setInterval(() => {
addRow();
}, 1500);
return () => clearInterval(ivl);
});
return (
<MaterialTable
data={data}
icons={tableIcons}
columns={columns}
options={{
grouping: true
}}
/>
);
};
render(<App />, document.querySelector("#root"));
@oze4 I was able to duplicate @Vindil's problem. The only difference I can see if that I have an async function in useEffect that is initializing the data and then being called within setInterval. Oh and I'm also using a shared context, not a local state.
I decided to simplify my code a bit and ditch the context, favoring the easier to maintain local state, and the issue persisted. I tried moving the async portion out of the equation by moving the async/await call into an IIFE in the global scope that set the result to a variable there and then used a non async approach in the useEffect that simply polled that higher level variable the IIFE was dumping its async results to.
It still reset the grouping when the state changed. The only other thing I can think of is that I have MT in its own component that's a child of where the fetch logic is, so the property of that component is changing as well as what's assigned to data={}.
So, I moved the hook doing the data fetch down into the child, but got the same result: the grouping resets. Though, search does not. 🤷♂️
@jroyleaz would you happen to have a demo/snippet of the code you used to reproduce?
@oze4 Sure, it's a new project though, so mind the cruft. 😄
Also, useInterval is a custom hook that cleanly implements setInterval, based on Dan Abramov's article here. Oh, and the refresh bug also clears any selected rows.
export default function Tabs() {
const [tabs, setTabs] = useState([]);
const removeTab = async (tabId) => browser.tabs.remove(tabId);
// Initial call
useEffect(() => {
async function onLoad() {
setTabs(await getTabs());
}
onLoad();
}, []);
// Poller to keep data minty fresh
useInterval(() => {
async function refresh() {
setTabs(await getTabs());
}
refresh();
}, 2000);
return (
<MaterialTable
title="Some Title"
data={tabs}
editable={{
isDeletable: (rowData) => rowData.url !== 'chrome://newtab',
onRowDelete: (oldData) =>
new Promise((resolve, reject) => {
setTimeout(() => {
removeTab(oldData.id);
resolve();
}, 250);
}),
}}
columns={[
{
width: 100,
title: 'Window',
field: 'windowId',
type: 'string',
sorting: true,
},
{
width: 150,
title: 'Domain',
field: 'domain',
type: 'string',
sorting: true,
},
{
title: 'Title',
field: 'title',
type: 'string',
grouping: false,
sorting: true,
cellStyle: {
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
maxWidth: '100%',
},
},
]}
onRowClick={(event, rowData) =>
switchToTab(rowData.id, rowData.windowId)
}
options={{
selection: true,
tableLayout: 'fixed',
pageSize: getIdealRowCount(),
grouping: true,
}}
localization={{
body: {
editRow: {
deleteText: 'Close this tab?',
},
},
}}
/>
);
}
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You can reopen it if it required.