From fa625dc4577659508fafd21da02e5c90b6f441b0 Mon Sep 17 00:00:00 2001 From: mertsincan Date: Mon, 5 Jul 2021 17:16:54 +0300 Subject: [PATCH] Fixed #2160 - Add cellClassName property to DataTable --- api-generator/components/datatable.js | 6 ++++++ src/components/datatable/BodyCell.js | 11 +++++++++-- src/components/datatable/BodyRow.js | 3 ++- src/components/datatable/DataTable.d.ts | 1 + src/components/datatable/DataTable.js | 4 +++- src/components/datatable/TableBody.js | 2 +- src/showcase/datatable/DataTableDoc.js | 6 ++++++ 7 files changed, 28 insertions(+), 5 deletions(-) diff --git a/api-generator/components/datatable.js b/api-generator/components/datatable.js index 25998f56fc..6d4f4a5d18 100644 --- a/api-generator/components/datatable.js +++ b/api-generator/components/datatable.js @@ -377,6 +377,12 @@ const DataTableProps = [ default: 'null', description: `Function that takes the row data and
returns an object in "{'styleclass' : condition}" format to define a classname for a particular now.` }, + { + name: 'cellClassName', + type: 'function', + default: 'null', + description: `Function that takes the cell data and
returns an object in "{'styleclass' : condition}" format to define a classname for a particular now.` + }, { name: 'rowGroupHeaderTemplate', type: 'function', diff --git a/src/components/datatable/BodyCell.js b/src/components/datatable/BodyCell.js index faa747e030..3239e2da06 100644 --- a/src/components/datatable/BodyCell.js +++ b/src/components/datatable/BodyCell.js @@ -387,7 +387,14 @@ export class BodyCell extends Component { render() { let content, editorKeyHelper; let cellSelected = this.props.allowCellSelection && this.isSelected(); - let cellClassName = classNames(this.props.bodyClassName || this.props.className, { + let cellClassName = null; + + if (this.props.cellClassName) { + let cellData = ObjectUtils.resolveFieldData(this.props.rowData, this.props.field); + cellClassName = this.props.cellClassName(cellData, { ...this.props, ...{ rowData: this.props.rowData } }); + } + + let className = classNames(this.props.bodyClassName || this.props.className, cellClassName, { 'p-selection-column': this.props.selectionMode, 'p-selectable-cell': this.props.allowCellSelection, 'p-highlight': cellSelected, @@ -511,7 +518,7 @@ export class BodyCell extends Component { } return ( - { this.container = el; }} role="cell" tabIndex={tabIndex} className={cellClassName} style={this.props.bodyStyle || this.props.style} onClick={this.onClick} onKeyDown={this.onKeyDown} + { this.container = el; }} role="cell" tabIndex={tabIndex} className={className} style={this.props.bodyStyle || this.props.style} onClick={this.onClick} onKeyDown={this.onKeyDown} rowSpan={this.props.rowSpan} onBlur={this.onBlur} onMouseDown={this.onMouseDown} onMouseUp={this.onMouseUp}> {editorKeyHelper} {content} diff --git a/src/components/datatable/BodyRow.js b/src/components/datatable/BodyRow.js index 50f699da4d..2db4ad8f73 100644 --- a/src/components/datatable/BodyRow.js +++ b/src/components/datatable/BodyRow.js @@ -315,7 +315,8 @@ export class BodyRow extends Component { let cell = ; + showRowReorderElement={this.props.showRowReorderElement} showSelectionElement={this.props.showSelectionElement} allowCellSelection={this.props.allowCellSelection} onClick={this.props.onCellClick} onEditingCellChange={this.props.onEditingCellChange} + cellClassName={this.props.cellClassName} />; cells.push(cell); } diff --git a/src/components/datatable/DataTable.d.ts b/src/components/datatable/DataTable.d.ts index f8daef6a38..54c7ac6633 100644 --- a/src/components/datatable/DataTable.d.ts +++ b/src/components/datatable/DataTable.d.ts @@ -231,6 +231,7 @@ export interface DataTableProps { rowExpansionTemplate?(data: any): React.ReactNode; onRowToggle?(e: DataTableRowToggleParams): void; rowClassName?(data: any): object; + cellClassName?(data: any, options: any): object; rowGroupHeaderTemplate?(data: any, index: number): React.ReactNode; rowGroupFooterTemplate?(data: any, index: number): React.ReactNode; onColumnResizeEnd?(e: DataTableColumnResizeEndParams): void; diff --git a/src/components/datatable/DataTable.js b/src/components/datatable/DataTable.js index 86fa09f0ef..54a464e08a 100644 --- a/src/components/datatable/DataTable.js +++ b/src/components/datatable/DataTable.js @@ -78,6 +78,7 @@ export class DataTable extends Component { rowGroupMode: null, autoLayout: false, rowClassName: null, + cellClassName: null, rowGroupHeaderTemplate: null, rowGroupFooterTemplate: null, loading: false, @@ -196,6 +197,7 @@ export class DataTable extends Component { rowGroupMode: PropTypes.string, autoLayout: PropTypes.bool, rowClassName: PropTypes.func, + cellClassName: PropTypes.func, rowGroupHeaderTemplate: PropTypes.func, rowGroupFooterTemplate: PropTypes.func, loading: PropTypes.bool, @@ -1407,7 +1409,7 @@ export class DataTable extends Component { onRowExpand={this.props.onRowExpand} onRowCollapse={this.props.onRowCollapse} emptyMessage={this.props.emptyMessage} virtualScroll={this.props.virtualScroll} virtualRowHeight={this.props.virtualRowHeight} loading={this.props.loading} groupField={this.props.groupField} rowGroupMode={this.props.rowGroupMode} rowGroupHeaderTemplate={this.props.rowGroupHeaderTemplate} rowGroupFooterTemplate={this.props.rowGroupFooterTemplate} - sortField={this.getSortField()} rowClassName={this.props.rowClassName} onRowReorder={this.props.onRowReorder} + sortField={this.getSortField()} rowClassName={this.props.rowClassName} cellClassName={this.props.cellClassName} onRowReorder={this.props.onRowReorder} editMode={this.props.editMode} editingRows={this.props.editingRows} rowEditorValidator={this.props.rowEditorValidator} onRowEditInit={this.props.onRowEditInit} onRowEditSave={this.props.onRowEditSave} onRowEditCancel={this.props.onRowEditCancel} onRowEditChange={this.props.onRowEditChange} expandableRowGroups={this.props.expandableRowGroups} showRowReorderElement={this.props.showRowReorderElement} showSelectionElement={this.props.showSelectionElement} diff --git a/src/components/datatable/TableBody.js b/src/components/datatable/TableBody.js index 913d5952f9..261d85f6bd 100644 --- a/src/components/datatable/TableBody.js +++ b/src/components/datatable/TableBody.js @@ -796,7 +796,7 @@ export class TableBody extends Component { let bodyRow = this.onRowDragStart(e, i)} onDragEnd={this.onRowDragEnd} onDragOver={(e) => this.onRowDragOver(e, i)} onDragLeave={this.onRowDragLeave} onDrop={this.onRowDrop} virtualScroll={this.props.virtualScroll} virtualRowHeight={this.props.virtualRowHeight} diff --git a/src/showcase/datatable/DataTableDoc.js b/src/showcase/datatable/DataTableDoc.js index ea1e4dc336..315419950a 100644 --- a/src/showcase/datatable/DataTableDoc.js +++ b/src/showcase/datatable/DataTableDoc.js @@ -2925,6 +2925,12 @@ const bodyTemplate = (data, props) => { null Function that takes the row data and
returns an object in "{'styleclass' : condition}" format to define a classname for a particular now. + + cellClassName + function + null + Function that takes the cell data and
returns an object in "{'styleclass' : condition}" format to define a classname for a particular now. + rowGroupHeaderTemplate function