• Home
  • Material ui datagrid how to tell which rows user has selected – JavaScript

Material ui datagrid how to tell which rows user has selected – JavaScript

To get the rows that the user has selected in a Material-UI DataGrid, you can use the getSelectedRows method provided by the DataGrid. This method returns an array of the indices of the selected rows.

Here is an example of how you can use the getSelectedRows method in a React component:

import { DataGrid } from '@material-ui/data-grid';

function MyComponent() {
const [selectedRows, setSelectedRows] = useState([]);

const handleRowSelectionChange = (event) => {
setSelectedRows(event.getSelectedRows());
}

return (
<DataGrid
rows={data}
onRowSelectionChange={handleRowSelectionChange}
/>
);
}

In this example, the handleRowSelectionChange function is called every time the user selects or deselects a row in the DataGrid. The event argument passed to the function contains the DataGrid component, and the getSelectedRows method can be used to get an array of the indices of the selected rows. The selected row indices are then stored in the selectedRows state variable using the setSelectedRows hook.