fix:syncHove属性bug;feature:滚动加载场景下保存expandKeys

This commit is contained in:
wanghaoo 2019-02-25 14:40:28 +08:00
parent e1e99843f2
commit e5c300075e
14 changed files with 429 additions and 422 deletions

View File

@ -144,7 +144,8 @@ var defaultProps = {
syncHover: true, syncHover: true,
setRowHeight: function setRowHeight() {}, setRowHeight: function setRowHeight() {},
setRowParentIndex: function setRowParentIndex() {}, setRowParentIndex: function setRowParentIndex() {},
tabIndex: '0' tabIndex: '0',
noExpandedRowKeys: []
}; };
var Table = function (_Component) { var Table = function (_Component) {
@ -395,7 +396,7 @@ var Table = function (_Component) {
expandedRows.push(this.getRowKey(record, index)); expandedRows.push(this.getRowKey(record, index));
this.onExpandedRowsChange(expandedRows); this.onExpandedRowsChange(expandedRows);
} }
this.props.onExpand(expanded, record); this.props.onExpand(expanded, record, index);
}; };
Table.prototype.onRowDestroy = function onRowDestroy(record, rowIndex) { Table.prototype.onRowDestroy = function onRowDestroy(record, rowIndex) {
@ -653,7 +654,7 @@ var Table = function (_Component) {
var fixedColumnsBodyRowsHeight = this.state.fixedColumnsBodyRowsHeight; var fixedColumnsBodyRowsHeight = this.state.fixedColumnsBodyRowsHeight;
var rst = []; var rst = [];
var isHiddenExpandIcon = void 0;
var height = void 0; var height = void 0;
var rowClassName = props.rowClassName; var rowClassName = props.rowClassName;
var rowRef = props.rowRef; var rowRef = props.rowRef;
@ -672,32 +673,31 @@ var Table = function (_Component) {
var lazyCurrentIndex = props.lazyLoad && props.lazyLoad.startIndex ? props.lazyLoad.startIndex : 0; var lazyCurrentIndex = props.lazyLoad && props.lazyLoad.startIndex ? props.lazyLoad.startIndex : 0;
var lazyParentIndex = props.lazyLoad && props.lazyLoad.startParentIndex ? props.lazyLoad.startParentIndex : 0; var lazyParentIndex = props.lazyLoad && props.lazyLoad.startParentIndex ? props.lazyLoad.startParentIndex : 0;
for (var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
var isHiddenExpandIcon = void 0;
var record = data[i]; var record = data[i];
var key = this.getRowKey(record, i); var key = this.getRowKey(record, i);
var childrenColumn = record[childrenColumnName]; var childrenColumn = record[childrenColumnName];
var isRowExpanded = this.isRowExpanded(record, i); var isRowExpanded = this.isRowExpanded(record, i);
var expandedRowContent = void 0; var expandedRowContent = void 0;
var expandedContentHeight = 0; var expandedContentHeight = 0;
if (expandedRowRender && isRowExpanded) {
expandedRowContent = expandedRowRender(record, i, indent);
expandedContentHeight = parseInt(expandedRowContent.props && expandedRowContent.props.style && expandedRowContent.props.style.height ? expandedRowContent.props.style.height : 0);
}
//只有当使用expandedRowRender参数的时候才去识别isHiddenExpandIcon隐藏行展开的icon
if (expandedRowRender && typeof props.haveExpandIcon == 'function') {
isHiddenExpandIcon = props.haveExpandIcon(record, i);
}
var className = rowClassName(record, i, indent);
var onHoverProps = {};
onHoverProps.onHover = this.handleRowHover;
//fixedIndex一般是跟index是一个值的只有是树结构时会讲子节点的值也累计上 //fixedIndex一般是跟index是一个值的只有是树结构时会讲子节点的值也累计上
var fixedIndex = i; var fixedIndex = i;
//判断是否是tree结构 //判断是否是tree结构
if (this.treeType) { if (this.treeType) {
fixedIndex = this.treeRowIndex; fixedIndex = this.treeRowIndex;
} }
if (expandedRowRender && isRowExpanded) {
expandedRowContent = expandedRowRender(record, fixedIndex + lazyCurrentIndex, indent);
expandedContentHeight = parseInt(expandedRowContent.props && expandedRowContent.props.style && expandedRowContent.props.style.height ? expandedRowContent.props.style.height : 0);
}
//只有当使用expandedRowRender参数的时候才去识别isHiddenExpandIcon隐藏行展开的icon
if (expandedRowRender && typeof props.haveExpandIcon == 'function') {
isHiddenExpandIcon = props.haveExpandIcon(record, i);
}
var onHoverProps = {};
onHoverProps.onHover = this.handleRowHover;
if (props.height) { if (props.height) {
height = props.height; height = props.height;
@ -713,6 +713,7 @@ var Table = function (_Component) {
} else { } else {
leafColumns = this.columnManager.leafColumns(); leafColumns = this.columnManager.leafColumns();
} }
var className = rowClassName(record, fixedIndex + lazyCurrentIndex, indent);
//合计代码如果是最后一行并且有合计功能时,最后一行为合计列 //合计代码如果是最后一行并且有合计功能时,最后一行为合计列
if (i == data.length - 1 && props.showSum) { if (i == data.length - 1 && props.showSum) {
@ -728,6 +729,11 @@ var Table = function (_Component) {
if (rootIndex == -1) { if (rootIndex == -1) {
index = i + lazyParentIndex; index = i + lazyParentIndex;
} }
var noexpandable = void 0;
if (props.noExpandedRowKeys.indexOf(key) >= 0) {
noexpandable = true;
isHiddenExpandIcon = true;
}
rst.push(_react2["default"].createElement(_TableRow2["default"], _extends({ rst.push(_react2["default"].createElement(_TableRow2["default"], _extends({
indent: indent, indent: indent,
indentSize: props.indentSize, indentSize: props.indentSize,
@ -740,7 +746,7 @@ var Table = function (_Component) {
visible: visible, visible: visible,
expandRowByClick: expandRowByClick, expandRowByClick: expandRowByClick,
onExpand: this.onExpanded, onExpand: this.onExpanded,
expandable: childrenColumn || expandedRowRender, expandable: !noexpandable && (childrenColumn || expandedRowRender),
expanded: isRowExpanded, expanded: isRowExpanded,
clsPrefix: props.clsPrefix + '-row', clsPrefix: props.clsPrefix + '-row',
childrenColumnName: childrenColumnName, childrenColumnName: childrenColumnName,
@ -761,7 +767,8 @@ var Table = function (_Component) {
setRowParentIndex: props.setRowParentIndex, setRowParentIndex: props.setRowParentIndex,
treeType: childrenColumn || this.treeType ? true : false, treeType: childrenColumn || this.treeType ? true : false,
fixedIndex: fixedIndex + lazyCurrentIndex, fixedIndex: fixedIndex + lazyCurrentIndex,
rootIndex: rootIndex rootIndex: rootIndex,
syncHover: props.syncHover
}))); })));
this.treeRowIndex++; this.treeRowIndex++;
var subVisible = visible && isRowExpanded; var subVisible = visible && isRowExpanded;

View File

@ -175,13 +175,14 @@ var TableRow = function (_Component) {
expandable = _props5.expandable, expandable = _props5.expandable,
expandRowByClick = _props5.expandRowByClick, expandRowByClick = _props5.expandRowByClick,
expanded = _props5.expanded, expanded = _props5.expanded,
onExpand = _props5.onExpand; onExpand = _props5.onExpand,
fixedIndex = _props5.fixedIndex;
if (expandable && expandRowByClick) { if (expandable && expandRowByClick) {
onExpand(!expanded, record, index, event); onExpand(!expanded, record, fixedIndex, event);
} }
this.set(function (e) { this.set(function (e) {
onRowClick(record, index, event); onRowClick(record, fixedIndex, event);
}); });
}; };
@ -189,19 +190,23 @@ var TableRow = function (_Component) {
var _props6 = this.props, var _props6 = this.props,
record = _props6.record, record = _props6.record,
index = _props6.index, index = _props6.index,
onRowDoubleClick = _props6.onRowDoubleClick; onRowDoubleClick = _props6.onRowDoubleClick,
fixedIndex = _props6.fixedIndex;
this.clear(); this.clear();
onRowDoubleClick(record, index, event); onRowDoubleClick(record, fixedIndex, event);
}; };
TableRow.prototype.onMouseEnter = function onMouseEnter(e) { TableRow.prototype.onMouseEnter = function onMouseEnter(e) {
var _props7 = this.props, var _props7 = this.props,
onHover = _props7.onHover, onHover = _props7.onHover,
hoverKey = _props7.hoverKey, hoverKey = _props7.hoverKey,
fixedIndex = _props7.fixedIndex; fixedIndex = _props7.fixedIndex,
syncHover = _props7.syncHover;
this.setState({ hovered: true }); if (!syncHover) {
this.setState({ hovered: true });
}
onHover(true, hoverKey, e, fixedIndex); onHover(true, hoverKey, e, fixedIndex);
}; };
@ -209,9 +214,12 @@ var TableRow = function (_Component) {
var _props8 = this.props, var _props8 = this.props,
onHover = _props8.onHover, onHover = _props8.onHover,
hoverKey = _props8.hoverKey, hoverKey = _props8.hoverKey,
fixedIndex = _props8.fixedIndex; fixedIndex = _props8.fixedIndex,
syncHover = _props8.syncHover;
this.setState({ hovered: false }); if (!syncHover) {
this.setState({ hovered: false });
}
onHover(false, hoverKey, e, fixedIndex); onHover(false, hoverKey, e, fixedIndex);
}; };

View File

@ -63,6 +63,7 @@ function bigData(Table) {
_this2.endIndex = _this2.currentIndex + _this2.loadCount; //数据结束位置 _this2.endIndex = _this2.currentIndex + _this2.loadCount; //数据结束位置
_this2.setRowHeight = _this2.setRowHeight.bind(_this2); _this2.setRowHeight = _this2.setRowHeight.bind(_this2);
_this2.setRowParentIndex = _this2.setRowParentIndex.bind(_this2); _this2.setRowParentIndex = _this2.setRowParentIndex.bind(_this2);
_this2.expandedRowKeys = [];
return _this2; return _this2;
} }
@ -269,6 +270,7 @@ function bigData(Table) {
var endIndex = this.endIndex, var endIndex = this.endIndex,
startIndex = this.startIndex; startIndex = this.startIndex;
var expandedRowKeys = this.props.expandedRowKeys ? this.props.expandedRowKeys : this.expandedRowKeys;
if (startIndex < 0) { if (startIndex < 0) {
startIndex = 0; startIndex = 0;
} }
@ -299,7 +301,7 @@ function bigData(Table) {
lazyLoad.preHeight = this.getSumHeight(0, startIndex); lazyLoad.preHeight = this.getSumHeight(0, startIndex);
lazyLoad.sufHeight = this.getSumHeight(endIndex, data.length); lazyLoad.sufHeight = this.getSumHeight(endIndex, data.length);
} }
// console.log('*******ScrollTop*****'+scrollTop); // console.log('*******expandedRowKeys*****'+expandedRowKeys);
return _react2["default"].createElement(Table, _extends({}, this.props, { return _react2["default"].createElement(Table, _extends({}, this.props, {
data: data.slice(startIndex, endIndex), data: data.slice(startIndex, endIndex),
lazyLoad: lazyLoad, lazyLoad: lazyLoad,
@ -308,7 +310,8 @@ function bigData(Table) {
setRowHeight: this.setRowHeight, setRowHeight: this.setRowHeight,
setRowParentIndex: this.setRowParentIndex, setRowParentIndex: this.setRowParentIndex,
onExpand: this.onExpand, onExpand: this.onExpand,
onExpandedRowsChange: this.onExpandedRowsChange onExpandedRowsChange: this.onExpandedRowsChange,
expandedRowKeys: expandedRowKeys
// className={'lazy-table'} // className={'lazy-table'}
})); }));
}; };
@ -372,30 +375,6 @@ function bigData(Table) {
_this.scrollTop = nextScrollTop; _this.scrollTop = nextScrollTop;
var viewHeight = parseInt(scroll.y); var viewHeight = parseInt(scroll.y);
_this.treeType = treeType; _this.treeType = treeType;
// let index = currentIndex;//记录下次当前位置
// let temp = currentIndex ?nextScrollTop - currentScrollTop:nextScrollTop;
// const isOrder = temp > 0 ?true:false;//true为向下滚动、false为向上滚动
// //根据scrollTop计算下次当前索引的位置
// if(isOrder){
// while (temp > 0) {
// temp -= this.cachedRowHeight[index] || rowHeight
// if(temp > 0){
// index += 1
// //保存当前index对应的scrollTop
// this.currentScrollTop += this.cachedRowHeight[index]|| rowHeight;
// }
// }
// }else{
// while(temp < 0){
// temp += this.cachedRowHeight[index] || rowHeight
// if(temp < 0){
// index -= 1
// this.currentScrollTop -= this.cachedRowHeight[index]|| rowHeight;
// }
// }
// }
var index = 0; var index = 0;
var temp = nextScrollTop; var temp = nextScrollTop;
var currentKey = void 0; var currentKey = void 0;
@ -454,17 +433,6 @@ function bigData(Table) {
if (rowsInView + index > endIndex - rowDiff && isOrder) { if (rowsInView + index > endIndex - rowDiff && isOrder) {
startIndex = index - loadBuffer > 0 ? index - loadBuffer : 0; startIndex = index - loadBuffer > 0 ? index - loadBuffer : 0;
endIndex = startIndex + loadCount; endIndex = startIndex + loadCount;
//树状结构则根据当前的节点重新计算startIndex和endIndex
// if(treeType){
// const currentParentIndex = _this.cachedRowParentIndex[index];
// startIndex = currentParentIndex - loadBuffer>0?currentParentIndex - loadBuffer:0;
// endIndex = startIndex + loadCount;
// // console.log(endIndex,"endIndex的parentIndex",parentEndIndex);
// // endIndex = parentEndIndex +1
// }else{
// startIndex = index - loadBuffer>0?index - loadBuffer:0;
// endIndex = startIndex + loadCount;
// }
if (endIndex > data.length) { if (endIndex > data.length) {
endIndex = data.length; endIndex = data.length;
} }
@ -473,12 +441,6 @@ function bigData(Table) {
_this4.endIndex = endIndex; _this4.endIndex = endIndex;
_this4.setState({ needRender: !needRender }); _this4.setState({ needRender: !needRender });
} }
// console.log(
// "===================",
// "**index**" + index,
// " **startIndex**" + this.startIndex,
// "**endIndex**" + this.endIndex
// );
} }
// 向上滚动当前的index是否已经加载currentIndex若干上临界值小于startIndex则重新渲染 // 向上滚动当前的index是否已经加载currentIndex若干上临界值小于startIndex则重新渲染
if (!isOrder && index < startIndex + rowDiff) { if (!isOrder && index < startIndex + rowDiff) {
@ -501,22 +463,48 @@ function bigData(Table) {
} }
}; };
this.onExpand = function (expandState, record) { this.onExpand = function (expandState, record, index) {
var _this = _this4; var _this = _this4;
var _this$expandedRowKeys = _this.expandedRowKeys,
expandedRowKeys = _this$expandedRowKeys === undefined ? [] : _this$expandedRowKeys;
var needRender = _this.state.needRender;
var rowKey = _this.getRowKey(record, index);
// 记录展开子表行的key
// 展开 // 展开
if (expandState) { if (record.children) {
record.children && record.children.forEach(function (item, index) { if (expandState) {
_this.expandChildRowKeys.push(_this.getRowKey(item, index)); record.children.forEach(function (item, index) {
}); _this.expandChildRowKeys.push(rowKey);
} else { });
// 收起 } else {
record.children && record.children.forEach(function (item, index) { // 收起
_this.expandChildRowKeys.splice(_this.expandChildRowKeys.findIndex(function (fitem) { record.children.forEach(function (item, index) {
return fitem.key === item.key; _this.expandChildRowKeys.splice(_this.expandChildRowKeys.findIndex(function (fitem) {
}), 1); return fitem.key === item.key;
}); }), 1);
});
}
}
//滚动加载expandedRowKeys自己维护否则有展开不全的问题
if (!_this.props.expandedRowKeys) {
if (expandState) {
expandedRowKeys.push(rowKey);
} else {
var _index = -1;
expandedRowKeys.forEach(function (r, i) {
if (r === rowKey) {
_index = i;
}
});
if (_index !== -1) {
expandedRowKeys.splice(_index, 1);
_this4.setState({ needRender: !needRender });
}
}
} }
// expandState为true时记录下
_this.props.onExpand(expandState, record); _this.props.onExpand(expandState, record);
}; };
}, _temp; }, _temp;

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
Object.defineProperty(exports, "__esModule", { Object.defineProperty(exports, "__esModule", {
value: true value: true
}); });
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
@ -36,137 +36,137 @@ var cloneDeep = require('lodash.clonedeep');
function dragColumn(Table) { function dragColumn(Table) {
return function (_Component) { return function (_Component) {
_inherits(DragColumn, _Component); _inherits(DragColumn, _Component);
function DragColumn(props) { function DragColumn(props) {
_classCallCheck(this, DragColumn); _classCallCheck(this, DragColumn);
var _this = _possibleConstructorReturn(this, _Component.call(this, props)); var _this = _possibleConstructorReturn(this, _Component.call(this, props));
_this.setColumOrderByIndex = function (_column) { _this.setColumOrderByIndex = function (_column) {
_column.forEach(function (da, i) { _column.forEach(function (da, i) {
da.dragIndex = i; da.dragIndex = i;
da.drgHover = false; da.drgHover = false;
});
return _column;
};
_this.onDrop = function (event, data) {
var dragSource = data.dragSource,
dragTarg = data.dragTarg;
var columns = _this.state.columns;
var sourceIndex = -1,
targetIndex = -1;
sourceIndex = columns.findIndex(function (da, i) {
return da.key == dragSource.key;
});
targetIndex = columns.findIndex(function (da, i) {
return da.key == dragTarg.key;
});
// for (let index = 0; index < columns.length; index++) {
// const da = columns[index];
// if(da.key === dragSource.key){
// columns[index] = dragTargColum;
// }
// if(da.key === dragTarg.key){
// columns[index] = dragSourceColum;
// }
// }
// 向前移动
if (targetIndex < sourceIndex) {
targetIndex = targetIndex + 1;
}
columns.splice(targetIndex, 0, columns.splice(sourceIndex, 1)[0]);
_this.setState({
columns: cloneDeep(columns)
});
if (_this.props.onDrop) {
_this.props.onDrop(event, data, columns);
}
};
_this.getTarget = function (evt) {
return evt.target || evt.srcElement;
};
_this.state = {
columns: _this.setColumOrderByIndex(props.columns)
};
return _this;
}
DragColumn.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
if (nextProps.columns != this.props.columns) {
this.setState({
columns: this.setColumOrderByIndex(nextProps.columns)
});
}
};
DragColumn.prototype.cloneDeep = function cloneDeep(obj) {
if ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object' || Object.keys(obj).length === 0) {
return obj;
}
var resultData = {};
return this.recursion(obj, resultData);
};
DragColumn.prototype.recursion = function (_recursion) {
function recursion(_x) {
return _recursion.apply(this, arguments);
}
recursion.toString = function () {
return _recursion.toString();
};
return recursion;
}(function (obj) {
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
for (key in obj) {
if (_typeof(obj[key]) == 'object' && Object.keys(obj[key].length > 0)) {
data[key] = recursion(obj[key]);
} else {
data[key] = obj[key];
}
}
return data;
}); });
return _column;
};
DragColumn.prototype.render = function render() { _this.onDrop = function (event, data) {
var _props = this.props, var dragSource = data.dragSource,
data = _props.data, dragTarg = data.dragTarg;
dragborder = _props.dragborder, var columns = _this.state.columns;
draggable = _props.draggable,
className = _props.className,
columns = _props.columns,
onDragStart = _props.onDragStart,
onDragEnter = _props.onDragEnter,
onDragOver = _props.onDragOver,
onDrop = _props.onDrop,
others = _objectWithoutProperties(_props, ['data', 'dragborder', 'draggable', 'className', 'columns', 'onDragStart', 'onDragEnter', 'onDragOver', 'onDrop']);
var key = new Date().getTime(); var sourceIndex = -1,
return _react2["default"].createElement(Table, _extends({}, others, { targetIndex = -1;
columns: this.state.columns,
data: data,
className: className + ' u-table-drag-border',
onDragStart: this.onDragStart,
onDragOver: this.onDragOver,
onDrop: this.onDrop,
onDragEnter: this.onDragEnter,
draggable: draggable,
dragborder: dragborder
// dragborder={false}
, dragborderKey: key
}));
};
return DragColumn; sourceIndex = columns.findIndex(function (da, i) {
}(_react.Component); return da.key == dragSource.key;
});
targetIndex = columns.findIndex(function (da, i) {
return da.key == dragTarg.key;
});
// for (let index = 0; index < columns.length; index++) {
// const da = columns[index];
// if(da.key === dragSource.key){
// columns[index] = dragTargColum;
// }
// if(da.key === dragTarg.key){
// columns[index] = dragSourceColum;
// }
// }
// 向前移动
if (targetIndex < sourceIndex) {
targetIndex = targetIndex + 1;
}
columns.splice(targetIndex, 0, columns.splice(sourceIndex, 1)[0]);
_this.setState({
columns: cloneDeep(columns)
});
if (_this.props.onDrop) {
_this.props.onDrop(event, data, columns);
}
};
_this.getTarget = function (evt) {
return evt.target || evt.srcElement;
};
_this.state = {
columns: _this.setColumOrderByIndex(props.columns)
};
return _this;
}
DragColumn.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
if (nextProps.columns != this.props.columns) {
this.setState({
columns: this.setColumOrderByIndex(nextProps.columns)
});
}
};
DragColumn.prototype.cloneDeep = function cloneDeep(obj) {
if ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object' || Object.keys(obj).length === 0) {
return obj;
}
var resultData = {};
return this.recursion(obj, resultData);
};
DragColumn.prototype.recursion = function (_recursion) {
function recursion(_x) {
return _recursion.apply(this, arguments);
}
recursion.toString = function () {
return _recursion.toString();
};
return recursion;
}(function (obj) {
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
for (key in obj) {
if (_typeof(obj[key]) == 'object' && Object.keys(obj[key].length > 0)) {
data[key] = recursion(obj[key]);
} else {
data[key] = obj[key];
}
}
return data;
});
DragColumn.prototype.render = function render() {
var _props = this.props,
data = _props.data,
dragborder = _props.dragborder,
draggable = _props.draggable,
className = _props.className,
columns = _props.columns,
onDragStart = _props.onDragStart,
onDragEnter = _props.onDragEnter,
onDragOver = _props.onDragOver,
onDrop = _props.onDrop,
others = _objectWithoutProperties(_props, ['data', 'dragborder', 'draggable', 'className', 'columns', 'onDragStart', 'onDragEnter', 'onDragOver', 'onDrop']);
var key = new Date().getTime();
return _react2["default"].createElement(Table, _extends({}, others, {
columns: this.state.columns,
data: data,
className: className + ' u-table-drag-border',
onDragStart: this.onDragStart,
onDragOver: this.onDragOver,
onDrop: this.onDrop,
onDragEnter: this.onDragEnter,
draggable: draggable,
dragborder: dragborder
// dragborder={false}
, dragborderKey: key
}));
};
return DragColumn;
}(_react.Component);
} }
module.exports = exports['default']; module.exports = exports['default'];

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
Object.defineProperty(exports, "__esModule", { Object.defineProperty(exports, "__esModule", {
value: true value: true
}); });
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
@ -20,29 +20,29 @@ exports.ObjectAssign = ObjectAssign;
*/ */
function sortBy(arr, prop, desc) { function sortBy(arr, prop, desc) {
var props = [], var props = [],
ret = [], ret = [],
i = 0, i = 0,
len = arr.length; len = arr.length;
if (typeof prop == 'string') { if (typeof prop == 'string') {
for (; i < len; i++) { for (; i < len; i++) {
var oI = arr[i]; var oI = arr[i];
(props[i] = new String(oI && oI[prop] || ''))._obj = oI; (props[i] = new String(oI && oI[prop] || ''))._obj = oI;
}
} else if (typeof prop == 'function') {
for (; i < len; i++) {
var _oI = arr[i];
(props[i] = new String(_oI && prop(_oI) || ''))._obj = _oI;
}
} else {
throw '参数类型错误';
} }
} else if (typeof prop == 'function') { props.sort();
for (; i < len; i++) { for (i = 0; i < len; i++) {
var _oI = arr[i]; ret[i] = props[i]._obj;
(props[i] = new String(_oI && prop(_oI) || ''))._obj = _oI;
} }
} else { if (desc) ret.reverse();
throw '参数类型错误'; return ret;
}
props.sort();
for (i = 0; i < len; i++) {
ret[i] = props[i]._obj;
}
if (desc) ret.reverse();
return ret;
}; };
/** /**
@ -51,11 +51,11 @@ function sortBy(arr, prop, desc) {
* @param {} property * @param {} property
*/ */
function compare(property) { function compare(property) {
return function (a, b) { return function (a, b) {
var value1 = a[property]; var value1 = a[property];
var value2 = b[property]; var value2 = b[property];
return value1 - value2; return value1 - value2;
}; };
} }
/** /**
@ -63,17 +63,17 @@ function compare(property) {
* @param {*} obj 要拷贝的对象 * @param {*} obj 要拷贝的对象
*/ */
function ObjectAssign(obj) { function ObjectAssign(obj) {
var b = obj instanceof Array; var b = obj instanceof Array;
var tagObj = b ? [] : {}; var tagObj = b ? [] : {};
if (b) { if (b) {
//数组 //数组
obj.forEach(function (da) { obj.forEach(function (da) {
var _da = {}; var _da = {};
_extends(_da, da); _extends(_da, da);
tagObj.push(_da); tagObj.push(_da);
}); });
} else { } else {
_extends(tagObj, obj); _extends(tagObj, obj);
} }
return tagObj; return tagObj;
} }

