fix(multiSelect.js): 修复全选功能中使用rowKey参数bug

This commit is contained in:
huyueb 2018-01-16 17:18:12 +08:00
parent 0dced88a46
commit 5f30910961
7 changed files with 72 additions and 34 deletions

View File

@ -68,7 +68,7 @@ class Demo extends Component {
| useFixedHeader | 是否使用固定表头 | bool | false | | useFixedHeader | 是否使用固定表头 | bool | false |
| bodyStyle | 添加到tablebody上的style | object | {} | | bodyStyle | 添加到tablebody上的style | object | {} |
| style | 添加到table上的style | object | {} | | style | 添加到table上的style | object | {} |
| rowKey | 如果rowKey是字符串`record [rowKey]`将被用作键。如果rowKey是function`rowKeyrecord'的返回值将被用作键。 | string or Function(record):string | 'key' | | rowKey | 如果rowKey是字符串`record [rowKey]`将被用作键。如果rowKey是function`rowKeyrecord, index`的返回值将被用作键。 | string or Function(record, index):string | 'key' |
| rowClassName | 获取行的classname | Function(record, index, indent):string | () => '' | | rowClassName | 获取行的classname | Function(record, index, indent):string | () => '' |
| expandedRowClassName | 获取展开行的className | Function(recode, index, indent):string | () => '' | | expandedRowClassName | 获取展开行的className | Function(recode, index, indent):string | () => '' |
| onExpand | 展开行时的钩子函数 | Function(expanded, record) | () => '' | | onExpand | 展开行时的钩子函数 | Function(expanded, record) | () => '' |

View File

@ -122,10 +122,11 @@ function multiSelect(Table) {
dataIndex: "checkbox", dataIndex: "checkbox",
width: "100px", width: "100px",
render: function render(text, record, index) { render: function render(text, record, index) {
var bool = checkedObj.hasOwnProperty(record["key"]); var rowKey = record["key"] ? record["key"] : _this2.getRowKey(record, i);
var bool = checkedObj.hasOwnProperty(rowKey);
return _react2["default"].createElement(_beeCheckbox2["default"], { return _react2["default"].createElement(_beeCheckbox2["default"], {
className: "table-checkbox", className: "table-checkbox",
checked: checkedObj[record.key], checked: checkedObj[rowKey],
disabled: !bool, disabled: !bool,
onChange: _this2.onCheckboxChange.bind(_this2, text, record, index) onChange: _this2.onCheckboxChange.bind(_this2, text, record, index)
}); });
@ -137,13 +138,23 @@ function multiSelect(Table) {
}; };
multiSelect.prototype.render = function render() { multiSelect.prototype.render = function render() {
var _this3 = this;
var columns = this.renderColumnsMultiSelect(this.props.columns).concat(); var columns = this.renderColumnsMultiSelect(this.props.columns).concat();
return _react2["default"].createElement(Table, _extends({}, this.props, { columns: columns })); return _react2["default"].createElement(Table, _extends({ ref: function ref(table_ref) {
_this3.table_ref = table_ref;
} }, this.props, { columns: columns }));
}; };
return multiSelect; return multiSelect;
}(_react.Component), _initialiseProps = function _initialiseProps() { }(_react.Component), _initialiseProps = function _initialiseProps() {
var _this3 = this; var _this4 = this;
this.getRowKey = function (record, index) {
var rowKey = _this4.props.rowKey || 'key';
var key = typeof rowKey === 'function' ? rowKey(record, index) : record[rowKey];
return key;
};
this.initCheckedObj = function (props) { this.initCheckedObj = function (props) {
var checkedObj = {}, var checkedObj = {},
@ -153,15 +164,16 @@ function multiSelect(Table) {
for (var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
var bool = selectDisabled && selectDisabled(data[i], i) || false; var bool = selectDisabled && selectDisabled(data[i], i) || false;
var rowKey = data[i]["key"] ? data[i]["key"] : _this4.getRowKey(data[i], i);
if (!bool) { if (!bool) {
checkedObj[data[i]["key"]] = selectedRow && selectedRow(data[i], i) || false; checkedObj[rowKey] = selectedRow && selectedRow(data[i], i) || false;
} }
} }
return checkedObj; return checkedObj;
}; };
this.onAllCheckChange = function () { this.onAllCheckChange = function () {
var self = _this3; var self = _this4;
var listData = self.state.data.concat(); var listData = self.state.data.concat();
var checkedObj = _extends({}, self.state.checkedObj); var checkedObj = _extends({}, self.state.checkedObj);
var data = self.props.data; var data = self.props.data;
@ -180,11 +192,12 @@ function multiSelect(Table) {
} }
} }
for (var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
var bool = checkedObj.hasOwnProperty(data[i]["key"]); var rowKey = data[i]["key"] ? data[i]["key"] : _this4.getRowKey(data[i], i);
var bool = checkedObj.hasOwnProperty(rowKey);
if (!bool) { if (!bool) {
selIds.splice(i, 1); selIds.splice(i, 1);
} else { } else {
checkedObj[data[i]["key"]] = !self.state.checkedAll; checkedObj[rowKey] = !self.state.checkedAll;
} }
} }
self.setState({ self.setState({
@ -196,7 +209,7 @@ function multiSelect(Table) {
}; };
this.onCheckboxChange = function (text, record, index) { this.onCheckboxChange = function (text, record, index) {
var self = _this3; var self = _this4;
var allFlag = false; var allFlag = false;
var selIds = self.state.selIds; var selIds = self.state.selIds;
var id = self.props.multiSelect ? self.props.multiSelect.param ? record[self.props.multiSelect.param] : record : record; var id = self.props.multiSelect ? self.props.multiSelect.param ? record[self.props.multiSelect.param] : record : record;
@ -204,12 +217,13 @@ function multiSelect(Table) {
var checkedArray = Object.keys(checkedObj); var checkedArray = Object.keys(checkedObj);
var getSelectedDataFunc = self.props.getSelectedDataFunc; var getSelectedDataFunc = self.props.getSelectedDataFunc;
if (checkedObj[record["key"]]) { var rowKey = record["key"] ? record["key"] : _this4.getRowKey(record, i);
if (checkedObj[rowKey]) {
selIds.remove(id); selIds.remove(id);
} else { } else {
selIds.push(id); selIds.push(id);
} }
checkedObj[record["key"]] = !checkedObj[record["key"]]; checkedObj[rowKey] = !checkedObj[rowKey];
for (var i = 0; i < checkedArray.length; i++) { for (var i = 0; i < checkedArray.length; i++) {
if (!checkedObj[checkedArray[i]]) { if (!checkedObj[checkedArray[i]]) {
allFlag = false; allFlag = false;

36
dist/demo.js vendored
View File

@ -10346,10 +10346,11 @@
dataIndex: "checkbox", dataIndex: "checkbox",
width: "100px", width: "100px",
render: function render(text, record, index) { render: function render(text, record, index) {
var bool = checkedObj.hasOwnProperty(record["key"]); var rowKey = record["key"] ? record["key"] : _this2.getRowKey(record, i);
var bool = checkedObj.hasOwnProperty(rowKey);
return _react2["default"].createElement(_beeCheckbox2["default"], { return _react2["default"].createElement(_beeCheckbox2["default"], {
className: "table-checkbox", className: "table-checkbox",
checked: checkedObj[record.key], checked: checkedObj[rowKey],
disabled: !bool, disabled: !bool,
onChange: _this2.onCheckboxChange.bind(_this2, text, record, index) onChange: _this2.onCheckboxChange.bind(_this2, text, record, index)
}); });
@ -10361,13 +10362,23 @@
}; };
multiSelect.prototype.render = function render() { multiSelect.prototype.render = function render() {
var _this3 = this;
var columns = this.renderColumnsMultiSelect(this.props.columns).concat(); var columns = this.renderColumnsMultiSelect(this.props.columns).concat();
return _react2["default"].createElement(Table, _extends({}, this.props, { columns: columns })); return _react2["default"].createElement(Table, _extends({ ref: function ref(table_ref) {
_this3.table_ref = table_ref;
} }, this.props, { columns: columns }));
}; };
return multiSelect; return multiSelect;
}(_react.Component), _initialiseProps = function _initialiseProps() { }(_react.Component), _initialiseProps = function _initialiseProps() {
var _this3 = this; var _this4 = this;
this.getRowKey = function (record, index) {
var rowKey = _this4.props.rowKey || 'key';
var key = typeof rowKey === 'function' ? rowKey(record, index) : record[rowKey];
return key;
};
this.initCheckedObj = function (props) { this.initCheckedObj = function (props) {
var checkedObj = {}, var checkedObj = {},
@ -10377,15 +10388,16 @@
for (var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
var bool = selectDisabled && selectDisabled(data[i], i) || false; var bool = selectDisabled && selectDisabled(data[i], i) || false;
var rowKey = data[i]["key"] ? data[i]["key"] : _this4.getRowKey(data[i], i);
if (!bool) { if (!bool) {
checkedObj[data[i]["key"]] = selectedRow && selectedRow(data[i], i) || false; checkedObj[rowKey] = selectedRow && selectedRow(data[i], i) || false;
} }
} }
return checkedObj; return checkedObj;
}; };
this.onAllCheckChange = function () { this.onAllCheckChange = function () {
var self = _this3; var self = _this4;
var listData = self.state.data.concat(); var listData = self.state.data.concat();
var checkedObj = _extends({}, self.state.checkedObj); var checkedObj = _extends({}, self.state.checkedObj);
var data = self.props.data; var data = self.props.data;
@ -10404,11 +10416,12 @@
} }
} }
for (var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
var bool = checkedObj.hasOwnProperty(data[i]["key"]); var rowKey = data[i]["key"] ? data[i]["key"] : _this4.getRowKey(data[i], i);
var bool = checkedObj.hasOwnProperty(rowKey);
if (!bool) { if (!bool) {
selIds.splice(i, 1); selIds.splice(i, 1);
} else { } else {
checkedObj[data[i]["key"]] = !self.state.checkedAll; checkedObj[rowKey] = !self.state.checkedAll;
} }
} }
self.setState({ self.setState({
@ -10420,7 +10433,7 @@
}; };
this.onCheckboxChange = function (text, record, index) { this.onCheckboxChange = function (text, record, index) {
var self = _this3; var self = _this4;
var allFlag = false; var allFlag = false;
var selIds = self.state.selIds; var selIds = self.state.selIds;
var id = self.props.multiSelect ? self.props.multiSelect.param ? record[self.props.multiSelect.param] : record : record; var id = self.props.multiSelect ? self.props.multiSelect.param ? record[self.props.multiSelect.param] : record : record;
@ -10428,12 +10441,13 @@
var checkedArray = Object.keys(checkedObj); var checkedArray = Object.keys(checkedObj);
var getSelectedDataFunc = self.props.getSelectedDataFunc; var getSelectedDataFunc = self.props.getSelectedDataFunc;
if (checkedObj[record["key"]]) { var rowKey = record["key"] ? record["key"] : _this4.getRowKey(record, i);
if (checkedObj[rowKey]) {
selIds.remove(id); selIds.remove(id);
} else { } else {
selIds.push(id); selIds.push(id);
} }
checkedObj[record["key"]] = !checkedObj[record["key"]]; checkedObj[rowKey] = !checkedObj[rowKey];
for (var i = 0; i < checkedArray.length; i++) { for (var i = 0; i < checkedArray.length; i++) {
if (!checkedObj[checkedArray[i]]) { if (!checkedObj[checkedArray[i]]) {
allFlag = false; allFlag = false;

2
dist/demo.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -26,7 +26,7 @@ import 'bee-table/build/Table.css';
| useFixedHeader | 是否使用固定表头 | bool | false | | useFixedHeader | 是否使用固定表头 | bool | false |
| bodyStyle | 添加到tablebody上的style | object | {} | | bodyStyle | 添加到tablebody上的style | object | {} |
| style | 添加到table上的style | object | {} | | style | 添加到table上的style | object | {} |
| rowKey | 如果rowKey是字符串`record [rowKey]`将被用作键。如果rowKey是function`rowKeyrecord'的返回值将被用作键。 | string or Function(record):string | 'key' | | rowKey | 如果rowKey是字符串`record [rowKey]`将被用作键。如果rowKey是function`rowKeyrecord, index`的返回值将被用作键。 | string or Function(record, index):string | 'key' |
| rowClassName | 获取行的classname | Function(record, index, indent):string | () => '' | | rowClassName | 获取行的classname | Function(record, index, indent):string | () => '' |
| expandedRowClassName | 获取展开行的className | Function(recode, index, indent):string | () => '' | | expandedRowClassName | 获取展开行的className | Function(recode, index, indent):string | () => '' |
| onExpand | 展开行时的钩子函数 | Function(expanded, record) | () => '' | | onExpand | 展开行时的钩子函数 | Function(expanded, record) | () => '' |

View File

@ -1,6 +1,6 @@
{ {
"name": "bee-table", "name": "bee-table",
"version": "1.0.11", "version": "1.0.12",
"description": "Table ui component for react", "description": "Table ui component for react",
"keywords": [ "keywords": [
"react", "react",

View File

@ -51,13 +51,20 @@ export default function multiSelect(Table) {
}); });
} }
} }
getRowKey=(record, index) => {
const rowKey = this.props.rowKey || 'key';
const key = (typeof rowKey === 'function') ?
rowKey(record, index) : record[rowKey];
return key;
}
initCheckedObj = props => { initCheckedObj = props => {
let checkedObj = {}, let checkedObj = {},
{ selectDisabled, selectedRow, data } = props; { selectDisabled, selectedRow, data } = props;
for (var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
let bool = (selectDisabled && selectDisabled(data[i], i)) || false; let bool = (selectDisabled && selectDisabled(data[i], i)) || false;
let rowKey = data[i]["key"] ? data[i]["key"] : this.getRowKey(data[i],i);
if (!bool) { if (!bool) {
checkedObj[data[i]["key"]] = checkedObj[rowKey] =
(selectedRow && selectedRow(data[i], i)) || false; (selectedRow && selectedRow(data[i], i)) || false;
} }
} }
@ -82,11 +89,12 @@ export default function multiSelect(Table) {
} }
} }
for (var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
let bool = checkedObj.hasOwnProperty(data[i]["key"]); let rowKey = data[i]["key"] ? data[i]["key"] : this.getRowKey(data[i],i);
let bool = checkedObj.hasOwnProperty(rowKey);
if (!bool) { if (!bool) {
selIds.splice(i, 1); selIds.splice(i, 1);
} else { } else {
checkedObj[data[i]["key"]] = !self.state.checkedAll; checkedObj[rowKey] = !self.state.checkedAll;
} }
} }
self.setState({ self.setState({
@ -108,12 +116,13 @@ export default function multiSelect(Table) {
let checkedObj = Object.assign({}, self.state.checkedObj); let checkedObj = Object.assign({}, self.state.checkedObj);
let checkedArray = Object.keys(checkedObj); let checkedArray = Object.keys(checkedObj);
let { getSelectedDataFunc } = self.props; let { getSelectedDataFunc } = self.props;
if (checkedObj[record["key"]]) { let rowKey = record["key"] ? record["key"] : this.getRowKey(record,i);
if (checkedObj[rowKey]) {
selIds.remove(id); selIds.remove(id);
} else { } else {
selIds.push(id); selIds.push(id);
} }
checkedObj[record["key"]] = !checkedObj[record["key"]]; checkedObj[rowKey] = !checkedObj[rowKey];
for (var i = 0; i < checkedArray.length; i++) { for (var i = 0; i < checkedArray.length; i++) {
if (!checkedObj[checkedArray[i]]) { if (!checkedObj[checkedArray[i]]) {
allFlag = false; allFlag = false;
@ -163,11 +172,12 @@ export default function multiSelect(Table) {
dataIndex: "checkbox", dataIndex: "checkbox",
width: "100px", width: "100px",
render: (text, record, index) => { render: (text, record, index) => {
let bool = checkedObj.hasOwnProperty(record["key"]); let rowKey = record["key"] ? record["key"] : this.getRowKey(record,i);
let bool = checkedObj.hasOwnProperty(rowKey);
return ( return (
<Checkbox <Checkbox
className="table-checkbox" className="table-checkbox"
checked={checkedObj[record.key]} checked={checkedObj[rowKey]}
disabled={!bool} disabled={!bool}
onChange={this.onCheckboxChange.bind( onChange={this.onCheckboxChange.bind(
this, this,
@ -186,7 +196,7 @@ export default function multiSelect(Table) {
} }
render() { render() {
let columns = this.renderColumnsMultiSelect(this.props.columns).concat(); let columns = this.renderColumnsMultiSelect(this.props.columns).concat();
return <Table {...this.props} columns={columns} />; return <Table ref={(table_ref) => { this.table_ref = table_ref; }} {...this.props} columns={columns} />;
} }
}; };
} }