Selection and Remote Data Pagination on Material Table React

瘦欲@ 提交于 2020-01-25 03:52:20

问题


I need help im using material-table on react for my data table. I want to use the selection and pagination at the same time, but the problem is if i select a certain row and change the page and return to previous page. it doesn't select that row. here is a sample snippet i created. Is it possible to override the selection props?

const Table = () => {
const [selectedRows, setSelectedRows] = useState([]);

function handleSelectChange(rows) {
setSelectedRows(rows)
}
return (
<MaterialTable
      title="Remote Data Preview"
      columns={[
        {
          title: 'Avatar',
          field: 'avatar',
          render: rowData => (
            <img
              style={{ height: 36, borderRadius: '50%' }}
              src={rowData.avatar}
            />
          ),
        },
        { title: 'Id', field: 'id' },
        { title: 'First Name', field: 'first_name' },
        { title: 'Last Name', field: 'last_name' },
      ]}
      options={{
        selection:true,
      }}
      onSelectionChange={(rows) => handleSelectChange(rows)}
      data={query =>
        new Promise((resolve, reject) => {
          let url = 'https://reqres.in/api/users?'
          url += 'per_page=' + query.pageSize
          url += '&page=' + (query.page + 1)
          fetch(url)
            .then(response => response.json())
            .then(result => {
              resolve({
                data: result.data,
                page: result.page - 1,
                totalCount: result.total,
              })
            })
        })
      }
    />
)
}

i already created an issue will put this a reference. https://github.com/mbrn/material-table/issues/1189


回答1:


Since you already know the selected rows, you just have to modify your fetched data like this:

data: result.data.map(row => selectedRows.find(selected => selected.id === row.id) ? { ...row, tableData: { checked: true } } : row)

This will add the tableData.checked key to the fetched data and it will display it as selected.

Here is a codesandbox.




回答2:


This solution only works if you do not select another row on a new page as the new rows from onSelectionChange will overwrite the existing selected rows from the previous pages. To be able to select rows across pages I changed the handleSelectionChange function to first check if the current selectedRows are included in the data displayed and for those that are not I spread that into the setState call.

function handleSelectionChange(rows) {
    const displayedIds = data.results.map(result => result.id)
    const selectedRowsNotDisplayed = selectedRows.filter(selectedRow => {
        return !displayedIds.includes(selectedRow.id)
    })

    setSelectedRows([...selectedRowsNotDisplayed, ...rows])
}


来源:https://stackoverflow.com/questions/58389384/selection-and-remote-data-pagination-on-material-table-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!