6
dist/demo.css vendored
View File

@ -8626,10 +8626,8 @@ ul {
color: rgb(66,165,245); } color: rgb(66,165,245); }
.u-table td a:active { .u-table td a:active {
color: rgb(21,101,192); } color: rgb(21,101,192); }
.u-table tr:hover { .u-table tr:hover td .uf-eye {
background: rgb(227,242,253); } visibility: visible !important; }
.u-table tr:hover td .uf-eye {
visibility: visible !important; }
.u-table tr tr a { .u-table tr tr a {
color: rgb(30,136,229); } color: rgb(30,136,229); }
.u-table tr tr a:hover { .u-table tr tr a:hover {

2
dist/demo.css.map vendored

File diff suppressed because one or more lines are too long

173
dist/demo.js vendored

File diff suppressed because one or more lines are too long

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", "name": "bee-table",
"version": "1.6.41.beta.0", "version": "1.6.42.beta.0",
"description": "Table ui component for react", "description": "Table ui component for react",
"keywords": [ "keywords": [
"react", "react",

View File

@ -85,6 +85,7 @@ const defaultProps = {
setRowHeight:()=>{}, setRowHeight:()=>{},
setRowParentIndex:()=>{}, setRowParentIndex:()=>{},
tabIndex:'0', tabIndex:'0',
noExpandedRowKeys:[]
}; };
class Table extends Component { class Table extends Component {
@ -289,7 +290,7 @@ class Table extends Component {
expandedRows.push(this.getRowKey(record, index)); expandedRows.push(this.getRowKey(record, index));
this.onExpandedRowsChange(expandedRows); this.onExpandedRowsChange(expandedRows);
} }
this.props.onExpand(expanded, record); this.props.onExpand(expanded, record,index);
} }
onRowDestroy(record, rowIndex) { onRowDestroy(record, rowIndex) {
@ -515,7 +516,7 @@ class Table extends Component {
const expandRowByClick = props.expandRowByClick; const expandRowByClick = props.expandRowByClick;
const { fixedColumnsBodyRowsHeight } = this.state; const { fixedColumnsBodyRowsHeight } = this.state;
let rst = []; let rst = [];
let isHiddenExpandIcon;
let height; let height;
const rowClassName = props.rowClassName; const rowClassName = props.rowClassName;
const rowRef = props.rowRef; const rowRef = props.rowRef;
@ -534,32 +535,34 @@ class Table extends Component {
const lazyCurrentIndex = props.lazyLoad && props.lazyLoad.startIndex ?props.lazyLoad.startIndex :0; const lazyCurrentIndex = props.lazyLoad && props.lazyLoad.startIndex ?props.lazyLoad.startIndex :0;
const lazyParentIndex = props.lazyLoad && props.lazyLoad.startParentIndex ?props.lazyLoad.startParentIndex :0; const lazyParentIndex = props.lazyLoad && props.lazyLoad.startParentIndex ?props.lazyLoad.startParentIndex :0;
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
let isHiddenExpandIcon;
const record = data[i]; const record = data[i];
const key = this.getRowKey(record, i); const key = this.getRowKey(record, i);
const childrenColumn = record[childrenColumnName]; const childrenColumn = record[childrenColumnName];
const isRowExpanded = this.isRowExpanded(record, i); const isRowExpanded = this.isRowExpanded(record, i);
let expandedRowContent; let expandedRowContent;
let expandedContentHeight = 0; let expandedContentHeight = 0;
//fixedIndex一般是跟index是一个值的只有是树结构时会讲子节点的值也累计上
let fixedIndex = i;
//判断是否是tree结构
if (this.treeType) {
fixedIndex = this.treeRowIndex;
}
if (expandedRowRender && isRowExpanded) { if (expandedRowRender && isRowExpanded) {
expandedRowContent = expandedRowRender(record, i, indent); expandedRowContent = expandedRowRender(record, fixedIndex+lazyCurrentIndex, indent);
expandedContentHeight = parseInt(expandedRowContent.props && expandedRowContent.props.style && expandedRowContent.props.style.height?expandedRowContent.props.style.height:0); expandedContentHeight = parseInt(expandedRowContent.props && expandedRowContent.props.style && expandedRowContent.props.style.height?expandedRowContent.props.style.height:0);
} }
//只有当使用expandedRowRender参数的时候才去识别isHiddenExpandIcon隐藏行展开的icon //只有当使用expandedRowRender参数的时候才去识别isHiddenExpandIcon隐藏行展开的icon
if (expandedRowRender && typeof props.haveExpandIcon == 'function') { if (expandedRowRender && typeof props.haveExpandIcon == 'function') {
isHiddenExpandIcon = props.haveExpandIcon(record, i); isHiddenExpandIcon = props.haveExpandIcon(record, i);
} }
let className = rowClassName(record, i, indent);
const onHoverProps = {}; const onHoverProps = {};
onHoverProps.onHover = this.handleRowHover; onHoverProps.onHover = this.handleRowHover;
//fixedIndex一般是跟index是一个值的只有是树结构时会讲子节点的值也累计上
let fixedIndex = i;
//判断是否是tree结构
if (this.treeType) {
fixedIndex = this.treeRowIndex;
}
if (props.height) { if (props.height) {
height = props.height height = props.height
@ -575,7 +578,7 @@ class Table extends Component {
} else { } else {
leafColumns = this.columnManager.leafColumns(); leafColumns = this.columnManager.leafColumns();
} }
let className = rowClassName(record, fixedIndex+lazyCurrentIndex, indent);
//合计代码如果是最后一行并且有合计功能时,最后一行为合计列 //合计代码如果是最后一行并且有合计功能时,最后一行为合计列
if(i == data.length -1 && props.showSum){ if(i == data.length -1 && props.showSum){
@ -591,6 +594,11 @@ class Table extends Component {
if(rootIndex ==-1){ if(rootIndex ==-1){
index = i+lazyParentIndex index = i+lazyParentIndex
} }
let noexpandable;
if(props.noExpandedRowKeys.indexOf(key) >= 0){
noexpandable = true;
isHiddenExpandIcon = true;
}
rst.push( rst.push(
<TableRow <TableRow
indent={indent} indent={indent}
@ -604,7 +612,7 @@ class Table extends Component {
visible={visible} visible={visible}
expandRowByClick={expandRowByClick} expandRowByClick={expandRowByClick}
onExpand={this.onExpanded} onExpand={this.onExpanded}
expandable={childrenColumn || expandedRowRender} expandable={!noexpandable && (childrenColumn || expandedRowRender)}
expanded={isRowExpanded} expanded={isRowExpanded}
clsPrefix={`${props.clsPrefix}-row`} clsPrefix={`${props.clsPrefix}-row`}
childrenColumnName={childrenColumnName} childrenColumnName={childrenColumnName}
@ -626,6 +634,7 @@ class Table extends Component {
treeType={childrenColumn||this.treeType?true:false} treeType={childrenColumn||this.treeType?true:false}
fixedIndex={fixedIndex+lazyCurrentIndex} fixedIndex={fixedIndex+lazyCurrentIndex}
rootIndex = {rootIndex} rootIndex = {rootIndex}
syncHover = {props.syncHover}
/> />
); );
this.treeRowIndex++; this.treeRowIndex++;

View File

@ -70,7 +70,7 @@ $checkbox-height:16px;
tr { tr {
// transition: all 0.3s ease; // transition: all 0.3s ease;
&:hover { &:hover {
background: $hover-bg-color-base; // background: $hover-bg-color-base;
td { td {
.uf-eye{ .uf-eye{
visibility: visible !important; visibility: visible !important;

View File

@ -112,31 +112,36 @@ class TableRow extends Component{
expandRowByClick, expandRowByClick,
expanded, expanded,
onExpand, onExpand,
fixedIndex
} = this.props; } = this.props;
if (expandable && expandRowByClick) { if (expandable && expandRowByClick) {
onExpand(!expanded, record, index,event); onExpand(!expanded, record, fixedIndex,event);
} }
this.set((e)=> { this.set((e)=> {
onRowClick(record, index, event); onRowClick(record, fixedIndex, event);
}); });
} }
onRowDoubleClick(event) { onRowDoubleClick(event) {
const { record, index, onRowDoubleClick } = this.props; const { record, index, onRowDoubleClick,fixedIndex } = this.props;
this.clear(); this.clear();
onRowDoubleClick(record, index, event); onRowDoubleClick(record, fixedIndex, event);
} }
onMouseEnter(e) { onMouseEnter(e) {
const { onHover, hoverKey,fixedIndex } = this.props; const { onHover, hoverKey,fixedIndex,syncHover } = this.props;
this.setState({ hovered: true }); if(syncHover){
this.setState({ hovered: true });
}
onHover(true, hoverKey,e,fixedIndex); onHover(true, hoverKey,e,fixedIndex);
} }
onMouseLeave(e) { onMouseLeave(e) {
const { onHover, hoverKey ,fixedIndex} = this.props; const { onHover, hoverKey ,fixedIndex,syncHover} = this.props;
this.setState({ hovered: false }); if(syncHover){
this.setState({ hovered: false });
}
onHover(false, hoverKey,e,fixedIndex); onHover(false, hoverKey,e,fixedIndex);
} }

View File

@ -42,6 +42,7 @@ export default function bigData(Table) {
this.endIndex = this.currentIndex + this.loadCount; //数据结束位置 this.endIndex = this.currentIndex + this.loadCount; //数据结束位置
this.setRowHeight = this.setRowHeight.bind(this); this.setRowHeight = this.setRowHeight.bind(this);
this.setRowParentIndex = this.setRowParentIndex.bind(this); this.setRowParentIndex = this.setRowParentIndex.bind(this);
this.expandedRowKeys = [];
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
const props = this.props; const props = this.props;
@ -227,30 +228,6 @@ export default function bigData(Table) {
_this.scrollTop = nextScrollTop; _this.scrollTop = nextScrollTop;
const viewHeight = parseInt(scroll.y); const viewHeight = parseInt(scroll.y);
_this.treeType = treeType; _this.treeType = treeType;
// let index = currentIndex;//记录下次当前位置
// let temp = currentIndex ?nextScrollTop - currentScrollTop:nextScrollTop;
// const isOrder = temp > 0 ?true:false;//true为向下滚动、false为向上滚动
// //根据scrollTop计算下次当前索引的位置
// if(isOrder){
// while (temp > 0) {
// temp -= this.cachedRowHeight[index] || rowHeight
// if(temp > 0){
// index += 1
// //保存当前index对应的scrollTop
// this.currentScrollTop += this.cachedRowHeight[index]|| rowHeight;
// }
// }
// }else{
// while(temp < 0){
// temp += this.cachedRowHeight[index] || rowHeight
// if(temp < 0){
// index -= 1
// this.currentScrollTop -= this.cachedRowHeight[index]|| rowHeight;
// }
// }
// }
let index = 0; let index = 0;
let temp = nextScrollTop; let temp = nextScrollTop;
let currentKey; let currentKey;
@ -319,17 +296,6 @@ export default function bigData(Table) {
if (rowsInView + index > endIndex - rowDiff && isOrder) { if (rowsInView + index > endIndex - rowDiff && isOrder) {
startIndex = index - loadBuffer > 0 ? index - loadBuffer : 0; startIndex = index - loadBuffer > 0 ? index - loadBuffer : 0;
endIndex = startIndex + loadCount; endIndex = startIndex + loadCount;
//树状结构则根据当前的节点重新计算startIndex和endIndex
// if(treeType){
// const currentParentIndex = _this.cachedRowParentIndex[index];
// startIndex = currentParentIndex - loadBuffer>0?currentParentIndex - loadBuffer:0;
// endIndex = startIndex + loadCount;
// // console.log(endIndex,"endIndex的parentIndex",parentEndIndex);
// // endIndex = parentEndIndex +1
// }else{
// startIndex = index - loadBuffer>0?index - loadBuffer:0;
// endIndex = startIndex + loadCount;
// }
if (endIndex > data.length) { if (endIndex > data.length) {
endIndex = data.length; endIndex = data.length;
} }
@ -338,12 +304,6 @@ export default function bigData(Table) {
this.endIndex = endIndex; this.endIndex = endIndex;
this.setState({ needRender: !needRender }); this.setState({ needRender: !needRender });
} }
// console.log(
// "===================",
// "**index**" + index,
// " **startIndex**" + this.startIndex,
// "**endIndex**" + this.endIndex
// );
} }
// 向上滚动当前的index是否已经加载currentIndex若干上临界值小于startIndex则重新渲染 // 向上滚动当前的index是否已经加载currentIndex若干上临界值小于startIndex则重新渲染
if (!isOrder && index < startIndex + rowDiff) { if (!isOrder && index < startIndex + rowDiff) {
@ -401,33 +361,61 @@ export default function bigData(Table) {
return parentIndex; return parentIndex;
} }
onExpand = (expandState, record) => { onExpand = (expandState, record,index) => {
const _this = this; const _this = this;
let {expandedRowKeys = []} = _this;
const {needRender} = _this.state;
const rowKey = _this.getRowKey(record, index);
// 记录展开子表行的key
// 展开 // 展开
if (expandState) { if( record.children){
record.children && if (expandState) {
record.children.forEach((item, index) => { record.children.forEach((item, index) => {
_this.expandChildRowKeys.push(_this.getRowKey(item, index)); _this.expandChildRowKeys.push(rowKey);
}); });
} else {
// 收起 } else {
record.children && // 收起
record.children.forEach((item, index) => { record.children.forEach((item, index) => {
_this.expandChildRowKeys.splice( _this.expandChildRowKeys.splice(
_this.expandChildRowKeys.findIndex( _this.expandChildRowKeys.findIndex(
fitem => fitem.key === item.key fitem => fitem.key === item.key
), ),
1 1
); );
}); });
}
}
//滚动加载expandedRowKeys自己维护否则有展开不全的问题
if(!_this.props.expandedRowKeys){
if(expandState){
expandedRowKeys.push(rowKey);
}else{
let index = -1;
expandedRowKeys.forEach((r, i) => {
if (r === rowKey) {
index = i;
}
});
if (index !== -1) {
expandedRowKeys.splice(index, 1);
this.setState({ needRender: !needRender });
}
}
} }
// expandState为true时记录下
_this.props.onExpand(expandState, record); _this.props.onExpand(expandState, record);
}; };
render() { render() {
const { data } = this.props; const { data } = this.props;
const { scrollTop } = this; const { scrollTop } = this;
let { endIndex, startIndex } = this; let { endIndex, startIndex } = this;
let expandedRowKeys = this.props.expandedRowKeys?this.props.expandedRowKeys: this.expandedRowKeys;
if(startIndex < 0){ if(startIndex < 0){
startIndex = 0; startIndex = 0;
} }
@ -466,7 +454,7 @@ export default function bigData(Table) {
lazyLoad.preHeight = this.getSumHeight(0, startIndex); lazyLoad.preHeight = this.getSumHeight(0, startIndex);
lazyLoad.sufHeight = this.getSumHeight(endIndex, data.length); lazyLoad.sufHeight = this.getSumHeight(endIndex, data.length);
} }
// console.log('*******ScrollTop*****'+scrollTop); // console.log('*******expandedRowKeys*****'+expandedRowKeys);
return ( return (
<Table <Table
{...this.props} {...this.props}
@ -478,6 +466,7 @@ export default function bigData(Table) {
setRowParentIndex={this.setRowParentIndex} setRowParentIndex={this.setRowParentIndex}
onExpand={this.onExpand} onExpand={this.onExpand}
onExpandedRowsChange={this.onExpandedRowsChange} onExpandedRowsChange={this.onExpandedRowsChange}
expandedRowKeys={expandedRowKeys}
// className={'lazy-table'} // className={'lazy-table'}
/> />
); );