0

My use case is that when a user filters the table data using search, I'd like to be able to use an external widget to perform actions on each row of that data as it is shown in the table.

Right now I dump all my data into cols={MyData} and sort through data[index] but ideally I'd like to be perform operations with something like currentlyDisplayedTableData[index].

There doesn't seem to be a documented way of doing this so I have no attempt to show, I'm just wondering if someone may have encountered this problem and could show me the light.

re: https://github.com/mbrn/material-table/issues/1124

imjared
  • 19,492
  • 4
  • 49
  • 72

2 Answers2

1

Just thought I should share another tip, if you just want to intercept/intervene and operate on the currently displayed data before render you can override the component for the table body as Tyler showed in the "issue" link.

But instead of adding a render method, like Tyler did, you can just intercept the props on it's "way down" like this and inject it in the next component (Body, Row, etc.

Note; look for EditRow and other components in https://material-table.com/#/docs/features/component-overriding


    <MaterialTable
    //... 

     /**
                 * be aware when making changes on data that there is a tableData object attached
                 * rowData: {
                 *  name: 'some name',
                 *  tableData : {id: 3}
                 * }
                 */
                components={{
                    Body: (props) => {
                        //intervene before rendering table                     
                        console.log("tampering with some table data ", props);
                        console.log(" -- table data looks like this ", props.renderData);
                        // do stuff.. 
                        const myRenderData = props.renderData;
                        return (
                            <>
                                <MTableBody {...props} renderData={myRenderData} />
                                {/* to show that you will make impact */}
                                {/* <MTableBody {...props} renderData={[]} /> */}
                            </>
                        )
                    },
                    Row: (props) => {
                        //intervene before rendering row                     
                        console.log("tampering with some row data ", props);
                        console.log(" -- row data looks like this ", props.data);
                        console.log(" -- row table data looks like this ", props.data.tableData);
                        // do stuff.. 
                        const myRenderData = props.data;
                        return (
                            <>
                                <MTableBodyRow {...props} data={myRenderData} />
                            </>
                        )
                    }
                }}

JimiSweden
  • 744
  • 10
  • 13
0

@imjared

I found this thread, via the issue, today and have now worked on and tested two working solutions for how to get hold on the filtered data. Maybe thisos what you want, or at least can hint you where to go, so I thought I should share it =)

Option 1 - listen for changes in MaterialTable.state.data with reference. (useRef, and UseEffect) Option 2 - built in MaterialTable.onSearchChange combined with reference to MaterialTable.state.data note, I have included 2 flavors of option 2.

Thanks @tylercaceres for the example you provided, it didn't fit for me but gave me a hint on how to do it.

Code is found here: MaterialTableGettingHoldOfRenderData.js material-table example getting filtered data, the tables current view data, including 2 options and some other examples of actions/buttons, how to use SvgIcon from Material-UI

JimiSweden
  • 744
  • 10
  • 13