From a0f6a1cbbba155ea8d10642d30381adcf0321ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20B=C3=BCy=C3=BCkdere?= Date: Sun, 26 Sep 2021 18:19:36 +0300 Subject: [PATCH] Feature Request #1303 , datatable added selection disabled prop. --- src/components/datatable/BodyCell.js | 5 +- src/components/datatable/BodyRow.js | 2 +- src/components/datatable/DataTable.d.ts | 1 + src/components/datatable/DataTable.js | 12 +- src/components/datatable/RowRadioButton.js | 2 +- src/components/datatable/TableBody.js | 15 +- .../datatable/DataTableSelectionDemo.js | 214 ++++++++++++++++++ 7 files changed, 243 insertions(+), 8 deletions(-) diff --git a/src/components/datatable/BodyCell.js b/src/components/datatable/BodyCell.js index 3f5f37c132..5a851b0f91 100644 --- a/src/components/datatable/BodyCell.js +++ b/src/components/datatable/BodyCell.js @@ -432,10 +432,11 @@ export class BodyCell extends Component { } if (showSelection) { + let isDisabled = this.props.isDataSelectable && !this.props.isDataSelectable(); if (this.props.selectionMode === 'single') - content = ; + content = ; else - content = ; + content = ; } } else if (this.props.rowReorder) { diff --git a/src/components/datatable/BodyRow.js b/src/components/datatable/BodyRow.js index 7db21ad90b..46d3f78bd5 100644 --- a/src/components/datatable/BodyRow.js +++ b/src/components/datatable/BodyRow.js @@ -321,7 +321,7 @@ export class BodyRow extends Component { let editing = this.getEditing(); let cell = ; diff --git a/src/components/datatable/DataTable.d.ts b/src/components/datatable/DataTable.d.ts index 54c7ac6633..d43101af13 100644 --- a/src/components/datatable/DataTable.d.ts +++ b/src/components/datatable/DataTable.d.ts @@ -266,6 +266,7 @@ export interface DataTableProps { customRestoreState?(): object; onStateSave?(state: object): void; onStateRestore?(state: object): void; + isDataSelectable?(data: any | DataTableCellClickEventParams) : boolean; } export declare class DataTable extends React.Component { diff --git a/src/components/datatable/DataTable.js b/src/components/datatable/DataTable.js index ec85f5b590..00a728b063 100644 --- a/src/components/datatable/DataTable.js +++ b/src/components/datatable/DataTable.js @@ -1240,6 +1240,9 @@ export class DataTable extends Component { if (!event.checked) { let visibleData = this.hasFilter() ? this.processData() : this.props.value; + if(this.props.isDataSelectable){ + visibleData = visibleData?.filter((k) => this.props.isDataSelectable(k)) + } selection = [...visibleData]; this.props.onAllRowsSelect && this.props.onAllRowsSelect({ originalEvent, data: selection, type: 'all' }); @@ -1353,7 +1356,12 @@ export class DataTable extends Component { return this.props.selection && this.props.totalRecords && this.props.selection.length === this.props.totalRecords; } - return this.props.selection && visibleData && visibleData.length && this.props.selection.length === visibleData.length; + let unselectableDataCount = 0; + if(this.props.isDataSelectable){ + unselectableDataCount = visibleData?.filter((k) => !this.props.isDataSelectable(k)).length; + } + + return this.props.selection && visibleData && visibleData.length && (this.props.selection.length + unselectableDataCount) === visibleData.length; } getFrozenColumns(columns) { @@ -1406,7 +1414,7 @@ export class DataTable extends Component { createTableBody(value, columns, frozen, selectionModeInColumn) { return this.props.isDataSelectable(data) === true); + } if (this.props.onSelectionChange && selection !== this.props.selection) { this.props.onSelectionChange({ originalEvent: event.originalEvent, @@ -793,9 +803,10 @@ export class TableBody extends Component { let isRowGroupExpanded = this.props.expandableRowGroups && hasSubheaderGrouping && rowGroupHeaderExpanded; if (!this.props.expandableRowGroups || isRowGroupExpanded) { //row content + let bodyRow = this.props.isDataSelectable(rowData))} selectOnEdit={this.props.selectOnEdit} onRadioClick={this.onRadioClick} onCheckboxClick={this.onCheckboxClick} selected={selected} contextMenuSelected={contextMenuSelected} rowClassName={this.props.rowClassName} cellClassName={this.props.cellClassName} sortField={this.props.sortField} rowGroupMode={this.props.rowGroupMode} groupRowSpan={groupRowSpan} onDragStart={(e) => this.onRowDragStart(e, i)} onDragEnd={this.onRowDragEnd} onDragOver={(e) => this.onRowDragOver(e, i)} onDragLeave={this.onRowDragLeave} diff --git a/src/showcase/datatable/DataTableSelectionDemo.js b/src/showcase/datatable/DataTableSelectionDemo.js index 5eb9df91b4..f931813574 100644 --- a/src/showcase/datatable/DataTableSelectionDemo.js +++ b/src/showcase/datatable/DataTableSelectionDemo.js @@ -63,6 +63,14 @@ export class DataTableSelectionDemo extends Component { return str.charAt(0).toUpperCase() + str.slice(1); } + isDataCellSelectable(data){ + return !(data.field === "category" && data.value === "Accessories"); + } + + isDataRowSelectable(rowData){ + return rowData.category !== 'Accessories'; + } + render() { return (
@@ -176,6 +184,7 @@ export class DataTableSelectionDemo extends Component { +
@@ -222,8 +231,41 @@ export class DataTableSelectionDemo extends Component {
+
+
Disabled Selection
+

Whether data can be selected or not can be handled by "isDataSelectable" prop.

+ +
Row Selection Disabled
+

Rows which category is "Accessories" can not be selected.

+ this.setState({selectedProducts8 : e.value })} dataKey="id"> + + + + + + + +
Cell Selection Disabled
+

Cell which is in category column and has "Accessories" value can not be selected.

+ this.setState({ selectedProduct2: e.value })} dataKey="id"> + + + + + + +
Cell Selection Disabled With Drag Selection
+ this.setState({selectedProducts6 : e.value })} dataKey="id"> + + + + + +
+ + ); @@ -300,6 +342,15 @@ export class DataTableSelectionDemo extends Component { return str.charAt(0).toUpperCase() + str.slice(1); } + isDataCellSelectable(data){ + return !(data.field === "category" && data.value === "Accessories"); + } + + isDataRowSelectable(rowData){ + return rowData.category !== 'Accessories'; + } + + render() { return (
@@ -449,6 +500,40 @@ export class DataTableSelectionDemo extends Component {
+ +
+
Disabled Selection
+

Whether data can be selected or not can be handled by "isDataSelectable" prop.

+ +
Row Selection Disabled
+

Rows which category is "Accessories" can not be selected.

+ this.setState({selectedProducts8 : e.value })} dataKey="id"> + + + + + + + + +
Cell Selection Disabled
+

Cell which is in category column and has "Accessories" value can not be selected.

+ this.setState({ selectedProduct2: e.value })} dataKey="id"> + + + + + + +
Cell Selection Disabled With Drag Selection
+ this.setState({selectedProducts6 : e.value })} dataKey="id"> + + + + + +
+ ); } @@ -510,6 +595,14 @@ const DataTableSelectionDemo = () => { return str.charAt(0).toUpperCase() + str.slice(1); } + const isDataCellSelectable = (data) => { + return !(data.field === "category" && data.value === "Accessories"); + } + + const isDataRowSelectable =(rowData) => { + return rowData.category !== 'Accessories'; + } + return (
@@ -658,6 +751,48 @@ const DataTableSelectionDemo = () => {
+ +
+
Disabled Selection
+

Whether data can be selected or not can be handled by "isDataSelectable" prop.

+ +
Row Selection Disabled
+

Rows which category is "Accessories" can not be selected.

+ setSelectedProducts8(e.value)} dataKey="id"> + + + + + + + +
Cell Selection Disabled
+

Cell which is in category column and has "Accessories" value can not be selected.

+ setSelectedProduct2(e.value)} dataKey="id"> + + + + + + +
Cell Selection Disabled
+

