合计逻辑重构
This commit is contained in:
parent
78d4156b2b
commit
4542f6a211
|
@ -178,6 +178,8 @@
|
|||
transition: background 0.3s ease; }
|
||||
.u-table th[colspan] {
|
||||
text-align: center; }
|
||||
.u-table th ::last-child {
|
||||
overflow: hidden; }
|
||||
.u-table td {
|
||||
border-bottom: 1px solid #e9e9e9; }
|
||||
.u-table tr {
|
||||
|
|
|
@ -629,6 +629,11 @@ var Table = function (_Component) {
|
|||
leafColumns = this.columnManager.leafColumns();
|
||||
}
|
||||
|
||||
//合计代码如果是最后一行并且有合计功能时,最后一行为合计列
|
||||
if (i == data.length - 1 && props.showSum) {
|
||||
className = className + ' sumrow';
|
||||
}
|
||||
|
||||
rst.push(_react2["default"].createElement(_TableRow2["default"], _extends({
|
||||
indent: indent,
|
||||
indentSize: props.indentSize,
|
||||
|
@ -914,10 +919,15 @@ var Table = function (_Component) {
|
|||
)
|
||||
);
|
||||
}
|
||||
|
||||
var leftFixedWidth = this.columnManager.getLeftColumnsWidth();
|
||||
var rightFixedWidth = this.columnManager.getRightColumnsWidth();
|
||||
var parStyle = {};
|
||||
if (!fixed) {
|
||||
parStyle = { 'marginLeft': leftFixedWidth, 'marginRight': rightFixedWidth };
|
||||
}
|
||||
return _react2["default"].createElement(
|
||||
'span',
|
||||
null,
|
||||
'div',
|
||||
{ style: parStyle },
|
||||
headTable,
|
||||
BodyTable
|
||||
);
|
||||
|
@ -1124,7 +1134,7 @@ var Table = function (_Component) {
|
|||
{ className: clsPrefix + '-content' },
|
||||
_react2["default"].createElement(
|
||||
'div',
|
||||
{ className: isTableScroll ? clsPrefix + '-scroll' : '', style: { 'marginLeft': leftFixedWidth } },
|
||||
{ className: isTableScroll ? clsPrefix + '-scroll' : '' },
|
||||
this.getTable({ columns: this.columnManager.groupedColumns() }),
|
||||
this.getEmptyText(),
|
||||
this.getFooter()
|
||||
|
|
|
@ -72,7 +72,8 @@ var TableCell = function (_Component) {
|
|||
index = _props2.index,
|
||||
expandIcon = _props2.expandIcon,
|
||||
column = _props2.column,
|
||||
fixed = _props2.fixed;
|
||||
fixed = _props2.fixed,
|
||||
showSum = _props2.showSum;
|
||||
var dataIndex = column.dataIndex,
|
||||
render = column.render;
|
||||
var _column$className = column.className,
|
||||
|
@ -84,7 +85,7 @@ var TableCell = function (_Component) {
|
|||
var colSpan = void 0;
|
||||
var rowSpan = void 0;
|
||||
|
||||
if (render) {
|
||||
if (render && !showSum) {
|
||||
text = render(text, record, index);
|
||||
if (this.isInvalidRenderCellText(text)) {
|
||||
tdProps = text.props || {};
|
||||
|
|
|
@ -583,7 +583,8 @@ var TableHeader = function (_Component) {
|
|||
delete da.drgHover;
|
||||
var fixedStyle = "";
|
||||
var canDotDrag = "";
|
||||
if (!fixed && (da.fixed || rows[0][columIndex].fixed)) {
|
||||
//主表格下、固定列或者是过滤行中含有固定列时添加该属性
|
||||
if (!fixed && (da.fixed || filterable && index == rows.length - 1 && rows[0][columIndex].fixed)) {
|
||||
fixedStyle = clsPrefix + "-row-fixed-columns-in-body";
|
||||
}
|
||||
|
||||
|
@ -607,8 +608,8 @@ var TableHeader = function (_Component) {
|
|||
if (!da.fixed) {
|
||||
return _react2["default"].createElement(
|
||||
"th",
|
||||
{ key: 'table-header-th-' + da.dataindex, className: thClassName, "data-th-fixed": da.fixed,
|
||||
"data-line-key": da.key, "data-line-index": columIndex, "data-th-width": da.width },
|
||||
_extends({}, da, { key: 'table-header-th-' + da.dataindex, className: thClassName, "data-th-fixed": da.fixed,
|
||||
"data-line-key": da.key, "data-line-index": columIndex, "data-th-width": da.width }),
|
||||
da.children,
|
||||
dragborder ? _react2["default"].createElement(
|
||||
"div",
|
||||
|
|
|
@ -187,6 +187,8 @@ var TableRow = function (_Component) {
|
|||
indentSize = _props7.indentSize,
|
||||
isHiddenExpandIcon = _props7.isHiddenExpandIcon,
|
||||
fixed = _props7.fixed;
|
||||
|
||||
var showSum = false;
|
||||
var className = this.props.className;
|
||||
|
||||
|
||||
|
@ -194,6 +196,10 @@ var TableRow = function (_Component) {
|
|||
className += ' ' + clsPrefix + '-hover';
|
||||
}
|
||||
|
||||
//判断是否为合计行
|
||||
if (className.indexOf('sumrow') > -1) {
|
||||
showSum = true;
|
||||
}
|
||||
var cells = [];
|
||||
|
||||
var expandIcon = _react2["default"].createElement(_ExpandIcon2["default"], {
|
||||
|
@ -207,7 +213,7 @@ var TableRow = function (_Component) {
|
|||
});
|
||||
|
||||
for (var i = 0; i < columns.length; i++) {
|
||||
if (expandIconAsCell && i === 0) {
|
||||
if (expandIconAsCell && i === 0 && !showSum) {
|
||||
cells.push(_react2["default"].createElement(
|
||||
'td',
|
||||
{
|
||||
|
@ -217,7 +223,7 @@ var TableRow = function (_Component) {
|
|||
expandIcon
|
||||
));
|
||||
}
|
||||
var isColumnHaveExpandIcon = expandIconAsCell || expandRowByClick ? false : i === expandIconColumnIndex;
|
||||
var isColumnHaveExpandIcon = expandIconAsCell || expandRowByClick || showSum ? false : i === expandIconColumnIndex;
|
||||
cells.push(_react2["default"].createElement(_TableCell2["default"], {
|
||||
clsPrefix: clsPrefix,
|
||||
record: record,
|
||||
|
@ -227,6 +233,7 @@ var TableRow = function (_Component) {
|
|||
column: columns[i],
|
||||
key: columns[i].key,
|
||||
fixed: fixed,
|
||||
showSum: showSum,
|
||||
expandIcon: isColumnHaveExpandIcon ? expandIcon : null
|
||||
}));
|
||||
}
|
||||
|
|
158
build/lib/sum.js
158
build/lib/sum.js
|
@ -22,13 +22,6 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
|
|||
|
||||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
|
||||
|
||||
//创建新列存放 “合计” 字段
|
||||
var columns2 = {
|
||||
title: "合计",
|
||||
key: "showSum",
|
||||
dataIndex: "showSum"
|
||||
};
|
||||
|
||||
function sum(Table) {
|
||||
return function (_React$Component) {
|
||||
_inherits(SumTable, _React$Component);
|
||||
|
@ -40,98 +33,6 @@ function sum(Table) {
|
|||
//array , tree
|
||||
var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
|
||||
|
||||
_this.currentFooter = function () {
|
||||
var data_2 = _this.props.data;
|
||||
var columns_sum = _this.props.columns.concat();
|
||||
var sumCol_index = void 0,
|
||||
sumColIndexArr = [];
|
||||
//用一个对象存储合计数据,这里合计对象的属性对应每列字段
|
||||
for (var i = 0; i < columns_sum.length; i++) {
|
||||
if (columns_sum[i].sumCol) {
|
||||
sumColIndexArr.push(columns_sum[i].dataIndex);
|
||||
}
|
||||
}
|
||||
var obj = {};
|
||||
sumColIndexArr.forEach(function (sumCol_index) {
|
||||
|
||||
obj[sumCol_index] = 0;
|
||||
if (Array.isArray(data_2)) {
|
||||
for (var _i = 0; _i < data_2.length; _i++) {
|
||||
if (typeof data_2[_i][sumCol_index] == "number" || !isNaN(data_2[_i][sumCol_index])) {
|
||||
obj[sumCol_index] -= -data_2[_i][sumCol_index];
|
||||
} else {
|
||||
obj[sumCol_index] = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
obj.key = sumCol_index + "sumData";
|
||||
});
|
||||
obj.showSum = "合计";
|
||||
obj = [obj];
|
||||
//将设置的和用户传入的合并属性
|
||||
columns_sum[0] = _extends({}, columns_sum[0], columns2);
|
||||
//除去列为特殊渲染的,避免像a标签这种html代码写入到合计中
|
||||
columns_sum.map(function (item, index) {
|
||||
if (typeof item.render == "function" && !item.sumCol) {
|
||||
item.render = "";
|
||||
}
|
||||
return item;
|
||||
});
|
||||
return _react2["default"].createElement(Table, _extends({}, _this.props, { loading: false, footerScroll: true, showHeader: false, columns: columns_sum, data: obj, originWidth: true }));
|
||||
};
|
||||
|
||||
_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, originWidth: true }));
|
||||
};
|
||||
|
||||
_this.getNodeItem = function (array, newArray) {
|
||||
array.forEach(function (da, i) {
|
||||
if (da.children) {
|
||||
|
@ -155,35 +56,54 @@ function sum(Table) {
|
|||
return type;
|
||||
};
|
||||
|
||||
_this.setFooterRender = function () {
|
||||
var columns = _this.props.columns;
|
||||
_this.addSumData = function () {
|
||||
var _this$props = _this.props,
|
||||
_this$props$data = _this$props.data,
|
||||
data = _this$props$data === undefined ? [] : _this$props$data,
|
||||
_this$props$columns = _this$props.columns,
|
||||
columns = _this$props$columns === undefined ? [] : _this$props$columns;
|
||||
|
||||
var sumdata = {},
|
||||
newColumns = [],
|
||||
newData = [];
|
||||
if (!Array.isArray(columns)) {
|
||||
console.log("data type is error !");return;
|
||||
console.log("columns type is error !");return;
|
||||
}
|
||||
var type = _this.getTableType();
|
||||
if (type == "tree") {
|
||||
return _this.currentTreeFooter();
|
||||
if (type == 'tree') {
|
||||
_this.getNodeItem(columns, newColumns);
|
||||
} else {
|
||||
return _this.currentFooter();
|
||||
newColumns = columns;
|
||||
}
|
||||
//返回一个新的数据
|
||||
newData = data.slice();
|
||||
newColumns.forEach(function (column, index) {
|
||||
sumdata[column.dataIndex] = "";
|
||||
if (column.sumCol) {
|
||||
var count = 0;
|
||||
data.forEach(function (da, i) {
|
||||
|
||||
var _num = parseInt(da[column.key]);
|
||||
//排查字段值为NAN情况
|
||||
if (_num === _num) {
|
||||
count += _num;
|
||||
}
|
||||
});
|
||||
sumdata[column.dataIndex] = count;
|
||||
}
|
||||
if (index == 0) {
|
||||
sumdata[column.dataIndex] = "合计 " + sumdata[column.dataIndex];
|
||||
}
|
||||
});
|
||||
|
||||
newData.push(sumdata);
|
||||
return newData;
|
||||
};
|
||||
|
||||
_this.tableType = "array";
|
||||
return _this;
|
||||
}
|
||||
|
||||
SumTable.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
|
||||
var columns = this.props.columns;
|
||||
|
||||
if (columns != nextProps.columns) {
|
||||
this.setFooterRender();
|
||||
}
|
||||
};
|
||||
|
||||
//合计数字列,并将计算所得数据存储到一个obj对象中
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前的表格类型。
|
||||
*
|
||||
|
@ -192,11 +112,9 @@ function sum(Table) {
|
|||
|
||||
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.setFooterRender
|
||||
// originWidth={true}
|
||||
showSum: true,
|
||||
data: this.addSumData()
|
||||
}));
|
||||
};
|
||||
|
||||
|
|
|
@ -8438,6 +8438,8 @@ ul {
|
|||
transition: background 0.3s ease; }
|
||||
.u-table th[colspan] {
|
||||
text-align: center; }
|
||||
.u-table th ::last-child {
|
||||
overflow: hidden; }
|
||||
.u-table td {
|
||||
border-bottom: 1px solid #e9e9e9; }
|
||||
.u-table tr {
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -11197,6 +11197,11 @@
|
|||
leafColumns = this.columnManager.leafColumns();
|
||||
}
|
||||
|
||||
//合计代码如果是最后一行并且有合计功能时,最后一行为合计列
|
||||
if (i == data.length - 1 && props.showSum) {
|
||||
className = className + ' sumrow';
|
||||
}
|
||||
|
||||
rst.push(_react2['default'].createElement(_TableRow2['default'], _extends({
|
||||
indent: indent,
|
||||
indentSize: props.indentSize,
|
||||
|
@ -11923,6 +11928,8 @@
|
|||
indentSize = _props7.indentSize,
|
||||
isHiddenExpandIcon = _props7.isHiddenExpandIcon,
|
||||
fixed = _props7.fixed;
|
||||
|
||||
var showSum = false;
|
||||
var className = this.props.className;
|
||||
|
||||
|
||||
|
@ -11930,6 +11937,10 @@
|
|||
className += ' ' + clsPrefix + '-hover';
|
||||
}
|
||||
|
||||
//判断是否为合计行
|
||||
if (className.indexOf('sumrow') > -1) {
|
||||
showSum = true;
|
||||
}
|
||||
var cells = [];
|
||||
|
||||
var expandIcon = _react2['default'].createElement(_ExpandIcon2['default'], {
|
||||
|
@ -11943,7 +11954,7 @@
|
|||
});
|
||||
|
||||
for (var i = 0; i < columns.length; i++) {
|
||||
if (expandIconAsCell && i === 0) {
|
||||
if (expandIconAsCell && i === 0 && !showSum) {
|
||||
cells.push(_react2['default'].createElement(
|
||||
'td',
|
||||
{
|
||||
|
@ -11953,7 +11964,7 @@
|
|||
expandIcon
|
||||
));
|
||||
}
|
||||
var isColumnHaveExpandIcon = expandIconAsCell || expandRowByClick ? false : i === expandIconColumnIndex;
|
||||
var isColumnHaveExpandIcon = expandIconAsCell || expandRowByClick || showSum ? false : i === expandIconColumnIndex;
|
||||
cells.push(_react2['default'].createElement(_TableCell2['default'], {
|
||||
clsPrefix: clsPrefix,
|
||||
record: record,
|
||||
|
@ -11963,6 +11974,7 @@
|
|||
column: columns[i],
|
||||
key: columns[i].key,
|
||||
fixed: fixed,
|
||||
showSum: showSum,
|
||||
expandIcon: isColumnHaveExpandIcon ? expandIcon : null
|
||||
}));
|
||||
}
|
||||
|
@ -12074,7 +12086,8 @@
|
|||
index = _props2.index,
|
||||
expandIcon = _props2.expandIcon,
|
||||
column = _props2.column,
|
||||
fixed = _props2.fixed;
|
||||
fixed = _props2.fixed,
|
||||
showSum = _props2.showSum;
|
||||
var dataIndex = column.dataIndex,
|
||||
render = column.render;
|
||||
var _column$className = column.className,
|
||||
|
@ -12086,7 +12099,7 @@
|
|||
var colSpan = void 0;
|
||||
var rowSpan = void 0;
|
||||
|
||||
if (render) {
|
||||
if (render && !showSum) {
|
||||
text = render(text, record, index);
|
||||
if (this.isInvalidRenderCellText(text)) {
|
||||
tdProps = text.props || {};
|
||||
|
@ -54355,13 +54368,6 @@
|
|||
|
||||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
|
||||
|
||||
//创建新列存放 “合计” 字段
|
||||
var columns2 = {
|
||||
title: "合计",
|
||||
key: "showSum",
|
||||
dataIndex: "showSum"
|
||||
};
|
||||
|
||||
function sum(Table) {
|
||||
return function (_React$Component) {
|
||||
_inherits(SumTable, _React$Component);
|
||||
|
@ -54373,98 +54379,6 @@
|
|||
//array , tree
|
||||
var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
|
||||
|
||||
_this.currentFooter = function () {
|
||||
var data_2 = _this.props.data;
|
||||
var columns_sum = _this.props.columns.concat();
|
||||
var sumCol_index = void 0,
|
||||
sumColIndexArr = [];
|
||||
//用一个对象存储合计数据,这里合计对象的属性对应每列字段
|
||||
for (var i = 0; i < columns_sum.length; i++) {
|
||||
if (columns_sum[i].sumCol) {
|
||||
sumColIndexArr.push(columns_sum[i].dataIndex);
|
||||
}
|
||||
}
|
||||
var obj = {};
|
||||
sumColIndexArr.forEach(function (sumCol_index) {
|
||||
|
||||
obj[sumCol_index] = 0;
|
||||
if (Array.isArray(data_2)) {
|
||||
for (var _i = 0; _i < data_2.length; _i++) {
|
||||
if (typeof data_2[_i][sumCol_index] == "number" || !isNaN(data_2[_i][sumCol_index])) {
|
||||
obj[sumCol_index] -= -data_2[_i][sumCol_index];
|
||||
} else {
|
||||
obj[sumCol_index] = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
obj.key = sumCol_index + "sumData";
|
||||
});
|
||||
obj.showSum = "合计";
|
||||
obj = [obj];
|
||||
//将设置的和用户传入的合并属性
|
||||
columns_sum[0] = _extends({}, columns_sum[0], columns2);
|
||||
//除去列为特殊渲染的,避免像a标签这种html代码写入到合计中
|
||||
columns_sum.map(function (item, index) {
|
||||
if (typeof item.render == "function" && !item.sumCol) {
|
||||
item.render = "";
|
||||
}
|
||||
return item;
|
||||
});
|
||||
return _react2["default"].createElement(Table, _extends({}, _this.props, { loading: false, footerScroll: true, showHeader: false, columns: columns_sum, data: obj, originWidth: true }));
|
||||
};
|
||||
|
||||
_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, originWidth: true }));
|
||||
};
|
||||
|
||||
_this.getNodeItem = function (array, newArray) {
|
||||
array.forEach(function (da, i) {
|
||||
if (da.children) {
|
||||
|
@ -54488,35 +54402,54 @@
|
|||
return type;
|
||||
};
|
||||
|
||||
_this.setFooterRender = function () {
|
||||
var columns = _this.props.columns;
|
||||
_this.addSumData = function () {
|
||||
var _this$props = _this.props,
|
||||
_this$props$data = _this$props.data,
|
||||
data = _this$props$data === undefined ? [] : _this$props$data,
|
||||
_this$props$columns = _this$props.columns,
|
||||
columns = _this$props$columns === undefined ? [] : _this$props$columns;
|
||||
|
||||
var sumdata = {},
|
||||
newColumns = [],
|
||||
newData = [];
|
||||
if (!Array.isArray(columns)) {
|
||||
console.log("data type is error !");return;
|
||||
console.log("columns type is error !");return;
|
||||
}
|
||||
var type = _this.getTableType();
|
||||
if (type == "tree") {
|
||||
return _this.currentTreeFooter();
|
||||
if (type == 'tree') {
|
||||
_this.getNodeItem(columns, newColumns);
|
||||
} else {
|
||||
return _this.currentFooter();
|
||||
newColumns = columns;
|
||||
}
|
||||
//返回一个新的数据
|
||||
newData = data.slice();
|
||||
newColumns.forEach(function (column, index) {
|
||||
sumdata[column.dataIndex] = "";
|
||||
if (column.sumCol) {
|
||||
var count = 0;
|
||||
data.forEach(function (da, i) {
|
||||
|
||||
var _num = parseInt(da[column.key]);
|
||||
//排查字段值为NAN情况
|
||||
if (_num === _num) {
|
||||
count += _num;
|
||||
}
|
||||
});
|
||||
sumdata[column.dataIndex] = count;
|
||||
}
|
||||
if (index == 0) {
|
||||
sumdata[column.dataIndex] = "合计 " + sumdata[column.dataIndex];
|
||||
}
|
||||
});
|
||||
|
||||
newData.push(sumdata);
|
||||
return newData;
|
||||
};
|
||||
|
||||
_this.tableType = "array";
|
||||
return _this;
|
||||
}
|
||||
|
||||
SumTable.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
|
||||
var columns = this.props.columns;
|
||||
|
||||
if (columns != nextProps.columns) {
|
||||
this.setFooterRender();
|
||||
}
|
||||
};
|
||||
|
||||
//合计数字列,并将计算所得数据存储到一个obj对象中
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前的表格类型。
|
||||
*
|
||||
|
@ -54525,11 +54458,9 @@
|
|||
|
||||
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.setFooterRender
|
||||
// originWidth={true}
|
||||
showSum: true,
|
||||
data: this.addSumData()
|
||||
}));
|
||||
};
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -494,7 +494,7 @@ class Table extends Component {
|
|||
if (expandedRowRender && typeof props.haveExpandIcon == 'function') {
|
||||
isHiddenExpandIcon = props.haveExpandIcon(record, i);
|
||||
}
|
||||
const className = rowClassName(record, i, indent);
|
||||
let className = rowClassName(record, i, indent);
|
||||
|
||||
const onHoverProps = {};
|
||||
if (this.columnManager.isAnyColumnsFixed()) {
|
||||
|
@ -524,6 +524,11 @@ class Table extends Component {
|
|||
}
|
||||
|
||||
|
||||
//合计代码如果是最后一行并且有合计功能时,最后一行为合计列
|
||||
if(i == data.length -1 && props.showSum){
|
||||
className = className + ' sumrow';
|
||||
}
|
||||
|
||||
rst.push(
|
||||
<TableRow
|
||||
indent={indent}
|
||||
|
|
|
@ -47,6 +47,9 @@ $checkbox-height:16px;
|
|||
&[colspan] {
|
||||
text-align: center;
|
||||
}
|
||||
::last-child{
|
||||
overflow: hidden;
|
||||
}
|
||||
}
|
||||
|
||||
td {
|
||||
|
|
|
@ -30,7 +30,7 @@ class TableCell extends Component{
|
|||
}
|
||||
render() {
|
||||
const { record, indentSize, clsPrefix, indent,
|
||||
index, expandIcon, column ,fixed} = this.props;
|
||||
index, expandIcon, column ,fixed,showSum} = this.props;
|
||||
const { dataIndex, render } = column;
|
||||
let {className = ''} = column;
|
||||
|
||||
|
@ -39,7 +39,7 @@ class TableCell extends Component{
|
|||
let colSpan;
|
||||
let rowSpan;
|
||||
|
||||
if (render) {
|
||||
if (render && !showSum) {
|
||||
text = render(text, record, index);
|
||||
if (this.isInvalidRenderCellText(text)) {
|
||||
tdProps = text.props || {};
|
||||
|
|
|
@ -125,13 +125,17 @@ class TableRow extends Component{
|
|||
expandIconColumnIndex, expandIconAsCell, expanded, expandRowByClick,
|
||||
expandable, onExpand, needIndentSpaced, indent, indentSize,isHiddenExpandIcon,fixed
|
||||
} = this.props;
|
||||
|
||||
let showSum = false;
|
||||
let { className } = this.props;
|
||||
|
||||
if (this.state.hovered) {
|
||||
className += ` ${clsPrefix}-hover`;
|
||||
}
|
||||
|
||||
|
||||
//判断是否为合计行
|
||||
if(className.indexOf('sumrow')>-1){
|
||||
showSum = true;
|
||||
}
|
||||
const cells = [];
|
||||
|
||||
const expandIcon = (
|
||||
|
@ -147,7 +151,7 @@ class TableRow extends Component{
|
|||
);
|
||||
|
||||
for (let i = 0; i < columns.length; i++) {
|
||||
if (expandIconAsCell && i === 0) {
|
||||
if (expandIconAsCell && i === 0 && !showSum ) {
|
||||
cells.push(
|
||||
<td
|
||||
className={`${clsPrefix}-expand-icon-cell`}
|
||||
|
@ -157,7 +161,7 @@ class TableRow extends Component{
|
|||
</td>
|
||||
);
|
||||
}
|
||||
const isColumnHaveExpandIcon = (expandIconAsCell || expandRowByClick)
|
||||
const isColumnHaveExpandIcon = (expandIconAsCell || expandRowByClick || showSum)
|
||||
? false : (i === expandIconColumnIndex);
|
||||
cells.push(
|
||||
<TableCell
|
||||
|
@ -169,6 +173,7 @@ class TableRow extends Component{
|
|||
column={columns[i]}
|
||||
key={columns[i].key}
|
||||
fixed= {fixed}
|
||||
showSum={showSum}
|
||||
expandIcon={(isColumnHaveExpandIcon) ? expandIcon : null}
|
||||
/>
|
||||
);
|
||||
|
|
125
src/lib/sum.js
125
src/lib/sum.js
|
@ -1,12 +1,5 @@
|
|||
import React from "react";
|
||||
|
||||
//创建新列存放 “合计” 字段
|
||||
let columns2 = {
|
||||
title: "合计",
|
||||
key: "showSum",
|
||||
dataIndex: "showSum"
|
||||
};
|
||||
|
||||
export default function sum(Table) {
|
||||
return class SumTable extends React.Component {
|
||||
//无状态
|
||||
|
@ -16,79 +9,6 @@ export default function sum(Table) {
|
|||
this.tableType = "array";
|
||||
}
|
||||
|
||||
componentWillReceiveProps(nextProps){
|
||||
const {columns} = this.props;
|
||||
if(columns != nextProps.columns){
|
||||
this.setFooterRender();
|
||||
}
|
||||
}
|
||||
|
||||
//合计数字列,并将计算所得数据存储到一个obj对象中
|
||||
currentFooter = () => {
|
||||
let data_2 = this.props.data;
|
||||
let columns_sum = this.props.columns.concat();
|
||||
let sumCol_index,sumColIndexArr=[];
|
||||
//用一个对象存储合计数据,这里合计对象的属性对应每列字段
|
||||
for (let i = 0; i < columns_sum.length; i++) {
|
||||
if (columns_sum[i].sumCol) {
|
||||
sumColIndexArr.push(columns_sum[i].dataIndex);
|
||||
}
|
||||
}
|
||||
let obj = {};
|
||||
sumColIndexArr.forEach(sumCol_index=>{
|
||||
|
||||
obj[sumCol_index] = 0;
|
||||
if (Array.isArray(data_2)) {
|
||||
for (let i = 0; i < data_2.length; i++) {
|
||||
if (
|
||||
typeof data_2[i][sumCol_index] == "number" ||
|
||||
!isNaN(data_2[i][sumCol_index])
|
||||
) {
|
||||
obj[sumCol_index] -= -data_2[i][sumCol_index];
|
||||
} else {
|
||||
obj[sumCol_index] = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
obj.key = sumCol_index+"sumData";
|
||||
|
||||
})
|
||||
obj.showSum = "合计";
|
||||
obj = [obj];
|
||||
//将设置的和用户传入的合并属性
|
||||
columns_sum[0] = Object.assign({}, columns_sum[0], columns2);
|
||||
//除去列为特殊渲染的,避免像a标签这种html代码写入到合计中
|
||||
columns_sum.map((item, index) => {
|
||||
if (typeof item.render == "function" && !item.sumCol) {
|
||||
item.render = "";
|
||||
}
|
||||
return item;
|
||||
});
|
||||
return <Table{...this.props} loading={false} footerScroll showHeader={false} columns={columns_sum} data={obj} originWidth={true}/>;
|
||||
};
|
||||
|
||||
currentTreeFooter =()=>{
|
||||
const {columns,data} = this.props;
|
||||
let _columns = [];
|
||||
this.getNodeItem(columns,_columns);
|
||||
let _countObj = {};
|
||||
for (let column of _columns) {
|
||||
if (typeof column.render == "function" && !column.sumCol) {
|
||||
column.render = "";
|
||||
}
|
||||
if(column.sumCol){
|
||||
let count = 0;
|
||||
data.forEach((da,i)=>{
|
||||
let _num = da[column.key];
|
||||
count += _num;
|
||||
})
|
||||
_countObj[column.key] = count;
|
||||
}
|
||||
}
|
||||
let _sumArray = [{key:"sumData",showSum:"合计",..._countObj}];
|
||||
columns[0] = Object.assign({}, columns[0], columns2);
|
||||
return <Table{...this.props} bordered={false} loading={false} footerScroll showHeader={false} columns={columns} data={_sumArray} originWidth={true}/>;
|
||||
}
|
||||
|
||||
getNodeItem =(array,newArray)=>{
|
||||
array.forEach((da,i)=>{
|
||||
|
@ -116,26 +36,51 @@ export default function sum(Table) {
|
|||
return type;
|
||||
}
|
||||
|
||||
setFooterRender=()=>{
|
||||
const {columns} = this.props;
|
||||
if (!Array.isArray(columns)) {console.log("data type is error !");return;}
|
||||
|
||||
|
||||
addSumData=()=>{
|
||||
let {data=[],columns=[]} = this.props;
|
||||
let sumdata = {},newColumns = [],newData = [];
|
||||
if (!Array.isArray(columns)) {console.log("columns type is error !");return;}
|
||||
let type = this.getTableType();
|
||||
if(type == "tree"){
|
||||
return this.currentTreeFooter();
|
||||
if(type == 'tree'){
|
||||
this.getNodeItem(columns,newColumns);
|
||||
}else{
|
||||
return this.currentFooter();
|
||||
newColumns = columns;
|
||||
}
|
||||
//返回一个新的数据
|
||||
newData = data.slice();
|
||||
newColumns.forEach((column,index)=>{
|
||||
sumdata[column.dataIndex] = "";
|
||||
if(column.sumCol){
|
||||
let count = 0;
|
||||
data.forEach((da,i)=>{
|
||||
|
||||
let _num = parseInt(da[column.key]);
|
||||
//排查字段值为NAN情况
|
||||
if(_num === _num){
|
||||
count += _num;
|
||||
}
|
||||
|
||||
})
|
||||
sumdata[column.dataIndex] = count;
|
||||
}
|
||||
if(index == 0){
|
||||
sumdata[column.dataIndex] = "合计 "+sumdata[column.dataIndex];
|
||||
}
|
||||
})
|
||||
|
||||
newData.push(sumdata);
|
||||
return newData;
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<Table
|
||||
{...this.props}
|
||||
footerScroll
|
||||
columns={this.props.columns}
|
||||
data={this.props.data}
|
||||
footer={this.setFooterRender}
|
||||
// originWidth={true}
|
||||
showSum={true}
|
||||
data={this.addSumData()}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue