mirror of
https://github.com/ant-design/ant-design.git
synced 2025-08-06 07:56:28 +08:00
feat: Table suport indeterminate checkbox (#3074)
* Table suport indeterminate checkbox * Table support indeterminate Checkbox
This commit is contained in:
parent
f04b65b25e
commit
602000f1a1
@ -565,6 +565,19 @@ export default class Table extends React.Component<TableProps, any> {
|
||||
return record[rowKey as string] || index;
|
||||
}
|
||||
|
||||
checkSelection(data, type, byDefaultChecked) {
|
||||
// type should be 'every' | 'some'
|
||||
if (type === 'every' || type === 'some') {
|
||||
return (
|
||||
byDefaultChecked
|
||||
? data[type](item => this.getCheckboxPropsByItem(item).defaultChecked)
|
||||
: data[type]((item, i) =>
|
||||
this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0)
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
renderRowSelection() {
|
||||
const prefixCls = this.props.prefixCls;
|
||||
const columns = this.props.columns.concat();
|
||||
@ -576,16 +589,26 @@ export default class Table extends React.Component<TableProps, any> {
|
||||
return true;
|
||||
});
|
||||
let checked;
|
||||
let indeterminate;
|
||||
if (!data.length) {
|
||||
checked = false;
|
||||
indeterminate = false;
|
||||
} else {
|
||||
checked = this.state.selectionDirty
|
||||
? data.every((item, i) =>
|
||||
this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0)
|
||||
? this.checkSelection(data, 'every', false)
|
||||
: (
|
||||
data.every((item, i) =>
|
||||
this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0) ||
|
||||
data.every(item => this.getCheckboxPropsByItem(item).defaultChecked)
|
||||
this.checkSelection(data, 'every', false) ||
|
||||
this.checkSelection(data, 'every', true)
|
||||
);
|
||||
indeterminate = this.state.selectionDirty
|
||||
? (
|
||||
this.checkSelection(data, 'some', false) &&
|
||||
!this.checkSelection(data, 'every', false)
|
||||
)
|
||||
: ((this.checkSelection(data, 'some', false) &&
|
||||
!this.checkSelection(data, 'every', false)) ||
|
||||
(this.checkSelection(data, 'some', true) &&
|
||||
!this.checkSelection(data, 'every', true))
|
||||
);
|
||||
}
|
||||
let selectionColumn;
|
||||
@ -599,6 +622,7 @@ export default class Table extends React.Component<TableProps, any> {
|
||||
const checkboxAllDisabled = data.every(item => this.getCheckboxPropsByItem(item).disabled);
|
||||
const checkboxAll = (
|
||||
<Checkbox checked={checked}
|
||||
indeterminate={indeterminate}
|
||||
disabled={checkboxAllDisabled}
|
||||
onChange={this.handleSelectAllRow}
|
||||
/>
|
||||
|
Loading…
Reference in New Issue
Block a user