3

I am Working with Material-Table . I need to select multiple rows on clicking a button based on some condition .the condition is written inside the code below. Can anyone guide me on how to select multiple rows based on some condition?

 <MaterialTable
                            icons={tableIcons}title=""

                            columns={[
                                { title: 'Project Name', field: 'Project_Name', render: rowData => <Link to='/projectdetails'>{rowData.Project_Name}</Link> },
                                { title: 'Methods Covered', field: 'Methods_Covered', type: 'numeric' },
                                { title: 'Methods not Covered', field: 'Methods_not_Covered', type: 'numeric' },
                                { title: 'Total Methods', field: 'Total_Methods', type: 'numeric' },

                                    ]}

                            data={this.state.results}


                            components={{
                                Toolbar: props => (
                                    <div>
                                        <MTableToolbar {...props} />
                                        <div className="Mtable">

                                       
                                            <div>
                                                <Button color="primary" onClick={rowData => 
                                                 (rowData.Methods_Covered ? 
                                                  rowData.tableData.checked = true : 
                                                  rowData.tableData.checked = false)}>
                                                 select Highlighted</Button>
                                            </div>
                                            
                                        </div>
                                      
                                    </div>
                                ),
                            }}
                            onSelectionChange={this.handleSelect}
                            onRowClick={this.handleRowClick}
                            options={{
                                sorting: true,
                                selection: true,
                                search: true,
                                searchAutoFocus: true,
                                searchFieldAlignment: 'right',
                                searchFieldStyle: {
                                    border: 'solid black 2px',
                                },
                              }}
  />

2 Answers2

1
<MaterialTable
    // here other params of table component      
    options={{
        selection: true,
        selectionProps: (rowData: any) => ({
            color: "primary",
            checked: agentGroups?.includes(rowData.value) ? true: false,
            onClick: (() => {
                let index = agentGroups.findIndex((group:string)=>group==rowData.value);
                index > -1 ? agentGroups.splice(index , 1) : agentGroups.push(rowData.value);
            }),
        }),
    }}
/>

The key is to modify(push or splice) the array in onClick function which you have used in checked key of selectionProps

  • Remove if already present in array, it will unselect the row
  • Push if not already present in array, it will select the row
Muhammad Awais
  • 1,608
  • 1
  • 21
  • 21
0

I haven't tried the code of the OP, but someone having this problem can check the selectionProps properties of the options props of the MaterialTable:

options={{
  sorting: true,
  selection: true,
  search: true,
  searchAutoFocus: true,
  searchFieldAlignment: 'right',
  searchFieldStyle: {
      border: 'solid black 2px',
  },
  selectionProps: ({ tableData: { checked } }) => ({
    checked
  }),
}}

Please check the last example here: https://material-table.com/#/docs/features/selection

And the docs for options here: https://material-table.com/#/docs/all-props

tiomno
  • 2,178
  • 26
  • 31