gregnb / mui-datatables

Datatables for React using Material-UI
MIT License
2.71k stars 934 forks source link

Error: number or rows selected on custom single selected row #402

Open 0xj0hnny opened 5 years ago

0xj0hnny commented 5 years ago

Hi,

First of all, great job on implemented this library. Since the table does not support single row select. I have to implemented some custom code to enforce the single select for the table. Below are the code that I implemented.

customToolbarSelect: (selectedRows) => {
            const newSelectedRow = selectedRows.data.length > 1 ? selectedRows.data[1] : selectedRows.data[0];
            const newSelectedIndex = parseInt(newSelectedRow.index);
            const temp = {
              data: newSelectedRow,
              lookup: {}
            };
            temp.lookup[`${newSelectedIndex}`] = true;
            if (selectedRows.data.length > 1) {
              selectedRows.data = [];
              selectedRows.lookup = temp.lookup;
              selectedRows.data.push(temp.data);
            }
          }
},
  ....

Expected Behavior

I expected that the number or `rows selected` on Toolbar would be either 0, or 1. 

Current Behavior

If I selected one row and then select another, the number of rows selected is 2, instead of 1. And unless I deselect the selected row to clear out the number. The number of rows selected stays at 2.

Steps to Reproduce (for bugs)

table

Your Environment

Tech Version
Material-UI 3.5.1
MUI-datatables 2.0.0-beta-41
React 16.6.0
browser Chrome
gregnb commented 5 years ago

If you want to implement this in the library I would accept a PR for the following:

In the component's options, one should be able to set

Name Type Default Description
selectableRows string 'multiple' Configure row selection. Options: 'single', 'multiple' or 'false'

When "single" is selected:

When "multiple" is selected:

When "false" is selected:

viniciusalvess commented 5 years ago

Just a fix on @johnnyliang213 's code. But that was a nice enhancement suggestion, thank you.

const newSelectedRow = selectedRows.data[selectedRows.data.length -1]; // selectedRows.data.length > 1 ? selectedRows.data[1] : selectedRows.data[0];
const newSelectedIndex = parseInt(newSelectedRow.index);
const temp = {
    data: newSelectedRow,
    lookup: {}
};

temp.lookup[`${newSelectedIndex}`] = true;
//if (selectedRows.data.length > 1) {
selectedRows.data = [];
selectedRows.lookup = temp.lookup;
selectedRows.data = temp.data; //selectedRows.data.push(temp.data);
//}