This commit is contained in:
huyueb 2018-04-02 14:38:00 +08:00
parent 6ca4030c47
commit 85b7ada464
5 changed files with 122 additions and 110 deletions

View File

@ -77,14 +77,18 @@ function multiSelect(Table) {
selectDisabled = props.selectDisabled,
selectedRow = props.selectedRow,
data = props.data,
selIds = void 0,
obj = void 0,
checkedObj = {};
if (nextProps.data !== data || nextProps.selectDisabled !== selectDisabled || nextProps.selectedRow !== selectedRow) {
checkedObj = this.initCheckedObj(nextProps).checkedObj;
obj = this.initCheckedObj(nextProps);
checkedObj = obj.checkedObj;
selIds = obj.selIds;
this.setState({
checkedAll: false,
checkedObj: checkedObj,
selIds: [],
selIds: selIds,
data: nextProps.data
});
}

213
dist/demo.js vendored
View File

@ -9969,6 +9969,8 @@
* 使用全选时得注意data中的key值一定要是唯一值
*/
function multiSelect(Table) {
var _class, _temp, _initialiseProps;
Array.prototype.indexOf = function (val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
@ -9981,7 +9983,7 @@
this.splice(index, 1);
}
};
return function (_Component) {
return _temp = _class = function (_Component) {
_inherits(multiSelect, _Component);
function multiSelect(props) {
@ -9989,107 +9991,7 @@
var _this = _possibleConstructorReturn(this, _Component.call(this, props));
_this.getRowKey = function (record, index) {
var rowKey = _this.props.rowKey || 'key';
var key = typeof rowKey === 'function' ? rowKey(record, index) : record[rowKey];
return key;
};
_this.initCheckedObj = function () {
var checkedObj = {},
_this$props = _this.props,
selectDisabled = _this$props.selectDisabled,
selectedRow = _this$props.selectedRow,
data = _this$props.data,
selIds_ = _this.state.selIds;
for (var i = 0; i < data.length; i++) {
var bool = selectDisabled && selectDisabled(data[i], i) || false;
var rowKey = data[i]["key"] ? data[i]["key"] : _this.getRowKey(data[i], i);
if (!bool) {
if (selectedRow && selectedRow(data[i], i)) {
selIds_.push(data[i]);
checkedObj[rowKey] = true;
} else {
checkedObj[rowKey] = false;
}
}
}
return {
checkedObj: checkedObj,
selIds: selIds_
};
};
_this.onAllCheckChange = function () {
var self = _this;
var listData = self.state.data.concat();
var checkedObj = _extends({}, self.state.checkedObj);
var data = self.props.data;
var selIds = [];
var id = self.props.multiSelect.param;
if (self.state.checkedAll) {
selIds = [];
} else {
for (var i = 0; i < listData.length; i++) {
if (id) {
selIds[i] = listData[i][id];
} else {
selIds[i] = listData[i];
}
}
}
for (var i = 0; i < data.length; i++) {
var rowKey = data[i]["key"] ? data[i]["key"] : _this.getRowKey(data[i], i);
var bool = checkedObj.hasOwnProperty(rowKey);
if (!bool) {
selIds.splice(i, 1);
} else {
checkedObj[rowKey] = !self.state.checkedAll;
}
}
self.setState({
checkedAll: !self.state.checkedAll,
checkedObj: checkedObj,
selIds: selIds
});
self.props.getSelectedDataFunc(selIds);
};
_this.onCheckboxChange = function (text, record, index) {
var self = _this;
var allFlag = false;
var selIds = self.state.selIds;
var id = self.props.multiSelect ? self.props.multiSelect.param ? record[self.props.multiSelect.param] : record : record;
var checkedObj = _extends({}, self.state.checkedObj);
var checkedArray = Object.keys(checkedObj);
var getSelectedDataFunc = self.props.getSelectedDataFunc;
var rowKey = record["key"] ? record["key"] : _this.getRowKey(record, i);
if (checkedObj[rowKey]) {
selIds.remove(id);
} else {
selIds.push(id);
}
checkedObj[rowKey] = !checkedObj[rowKey];
for (var i = 0; i < checkedArray.length; i++) {
if (!checkedObj[checkedArray[i]]) {
allFlag = false;
break;
} else {
allFlag = true;
}
}
self.setState({
checkedAll: allFlag,
checkedObj: checkedObj,
selIds: selIds
});
if (typeof getSelectedDataFunc === "function") {
getSelectedDataFunc(selIds);
}
};
_initialiseProps.call(_this);
_this.state = {
checkedAll: false,
@ -10101,7 +10003,7 @@
}
multiSelect.prototype.componentDidMount = function componentDidMount() {
this.setState(this.initCheckedObj());
this.setState(this.initCheckedObj(this.props));
};
multiSelect.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
@ -10180,7 +10082,110 @@
};
return multiSelect;
}(_react.Component);
}(_react.Component), _initialiseProps = function _initialiseProps() {
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) {
var checkedObj = {},
selectDisabled = props.selectDisabled,
selectedRow = props.selectedRow,
data = props.data,
selIds_ = _this4.state.selIds;
for (var i = 0; i < data.length; i++) {
var bool = selectDisabled && selectDisabled(data[i], i) || false;
var rowKey = data[i]["key"] ? data[i]["key"] : _this4.getRowKey(data[i], i);
if (!bool) {
if (selectedRow && selectedRow(data[i], i)) {
selIds_.push(data[i]);
checkedObj[rowKey] = true;
} else {
checkedObj[rowKey] = false;
}
}
}
return {
checkedObj: checkedObj,
selIds: selIds_
};
};
this.onAllCheckChange = function () {
var self = _this4;
var listData = self.state.data.concat();
var checkedObj = _extends({}, self.state.checkedObj);
var data = self.props.data;
var selIds = [];
var id = self.props.multiSelect.param;
if (self.state.checkedAll) {
selIds = [];
} else {
for (var i = 0; i < listData.length; i++) {
if (id) {
selIds[i] = listData[i][id];
} else {
selIds[i] = listData[i];
}
}
}
for (var i = 0; i < data.length; i++) {
var rowKey = data[i]["key"] ? data[i]["key"] : _this4.getRowKey(data[i], i);
var bool = checkedObj.hasOwnProperty(rowKey);
if (!bool) {
selIds.splice(i, 1);
} else {
checkedObj[rowKey] = !self.state.checkedAll;
}
}
self.setState({
checkedAll: !self.state.checkedAll,
checkedObj: checkedObj,
selIds: selIds
});
self.props.getSelectedDataFunc(selIds);
};
this.onCheckboxChange = function (text, record, index) {
var self = _this4;
var allFlag = false;
var selIds = self.state.selIds;
var id = self.props.multiSelect ? self.props.multiSelect.param ? record[self.props.multiSelect.param] : record : record;
var checkedObj = _extends({}, self.state.checkedObj);
var checkedArray = Object.keys(checkedObj);
var getSelectedDataFunc = self.props.getSelectedDataFunc;
var rowKey = record["key"] ? record["key"] : _this4.getRowKey(record, i);
if (checkedObj[rowKey]) {
selIds.remove(id);
} else {
selIds.push(id);
}
checkedObj[rowKey] = !checkedObj[rowKey];
for (var i = 0; i < checkedArray.length; i++) {
if (!checkedObj[checkedArray[i]]) {
allFlag = false;
break;
} else {
allFlag = true;
}
}
self.setState({
checkedAll: allFlag,
checkedObj: checkedObj,
selIds: selIds
});
if (typeof getSelectedDataFunc === "function") {
getSelectedDataFunc(selIds);
}
};
}, _temp;
}
module.exports = exports["default"];

2
dist/demo.js.map vendored

File diff suppressed because one or more lines are too long

View File

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

View File

@ -37,17 +37,20 @@ export default function multiSelect(Table) {
componentWillReceiveProps(nextProps) {
let props = this.props,
{ selectDisabled, selectedRow, data } = props,
selIds,obj,
checkedObj = {};
if (
nextProps.data !== data ||
nextProps.selectDisabled !== selectDisabled ||
nextProps.selectedRow !== selectedRow
) {
checkedObj = this.initCheckedObj(nextProps).checkedObj;
obj = this.initCheckedObj(nextProps);
checkedObj = obj.checkedObj;
selIds = obj.selIds;
this.setState({
checkedAll: false,
checkedObj: checkedObj,
selIds: [],
selIds: selIds,
data: nextProps.data
});
}