Cell which is in category column and has "Accessories" value can not be selected.

+ setselectedProduct2(e.value)} dataKey="id"> + + + + + + +
Cell Selection Disabled With Drag Selection
+ setselectedProducts6(e.value)} dataKey="id"> + + + + + +
); } @@ -718,6 +853,14 @@ const DataTableSelectionDemo = () => { toast.current.show({ severity: 'warn', summary: \`Item Unselected In Product\`, detail: \`\${toCapitalize(event.field)}: \${event.value}\`, life: 3000 }); } + const isDataCellSelectable = (data) => { + return !(data.field === "category" && data.value === "Accessories"); + } + + const isDataRowSelectable =(rowData) => { + return rowData.category !== 'Accessories'; + } + return (
@@ -866,6 +1009,38 @@ const DataTableSelectionDemo = () => {
+ +
+
Disabled Selection
+

Whether data can be selected or not can be handled by "isDataSelectable" prop.

+ +
Row Selection Disabled
+

Rows which category is "Accessories" can not be selected.

+ setSelectedProducts8(e.value)} dataKey="id"> + + + + + + + +
Cell Selection Disabled
+

Cell which is in category column and has "Accessories" value can not be selected.

+ setselectedProduct2(e.value)} dataKey="id"> + + + + + + +
Cell Selection Disabled With Drag Selection
+ setselectedProducts6(e.value)} dataKey="id"> + + + + + +
); } @@ -933,6 +1108,14 @@ const DataTableSelectionDemo = () => { return str.charAt(0).toUpperCase() + str.slice(1); } + const isDataCellSelectable = (data) => { + return !(data.field === "category" && data.value === "Accessories"); + } + + const isDataRowSelectable =(rowData) => { + return rowData.category !== 'Accessories'; + } + return (
@@ -1081,6 +1264,37 @@ const DataTableSelectionDemo = () => {
+ +
+
Disabled Selection
+

Whether data can be selected or not can be handled by "isDataSelectable" prop.

+
Row Selection Disabled
+

Rows which category is "Accessories" can not be selected.

+ setSelectedProducts8(e.value)} dataKey="id"> + + + + + + + +
Cell Selection Disabled
+

Cell which is in category column and has "Accessories" value can not be selected.

+ setselectedProduct2(e.value)} dataKey="id"> + + + + + + +
Cell Selection Disabled With Drag Selection
+ setselectedProducts6(e.value)} dataKey="id"> + + + + + +
); }