material-table icon indicating copy to clipboard operation
material-table copied to clipboard

Update data based on state of outer component

Open perfectstrong opened this issue 7 years ago • 1 comments

Describe the bug I have two tables on page, the second will show all related informations of the selected item from the first. I decided to pass the selected item via this.state.selectedPatient of outer component. However, clicking on a row of the first table will trigger an error:

TypeError: data.map is not a function                                 data-manager.js:265

To Reproduce

// The first table
<MaterialTable
    title="The first table"
    columns={someColumns}
    data={[someDataFromElsewhere]}
    onRowClick={(event, rowData) => this.setState({selectedPatient: rowData})}
/>

// The second table
<MaterialTable
    title="The second table"
    columns={columnsOfClass_Cycle}
    data={
        this.state.selectedPatient.hasOwnProperty("id") ?
            () =>
                new Promise((resolve, reject) =>
                        this.cyclesController // A relay to retrieve data from REST server
                            .searchByPatientId(this.state.selectedPatient.id)
                            .then((result: {
                                    cycles: Cycle[],
                                    page: {
                                        size: number,
                                        totalElements: number,
                                        totalPages: number,
                                        number: number,
                                    }
                                }) => resolve({
                                    data: result.cycles,
                                    page: result.page.number,
                                    totalCount: result.page.totalElements,
                                }),
                                reject
                            )
                        .catch(console.error))
            // No selected patient
            : []
    }
/>

Expected behavior The second table should show all related informations of the selected item from the first.

Screenshots image

image

Desktop

  • Material-table: 1.36.6
  • OS: Windows 10
  • Browser Firefox
  • Version 67.0.3

perfectstrong avatar Jun 19 '19 06:06 perfectstrong

In javascript does the method .map() it's only for arrays, [].

someDataFromElsewhere must be a array in this case.

If it is already are a array just do it: data: {someDataFromElsewhere} If it is a object try: data: {[ ...someDataFromElsewhere ]}

AugustoCalaca avatar Jun 20 '19 14:06 AugustoCalaca