diff --git a/components/table/SelectionCheckboxAll.tsx b/components/table/SelectionCheckboxAll.tsx index 318f75812a..fc4b833a88 100644 --- a/components/table/SelectionCheckboxAll.tsx +++ b/components/table/SelectionCheckboxAll.tsx @@ -5,7 +5,7 @@ import { Store } from './createStore'; export interface SelectionCheckboxAllProps { store: Store; disabled: boolean; - getCheckboxPropsByItem: (item) => any; + getCheckboxPropsByItem: (item, index) => any; getRecordKey: (record, index?) => string; data: any[]; onChange: (e) => void; @@ -50,7 +50,7 @@ export default class SelectionCheckboxAll extends React.Component getCheckboxPropsByItem(item).defaultChecked) + ? data[type]((item, i) => getCheckboxPropsByItem(item, i).defaultChecked) : data[type]((item, i) => store.getState().selectedRowKeys.indexOf(getRecordKey(item, i)) >= 0) ); diff --git a/components/table/Table.tsx b/components/table/Table.tsx index 05c425b20c..d9285a3760 100755 --- a/components/table/Table.tsx +++ b/components/table/Table.tsx @@ -156,12 +156,12 @@ export default class Table extends React.Component, any> { }); } - getCheckboxPropsByItem = (item) => { + getCheckboxPropsByItem = (item, index) => { const { rowSelection = {} } = this.props; if (!rowSelection.getCheckboxProps) { return {}; } - const key = this.getRecordKey(item); + const key = this.getRecordKey(item, index); // Cache checkboxProps if (!this.CheckboxPropsCache[key]) { this.CheckboxPropsCache[key] = rowSelection.getCheckboxProps(item); @@ -175,7 +175,7 @@ export default class Table extends React.Component, any> { return []; } return this.getFlatData() - .filter(item => this.getCheckboxPropsByItem(item).defaultChecked) + .filter((item, rowIndex) => this.getCheckboxPropsByItem(item, rowIndex).defaultChecked) .map((record, rowIndex) => this.getRecordKey(record, rowIndex)); } @@ -465,7 +465,7 @@ export default class Table extends React.Component, any> { const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection(); const selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection); const changableRowKeys = data - .filter(item => !this.getCheckboxPropsByItem(item).disabled) + .filter((item, i) => !this.getCheckboxPropsByItem(item, i).disabled) .map((item, i) => this.getRecordKey(item, i)); // 记录变化的列 @@ -532,7 +532,7 @@ export default class Table extends React.Component, any> { renderSelectionBox = (type) => { return (_, record, index) => { let rowIndex = this.getRecordKey(record, index); // 从 1 开始 - const props = this.getCheckboxPropsByItem(record); + const props = this.getCheckboxPropsByItem(record, index); const handleChange = (e) => { type === 'radio' ? this.handleRadioSelect(record, rowIndex, e) : this.handleSelect(record, rowIndex, e); @@ -553,12 +553,10 @@ export default class Table extends React.Component, any> { }; } - getRecordKey = (record, index?): string => { + getRecordKey = (record, index): string => { const rowKey = this.props.rowKey; - if (typeof rowKey === 'function') { - return rowKey(record, index); - } - let recordKey = record[rowKey as string] !== undefined ? record[rowKey as string] : index; + const recordKey = (typeof rowKey === 'function') ? + rowKey(record, index) : record[rowKey as string]; warning(recordKey !== undefined, 'Each record in table should have a unique `key` prop, or set `rowKey` to an unique primary key.' ); @@ -569,9 +567,9 @@ export default class Table extends React.Component, any> { const { prefixCls, rowSelection } = this.props; const columns = this.columns.concat(); if (rowSelection) { - const data = this.getFlatCurrentPageData().filter((item) => { + const data = this.getFlatCurrentPageData().filter((item, index) => { if (rowSelection.getCheckboxProps) { - return !this.getCheckboxPropsByItem(item).disabled; + return !this.getCheckboxPropsByItem(item, index).disabled; } return true; }); @@ -581,7 +579,7 @@ export default class Table extends React.Component, any> { className: `${prefixCls}-selection-column`, }; if (rowSelection.type !== 'radio') { - const checkboxAllDisabled = data.every(item => this.getCheckboxPropsByItem(item).disabled); + const checkboxAllDisabled = data.every((item, index) => this.getCheckboxPropsByItem(item, index).disabled); selectionColumn.title = (