Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2918: RowExpander allow function using rowData #3443

Merged
merged 1 commit into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion components/lib/column/column.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type ColumnBodyType = React.ReactNode | ((data: any, options: ColumnBodyOptions)

type ColumnBodyClassType = string | ((data: any, options: ColumnBodyOptions) => string);

type ColumnExpanderType = boolean | ((data: any, options: ColumnBodyOptions) => boolean);

type ColumnFooterType = React.ReactNode | ((options: ColumnFooterOptions) => React.ReactNode);

type ColumnEditorType = React.ReactNode | ((options: ColumnEditorOptions) => React.ReactNode);
Expand Down Expand Up @@ -196,7 +198,7 @@ export interface ColumnProps {
dataType?: ColumnDataType;
editor?: ColumnEditorType;
excludeGlobalFilter?: boolean;
expander?: boolean;
expander?: ColumnExpanderType;
exportable?: boolean;
field?: string;
filter?: boolean;
Expand Down
7 changes: 4 additions & 3 deletions components/lib/datatable/BodyCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,16 +501,17 @@ export const BodyCell = React.memo((props) => {
const tabIndex = getTabIndex(cellSelected);
const selectionMode = getColumnProp('selectionMode');
const rowReorder = getColumnProp('rowReorder');
const expander = getColumnProp('expander');
const rowEditor = getColumnProp('rowEditor');
const header = getColumnProp('header');
const body = getColumnProp('body');
const editor = getColumnProp('editor');
const frozen = getColumnProp('frozen');
const align = getColumnProp('align');
const value = resolveFieldData();
const cellClassName = ObjectUtils.getPropValue(props.cellClassName, value, { props: props.tableProps, rowData: props.rowData, column: props.column });
const bodyClassName = ObjectUtils.getPropValue(getColumnProp('bodyClassName'), props.rowData, { column: props.column, field: field, rowIndex: props.rowIndex, frozenRow: props.frozenRow, props: props.tableProps });
const columnBodyOptions = { column: props.column, field: field, rowIndex: props.rowIndex, frozenRow: props.frozenRow, props: props.tableProps };
const expander = ObjectUtils.getPropValue(getColumnProp('expander'), props.rowData, columnBodyOptions);
const cellClassName = ObjectUtils.getPropValue(props.cellClassName, value, columnBodyOptions);
const bodyClassName = ObjectUtils.getPropValue(getColumnProp('bodyClassName'), props.rowData, columnBodyOptions);
const className = classNames(bodyClassName, getColumnProp('className'), cellClassName, {
'p-selection-column': selectionMode !== null,
'p-editable-column': editor,
Expand Down
48 changes: 34 additions & 14 deletions pages/datatable/rowexpand.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React, { useState, useEffect, useRef, memo } from 'react';
import { DataTable } from '../../components/lib/datatable/DataTable';
import { Column } from '../../components/lib/column/Column';
import { TabView } from '../../components/lib/tabview/TabView';
import getConfig from 'next/config';
import Head from 'next/head';
import React, { memo, useEffect, useRef, useState } from 'react';
import { DocActions } from '../../components/doc/common/docactions';
import { useLiveEditorTabs } from '../../components/doc/common/liveeditor';
import { ProductService } from '../../service/ProductService';
import { Rating } from '../../components/lib/rating//Rating';
import { Button } from '../../components/lib/button/Button';
import { Column } from '../../components/lib/column/Column';
import { DataTable } from '../../components/lib/datatable/DataTable';
import { Rating } from '../../components/lib/rating//Rating';
import { TabView } from '../../components/lib/tabview/TabView';
import { Toast } from '../../components/lib/toast/Toast';
import { DocActions } from '../../components/doc/common/docactions';
import Head from 'next/head';
import getConfig from 'next/config';
import { ProductService } from '../../service/ProductService';

const DataTableRowExpansionDemo = () => {
const [products, setProducts] = useState([]);
Expand Down Expand Up @@ -84,6 +84,10 @@ const DataTableRowExpansionDemo = () => {
return <span className={`product-badge status-${rowData.inventoryStatus.toLowerCase()}`}>{rowData.inventoryStatus}</span>;
};

const allowExpansion = (rowData) => {
return rowData.orders.length > 0;
};

const rowExpansionTemplate = (data) => {
return (
<div className="orders-subtable">
Expand Down Expand Up @@ -139,7 +143,7 @@ const DataTableRowExpansionDemo = () => {
dataKey="id"
header={header}
>
<Column expander style={{ width: '3em' }} />
<Column expander={allowExpansion} style={{ width: '3em' }} />
<Column field="name" header="Name" sortable />
<Column header="Image" body={imageBodyTemplate} />
<Column field="price" header="Price" sortable body={priceBodyTemplate} />
Expand Down Expand Up @@ -259,6 +263,10 @@ export class DataTableRowExpansionDemo extends Component {
return <span className={\`product-badge status-\${rowData.inventoryStatus.toLowerCase()}\`}>{rowData.inventoryStatus}</span>;
}
allowExpansion(rowData) {
return rowData.orders.length > 0;
};
rowExpansionTemplate(data) {
return (
<div className="orders-subtable">
Expand Down Expand Up @@ -291,7 +299,7 @@ export class DataTableRowExpansionDemo extends Component {
<DataTable value={this.state.products} expandedRows={this.state.expandedRows} onRowToggle={(e) => this.setState({ expandedRows: e.data })}
onRowExpand={this.onRowExpand} onRowCollapse={this.onRowCollapse} responsiveLayout="scroll"
rowExpansionTemplate={this.rowExpansionTemplate} dataKey="id" header={header}>
<Column expander style={{ width: '3em' }} />
<Column expander={allowExpansion} style={{ width: '3em' }} />
<Column field="name" header="Name" sortable />
<Column header="Image" body={this.imageBodyTemplate} />
<Column field="price" header="Price" sortable body={this.priceBodyTemplate} />
Expand Down Expand Up @@ -388,6 +396,10 @@ const DataTableRowExpansionDemo = () => {
return <span className={\`product-badge status-\${rowData.inventoryStatus.toLowerCase()}\`}>{rowData.inventoryStatus}</span>;
}
const allowExpansion = (rowData) => {
return rowData.orders.length > 0;
};
const rowExpansionTemplate = (data) => {
return (
<div className="orders-subtable">
Expand Down Expand Up @@ -419,7 +431,7 @@ const DataTableRowExpansionDemo = () => {
<DataTable value={products} expandedRows={expandedRows} onRowToggle={(e) => setExpandedRows(e.data)}
onRowExpand={onRowExpand} onRowCollapse={onRowCollapse} responsiveLayout="scroll"
rowExpansionTemplate={rowExpansionTemplate} dataKey="id" header={header}>
<Column expander style={{ width: '3em' }} />
<Column expander={allowExpansion} style={{ width: '3em' }} />
<Column field="name" header="Name" sortable />
<Column header="Image" body={imageBodyTemplate} />
<Column field="price" header="Price" sortable body={priceBodyTemplate} />
Expand Down Expand Up @@ -515,6 +527,10 @@ const DataTableRowExpansionDemo = () => {
return <span className={\`product-badge status-\${rowData.inventoryStatus.toLowerCase()}\`}>{rowData.inventoryStatus}</span>;
}
const allowExpansion = (rowData) => {
return rowData.orders.length > 0;
};
const rowExpansionTemplate = (data) => {
return (
<div className="orders-subtable">
Expand Down Expand Up @@ -546,7 +562,7 @@ const DataTableRowExpansionDemo = () => {
<DataTable value={products} expandedRows={expandedRows} onRowToggle={(e) => setExpandedRows(e.data)}
onRowExpand={onRowExpand} onRowCollapse={onRowCollapse} responsiveLayout="scroll"
rowExpansionTemplate={rowExpansionTemplate} dataKey="id" header={header}>
<Column expander style={{ width: '3em' }} />
<Column expander={allowExpansion} style={{ width: '3em' }} />
<Column field="name" header="Name" sortable />
<Column header="Image" body={imageBodyTemplate} />
<Column field="price" header="Price" sortable body={priceBodyTemplate} />
Expand Down Expand Up @@ -654,6 +670,10 @@ const DataTableRowExpansionDemo = () => {
return <span className={\`product-badge status-\${rowData.inventoryStatus.toLowerCase()}\`}>{rowData.inventoryStatus}</span>;
}
const allowExpansion = (rowData) => {
return rowData.orders.length > 0;
};
const rowExpansionTemplate = (data) => {
return (
<div className="orders-subtable">
Expand Down Expand Up @@ -685,7 +705,7 @@ const DataTableRowExpansionDemo = () => {
<DataTable value={products} expandedRows={expandedRows} onRowToggle={(e) => setExpandedRows(e.data)}
onRowExpand={onRowExpand} onRowCollapse={onRowCollapse} responsiveLayout="scroll"
rowExpansionTemplate={rowExpansionTemplate} dataKey="id" header={header}>
<Column expander style={{ width: '3em' }} />
<Column expander={allowExpansion} style={{ width: '3em' }} />
<Column field="name" header="Name" sortable />
<Column header="Image" body={imageBodyTemplate} />
<Column field="price" header="Price" sortable body={priceBodyTemplate} />
Expand Down