From 64d40593163609d975532a04b3d41c6e45857414 Mon Sep 17 00:00:00 2001 From: jonyshi Date: Wed, 18 Apr 2018 16:10:02 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=90=88=E5=B9=B6=E6=A0=87=E9=A2=98?= =?UTF-8?q?=E5=90=8E=E7=9A=84=E5=90=88=E8=AE=A1,=E4=B8=94=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=9A=E5=AD=97=E6=AE=B5=E7=BB=9F=E8=AE=A1):=20?= =?UTF-8?q?=E5=90=88=E5=B9=B6=E6=A0=87=E9=A2=98=E5=90=8E=E7=9A=84=E5=90=88?= =?UTF-8?q?=E8=AE=A1,=E4=B8=94=E6=94=AF=E6=8C=81=E5=A4=9A=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 合并标题后的合计,且支持多字段统计 BREAKING CHANGE: 合并标题后的合计,且支持多字段统计 --- build/lib/sum.js | 99 +++++++++++++++++++++++++++++++++++++++++++++++- package.json | 2 +- 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/build/lib/sum.js b/build/lib/sum.js index 78483c3..6557c5c 100644 --- a/build/lib/sum.js +++ b/build/lib/sum.js @@ -37,6 +37,7 @@ function sum(Table) { function SumTable(props) { _classCallCheck(this, SumTable); + //array , tree var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this.currentFooter = function () { @@ -76,17 +77,113 @@ function sum(Table) { return _react2["default"].createElement(Table, _extends({}, _this.props, { loading: false, footerScroll: true, showHeader: false, columns: columns_sum, data: obj })); }; + _this.currentTreeFooter = function () { + var _this$props = _this.props, + columns = _this$props.columns, + data = _this$props.data; + + var _columns = []; + _this.getNodeItem(columns, _columns); + var _countObj = {}; + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; + + try { + var _loop = function _loop() { + var column = _step.value; + + if (typeof column.render == "function" && !column.sumCol) { + column.render = ""; + } + if (column.sumCol) { + var count = 0; + data.forEach(function (da, i) { + var _num = da[column.key]; + count += _num; + }); + _countObj[column.key] = count; + } + }; + + for (var _iterator = _columns[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + _loop(); + } + } catch (err) { + _didIteratorError = true; + _iteratorError = err; + } finally { + try { + if (!_iteratorNormalCompletion && _iterator["return"]) { + _iterator["return"](); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } + } + } + + var _sumArray = [_extends({ key: "sumData", showSum: "合计" }, _countObj)]; + columns[0] = _extends({}, columns[0], columns2); + return _react2["default"].createElement(Table, _extends({}, _this.props, { bordered: false, loading: false, footerScroll: true, showHeader: false, columns: columns, data: _sumArray })); + }; + + _this.getNodeItem = function (array, newArray) { + array.forEach(function (da, i) { + if (da.children) { + _this.getNodeItem(da.children, newArray); + } else { + newArray.push(da); + } + }); + }; + + _this.getTableType = function () { + var columns = _this.props.columns; + + var type = "array"; + columns.find(function (da, i) { + if (da.children) { + type = "tree"; + return type; + } + }); + return type; + }; + + _this.setFooterRender = function () { + var columns = _this.props.columns; + + if (!Array.isArray(columns)) { + console.log("data type is error !");return; + } + var type = _this.getTableType(); + if (type == "tree") { + return _this.currentTreeFooter(); + } else { + return _this.currentFooter(); + } + }; + + _this.tableType = "array"; return _this; } //合计数字列,并将计算所得数据存储到一个obj对象中 + /** + * 获取当前的表格类型。 + * + */ + + SumTable.prototype.render = function render() { return _react2["default"].createElement(Table, _extends({}, this.props, { footerScroll: true, columns: this.props.columns, data: this.props.data, - footer: this.currentFooter + footer: this.setFooterRender })); }; diff --git a/package.json b/package.json index 23e4957..07d1ad1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bee-table", - "version": "1.1.0", + "version": "1.1.1", "description": "Table ui component for react", "keywords": [ "react",