2018-12-18 13:59:48 +08:00
|
|
|
|
import React, { Component } from "react";
|
2019-01-03 16:21:24 +08:00
|
|
|
|
import PropTypes from "prop-types";
|
2019-08-31 11:32:29 +08:00
|
|
|
|
import { convertListToTree } from "./utils";
|
2019-01-25 11:17:52 +08:00
|
|
|
|
const defaultHeight = 30;
|
2019-07-02 16:35:33 +08:00
|
|
|
|
const rowDiff = 2; //行差值
|
2019-01-03 16:21:24 +08:00
|
|
|
|
let treeTypeIndex = 0;
|
2018-12-18 13:59:48 +08:00
|
|
|
|
export default function bigData(Table) {
|
|
|
|
|
return class BigData extends Component {
|
2019-01-03 16:21:24 +08:00
|
|
|
|
static defaultProps = {
|
|
|
|
|
data: [],
|
|
|
|
|
loadBuffer: 5,
|
2019-01-16 13:57:31 +08:00
|
|
|
|
rowKey: "key",
|
|
|
|
|
onExpand() {},
|
2019-01-22 11:03:53 +08:00
|
|
|
|
scroll: {},
|
2019-01-25 11:17:52 +08:00
|
|
|
|
currentIndex:-1,
|
|
|
|
|
isTree:false
|
2018-12-18 13:59:48 +08:00
|
|
|
|
};
|
2018-12-20 19:37:07 +08:00
|
|
|
|
static propTypes = {
|
2019-01-03 16:21:24 +08:00
|
|
|
|
loadBuffer: PropTypes.number
|
|
|
|
|
};
|
2018-12-18 13:59:48 +08:00
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
scrollLeft: 0,
|
|
|
|
|
scrollTop: 0
|
|
|
|
|
};
|
2019-01-03 16:21:24 +08:00
|
|
|
|
const rowHeight = this.props.height ? this.props.height : defaultHeight;
|
2018-12-18 18:14:09 +08:00
|
|
|
|
//默认显示25条,rowsInView根据定高算的。在非固定高下,这个只是一个大概的值。
|
2018-12-25 13:34:37 +08:00
|
|
|
|
const scrollY = this.props.scroll.y ? parseInt(this.props.scroll.y) : 0;
|
2019-02-20 13:27:33 +08:00
|
|
|
|
this.rowsInView = scrollY ? Math.floor(scrollY / rowHeight) : 20;
|
2018-12-20 18:07:30 +08:00
|
|
|
|
this.currentIndex = 0;
|
2019-01-03 16:21:24 +08:00
|
|
|
|
this.loadCount = props.loadBuffer
|
|
|
|
|
? this.rowsInView + props.loadBuffer * 2
|
|
|
|
|
: 26; //一次加载多少数据
|
|
|
|
|
this.cachedRowHeight = []; //缓存每行的高度
|
2019-01-16 13:57:31 +08:00
|
|
|
|
this.cachedRowParentIndex = [];
|
|
|
|
|
this.expandChildRowKeys = [];
|
|
|
|
|
this.firstLevelKey = [];
|
|
|
|
|
this.keys = [];
|
2018-12-18 13:59:48 +08:00
|
|
|
|
this.lastScrollTop = 0;
|
2018-12-20 19:37:07 +08:00
|
|
|
|
this.currentScrollTop = 0;
|
2019-01-03 16:21:24 +08:00
|
|
|
|
this.startIndex = this.currentIndex; //数据开始位置
|
|
|
|
|
this.endIndex = this.currentIndex + this.loadCount; //数据结束位置
|
2018-12-18 18:14:09 +08:00
|
|
|
|
this.setRowHeight = this.setRowHeight.bind(this);
|
2019-01-03 16:21:24 +08:00
|
|
|
|
this.setRowParentIndex = this.setRowParentIndex.bind(this);
|
2019-11-06 15:03:26 +08:00
|
|
|
|
this.expandedRowKeys = props.expandedRowKeys || [];
|
2019-08-31 11:32:29 +08:00
|
|
|
|
this.flatTreeKeysMap = {}; //树表,扁平结构数据的 Map 映射,方便获取各节点信息
|
|
|
|
|
this.flatTreeData = []; //深度遍历处理后的data数组
|
|
|
|
|
this.treeData = []; //树表的data数据
|
2019-01-03 16:21:24 +08:00
|
|
|
|
}
|
2019-01-16 13:57:31 +08:00
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
|
const props = this.props;
|
2021-04-28 10:44:43 +08:00
|
|
|
|
const {currentIndex, expandedRowKeys:newExpandedKeys} = nextProps;
|
2021-04-30 14:32:24 +08:00
|
|
|
|
const newData = nextProps.data
|
2019-12-26 18:28:11 +08:00
|
|
|
|
const _this = this,dataLen = newData.length;
|
2019-01-16 13:57:31 +08:00
|
|
|
|
if (nextProps.scroll.y !== props.scroll.y) {
|
|
|
|
|
const rowHeight = nextProps.height ? nextProps.height : defaultHeight;
|
|
|
|
|
const scrollY = nextProps.scroll.y ? parseInt(nextProps.scroll.y) : 0;
|
2019-02-20 13:27:33 +08:00
|
|
|
|
_this.rowsInView = scrollY ? Math.floor(scrollY / rowHeight) : 20;
|
2019-01-22 11:03:53 +08:00
|
|
|
|
_this.loadCount = props.loadBuffer
|
|
|
|
|
? _this.rowsInView + props.loadBuffer * 2
|
2019-01-16 13:57:31 +08:00
|
|
|
|
: 26; //一次加载多少数据
|
2019-01-22 11:03:53 +08:00
|
|
|
|
_this.currentIndex = 0;
|
|
|
|
|
_this.startIndex = _this.currentIndex; //数据开始位置
|
|
|
|
|
_this.endIndex = _this.currentIndex + _this.loadCount; //数据结束位置
|
2019-02-19 09:30:18 +08:00
|
|
|
|
|
2019-01-16 13:57:31 +08:00
|
|
|
|
}
|
2019-12-05 10:04:21 +08:00
|
|
|
|
if('data' in nextProps){
|
2019-12-26 18:28:11 +08:00
|
|
|
|
const isTreeType = nextProps.isTree ? true : _this.checkIsTreeType(newData);
|
2019-09-11 21:08:18 +08:00
|
|
|
|
_this.treeType = isTreeType;
|
2019-08-23 15:51:32 +08:00
|
|
|
|
//fix: 滚动加载场景中,数据动态改变下占位计算错误的问题(26 Jun)
|
2019-12-26 18:28:11 +08:00
|
|
|
|
if (newData.toString() !== props.data.toString()) {
|
2019-12-05 10:04:21 +08:00
|
|
|
|
_this.cachedRowHeight = []; //缓存每行的高度
|
|
|
|
|
_this.cachedRowParentIndex = [];
|
2019-12-26 18:28:11 +08:00
|
|
|
|
_this.computeCachedRowParentIndex(newData);
|
2020-04-10 12:54:37 +08:00
|
|
|
|
// fix:切换数据源,startIndex、endIndex错误
|
2020-10-09 13:34:44 +08:00
|
|
|
|
if(_this.scrollTop <= 0) { // 增加scrollTop 判断,ncc场景下滚动条不在最上层, 会出现空白,因为重置了currentIndex没有重置滚动条
|
|
|
|
|
_this.currentIndex = 0;
|
|
|
|
|
_this.startIndex = _this.currentIndex; //数据开始位置
|
|
|
|
|
_this.endIndex = _this.currentIndex + _this.loadCount;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 10:04:21 +08:00
|
|
|
|
}
|
2019-09-11 21:08:18 +08:00
|
|
|
|
_this.treeData = [];
|
|
|
|
|
_this.flatTreeData = [];
|
2019-12-26 18:28:11 +08:00
|
|
|
|
if(newData.length>0){
|
2019-02-19 16:08:44 +08:00
|
|
|
|
_this.endIndex = _this.currentIndex - nextProps.loadBuffer + _this.loadCount; //数据结束位置
|
2019-02-19 09:30:18 +08:00
|
|
|
|
}
|
2019-09-11 21:08:18 +08:00
|
|
|
|
if(isTreeType){
|
2019-12-26 18:28:11 +08:00
|
|
|
|
_this.getTreeData(newExpandedKeys, newData);
|
2019-09-11 21:08:18 +08:00
|
|
|
|
}
|
2019-01-22 11:03:53 +08:00
|
|
|
|
}
|
|
|
|
|
//如果传currentIndex,会判断该条数据是否在可视区域,如果没有的话,则重新计算startIndex和endIndex
|
2020-11-20 15:15:44 +08:00
|
|
|
|
if(currentIndex != -1 && currentIndex !== this.currentIndex){
|
2019-01-22 11:03:53 +08:00
|
|
|
|
_this.setStartAndEndIndex(currentIndex,dataLen);
|
2019-01-16 13:57:31 +08:00
|
|
|
|
}
|
2019-01-03 16:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 11:32:29 +08:00
|
|
|
|
componentWillMount() {
|
|
|
|
|
const { data,isTree } = this.props;
|
|
|
|
|
const isTreeType = isTree?true:this.checkIsTreeType();
|
|
|
|
|
//设置data中每个元素的parentIndex
|
2019-01-16 13:57:31 +08:00
|
|
|
|
this.computeCachedRowParentIndex(data);
|
2019-08-31 11:32:29 +08:00
|
|
|
|
//如果是树表,递归data
|
|
|
|
|
if(isTreeType){
|
|
|
|
|
this.treeType = isTreeType;
|
2019-09-11 21:08:18 +08:00
|
|
|
|
this.getTreeData();
|
2019-08-31 11:32:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 21:08:18 +08:00
|
|
|
|
/**
|
|
|
|
|
* 如果是树形表,需要对传入的 data 进行处理
|
2019-12-26 18:28:11 +08:00
|
|
|
|
* @param expandedKeys: nextProps 中传入的新 expandedRowKeys 属性值
|
|
|
|
|
* @param newData: nextProps 中传入的新 data 属性值
|
2019-09-11 21:08:18 +08:00
|
|
|
|
*/
|
2019-12-26 18:28:11 +08:00
|
|
|
|
getTreeData = (expandedKeys, newData) => {
|
2019-12-05 16:27:38 +08:00
|
|
|
|
let { startIndex, endIndex } = this;
|
2019-12-26 18:28:11 +08:00
|
|
|
|
const data = newData ? newData : this.props.data;
|
2019-12-05 16:27:38 +08:00
|
|
|
|
this.cacheExpandedKeys = expandedKeys && new Set(expandedKeys);
|
2019-12-05 10:04:21 +08:00
|
|
|
|
// 深递归 data,截取可视区 data 数组,再将扁平结构转换成嵌套结构
|
2019-09-11 21:08:18 +08:00
|
|
|
|
let sliceTreeList = [];
|
|
|
|
|
let flatTreeData = this.deepTraversal(data);
|
|
|
|
|
this.flatTreeData = flatTreeData;
|
2019-12-05 10:04:21 +08:00
|
|
|
|
sliceTreeList = flatTreeData.slice(startIndex, endIndex);
|
2019-09-11 21:08:18 +08:00
|
|
|
|
this.handleTreeListChange(sliceTreeList);
|
2019-12-05 10:04:21 +08:00
|
|
|
|
|
2019-12-05 16:27:38 +08:00
|
|
|
|
this.cacheExpandedKeys = expandedKeys && null;
|
2019-09-11 21:08:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 11:32:29 +08:00
|
|
|
|
/**
|
|
|
|
|
* 深度遍历树形 data,把数据拍平,变为一维数组
|
|
|
|
|
* @param {*} data
|
|
|
|
|
* @param {*} parentKey 标识父节点
|
|
|
|
|
*/
|
2019-12-05 10:04:21 +08:00
|
|
|
|
deepTraversal = (treeData, parentKey=null) => {
|
2019-08-31 11:32:29 +08:00
|
|
|
|
const _this = this;
|
|
|
|
|
let {cacheExpandedKeys, expandedRowKeys = [], flatTreeKeysMap} = _this,
|
2019-11-30 14:16:43 +08:00
|
|
|
|
expandedKeysSet = cacheExpandedKeys ? cacheExpandedKeys : new Set(expandedRowKeys),
|
2019-08-31 11:32:29 +08:00
|
|
|
|
flatTreeData = [],
|
|
|
|
|
dataCopy = treeData;
|
|
|
|
|
if(Array.isArray(dataCopy)){
|
|
|
|
|
for (let i=0, l=dataCopy.length; i<l; i++) {
|
2020-04-01 14:54:41 +08:00
|
|
|
|
let { children, ...props } = dataCopy[i],
|
|
|
|
|
key = this.getRowKey(dataCopy[i],i),//bugfix生成key字段,否则树无法展开
|
2019-11-30 14:16:43 +08:00
|
|
|
|
dataCopyI = new Object(),
|
2020-03-11 13:52:12 +08:00
|
|
|
|
_isLeaf = (children && children.length > 0) ? false : true,
|
2019-11-30 14:16:43 +08:00
|
|
|
|
//如果父节点是收起状态,则子节点的展开状态无意义。(一级节点或根节点直接判断自身状态即可)
|
|
|
|
|
isExpanded = (parentKey === null || expandedKeysSet.has(parentKey)) ? expandedKeysSet.has(key) : false;
|
2019-08-31 11:32:29 +08:00
|
|
|
|
dataCopyI = Object.assign(dataCopyI,{
|
|
|
|
|
key,
|
|
|
|
|
isExpanded,
|
|
|
|
|
parentKey : parentKey,
|
2020-03-11 13:52:12 +08:00
|
|
|
|
_isLeaf,
|
2019-08-31 11:32:29 +08:00
|
|
|
|
index: flatTreeData.length
|
|
|
|
|
},{...props});
|
2019-12-05 10:04:21 +08:00
|
|
|
|
|
|
|
|
|
flatTreeData.push(dataCopyI); // 取每项数据放入一个新数组
|
|
|
|
|
flatTreeKeysMap[key] = dataCopyI;
|
|
|
|
|
|
|
|
|
|
// 优化递归逻辑,如果当前节点是收起状态,则不遍历其子节点
|
|
|
|
|
if (Array.isArray(children) && children.length > 0 && isExpanded){
|
|
|
|
|
flatTreeData = flatTreeData.concat(this.deepTraversal(children, key));
|
2019-08-31 11:32:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return flatTreeData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将截取后的 List 数组转换为 Tree 结构,并更新 state
|
|
|
|
|
*/
|
|
|
|
|
handleTreeListChange = (treeList, startIndex, endIndex) => {
|
|
|
|
|
// 属性配置设置
|
|
|
|
|
let attr = {
|
|
|
|
|
id: 'key',
|
|
|
|
|
parendId: 'parentKey',
|
|
|
|
|
rootId: null,
|
2020-03-11 13:52:12 +08:00
|
|
|
|
_isLeaf: '_isLeaf'
|
2019-08-31 11:32:29 +08:00
|
|
|
|
};
|
|
|
|
|
let treeData = convertListToTree(treeList, attr, this.flatTreeKeysMap);
|
|
|
|
|
|
|
|
|
|
this.startIndex = typeof(startIndex) !== "undefined" ? startIndex : this.startIndex;
|
|
|
|
|
this.endIndex = typeof(endIndex) !== "undefined" ? endIndex : this.endIndex;
|
|
|
|
|
|
|
|
|
|
this.treeData = treeData;
|
2019-01-16 13:57:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*设置data中每个元素的parentIndex
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
computeCachedRowParentIndex = data => {
|
2019-01-25 11:17:52 +08:00
|
|
|
|
const {isTree} = this.props;
|
|
|
|
|
const isTreeType = isTree?true:this.checkIsTreeType();
|
2019-06-19 18:01:30 +08:00
|
|
|
|
treeTypeIndex=0;
|
2019-01-16 13:57:31 +08:00
|
|
|
|
if (isTreeType) {
|
|
|
|
|
data.forEach((item, index) => {
|
|
|
|
|
this.firstLevelKey[index] = this.getRowKey(item, index);
|
2019-01-03 16:21:24 +08:00
|
|
|
|
this.cachedRowParentIndex[treeTypeIndex] = index;
|
|
|
|
|
//保存所有的keys跟小标对应起来
|
2019-01-16 13:57:31 +08:00
|
|
|
|
this.keys[treeTypeIndex] = this.getRowKey(item, index);
|
2019-01-03 16:21:24 +08:00
|
|
|
|
treeTypeIndex++;
|
2019-01-16 13:57:31 +08:00
|
|
|
|
if (item.children) {
|
|
|
|
|
this.getData(item.children, index);
|
2019-01-03 16:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-01-16 13:57:31 +08:00
|
|
|
|
};
|
2019-01-03 16:21:24 +08:00
|
|
|
|
|
2019-01-22 11:03:53 +08:00
|
|
|
|
setStartAndEndIndex(currentIndex,dataLen){
|
|
|
|
|
const _this = this;
|
2019-02-13 17:11:07 +08:00
|
|
|
|
if(currentIndex > _this.currentIndex + _this.rowsInView){
|
2019-01-22 11:03:53 +08:00
|
|
|
|
_this.currentIndex = currentIndex;
|
|
|
|
|
_this.endIndex = _this.currentIndex; //数据开始位置
|
|
|
|
|
_this.startIndex = _this.currentIndex - _this.loadCount; //数据结束位置
|
|
|
|
|
if(_this.endIndex > dataLen){
|
|
|
|
|
_this.endIndex = dataLen;
|
|
|
|
|
}
|
|
|
|
|
if(_this.startIndex < 0){
|
|
|
|
|
_this.startIndex = 0;
|
|
|
|
|
}
|
2019-01-25 11:17:52 +08:00
|
|
|
|
//重新设定scrollTop值
|
|
|
|
|
_this.scrollTop = _this.getSumHeight(0, _this.endIndex - _this.rowsInView +2);
|
|
|
|
|
}else if(currentIndex < _this.currentIndex){
|
2019-01-22 11:03:53 +08:00
|
|
|
|
_this.currentIndex = currentIndex;
|
|
|
|
|
_this.startIndex = currentIndex;
|
|
|
|
|
_this.endIndex = currentIndex + _this.loadCount;
|
|
|
|
|
if(_this.endIndex > dataLen){
|
|
|
|
|
_this.endIndex = dataLen;
|
|
|
|
|
}
|
|
|
|
|
if(_this.startIndex < 0){
|
|
|
|
|
_this.startIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
//重新设定scrollTop值
|
2019-01-25 11:17:52 +08:00
|
|
|
|
_this.scrollTop = _this.getSumHeight(0, _this.startIndex);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-22 11:03:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-03 16:21:24 +08:00
|
|
|
|
getRowKey(record, index) {
|
|
|
|
|
const rowKey = this.props.rowKey;
|
2019-01-16 13:57:31 +08:00
|
|
|
|
const key =
|
|
|
|
|
typeof rowKey === "function" ? rowKey(record, index) : record[rowKey];
|
|
|
|
|
|
2019-01-03 16:21:24 +08:00
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
*判断是否是树形结构
|
|
|
|
|
*
|
|
|
|
|
*/
|
2019-09-11 21:08:18 +08:00
|
|
|
|
checkIsTreeType(newData) {
|
|
|
|
|
const data = newData ? newData : this.props.data;
|
2019-01-03 16:21:24 +08:00
|
|
|
|
let rs = false;
|
2019-01-16 13:57:31 +08:00
|
|
|
|
const len = data.length > 30 ? 30 : data.length;
|
2019-01-03 16:21:24 +08:00
|
|
|
|
//取前三十个看看是否有children属性,有则为树形结构
|
2019-01-16 13:57:31 +08:00
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
|
if (data[i].children) {
|
2019-01-03 16:21:24 +08:00
|
|
|
|
rs = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rs;
|
|
|
|
|
}
|
2019-01-16 13:57:31 +08:00
|
|
|
|
getData(data, parentIndex) {
|
|
|
|
|
data.forEach((subItem, subIndex) => {
|
|
|
|
|
this.cachedRowParentIndex[treeTypeIndex] = parentIndex;
|
|
|
|
|
this.keys[treeTypeIndex] = this.getRowKey(subItem, subIndex);
|
|
|
|
|
treeTypeIndex++;
|
|
|
|
|
if (subItem.children) {
|
|
|
|
|
this.getData(subItem.children, parentIndex);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-01-03 16:21:24 +08:00
|
|
|
|
}
|
2019-01-16 13:57:31 +08:00
|
|
|
|
componentWillUnmount() {
|
2019-01-03 16:21:24 +08:00
|
|
|
|
this.cachedRowHeight = [];
|
|
|
|
|
this.cachedRowParentIndex = [];
|
2018-12-18 13:59:48 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
*获取数据区高度
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
**/
|
|
|
|
|
getContentHeight() {
|
|
|
|
|
if (!this.props.data) return 0;
|
|
|
|
|
return this.getSumHeight(0, this.props.data.length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getSumHeight(start, end) {
|
|
|
|
|
const { height } = this.props;
|
2019-01-05 00:09:35 +08:00
|
|
|
|
let rowHeight = height ? height : defaultHeight;
|
2019-01-16 13:57:31 +08:00
|
|
|
|
let sumHeight = 0,
|
|
|
|
|
currentKey,
|
|
|
|
|
currentRowHeight = rowHeight;
|
2019-01-05 00:09:35 +08:00
|
|
|
|
|
2018-12-18 13:59:48 +08:00
|
|
|
|
for (let i = start; i < end; i++) {
|
2019-01-16 13:57:31 +08:00
|
|
|
|
if (this.cachedRowHeight[i] == undefined) {
|
|
|
|
|
if (this.treeType) {
|
2019-08-31 11:32:29 +08:00
|
|
|
|
// currentKey = this.keys[i];
|
2020-11-12 10:07:48 +08:00
|
|
|
|
currentKey = this.flatTreeData[i] && this.flatTreeData[i].key;
|
2019-01-05 00:09:35 +08:00
|
|
|
|
currentRowHeight = 0;
|
2019-01-16 13:57:31 +08:00
|
|
|
|
if (
|
2019-08-31 11:32:29 +08:00
|
|
|
|
this.flatTreeKeysMap.hasOwnProperty(currentKey)
|
2019-01-16 13:57:31 +08:00
|
|
|
|
) {
|
2019-01-05 00:09:35 +08:00
|
|
|
|
currentRowHeight = rowHeight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sumHeight += currentRowHeight;
|
2019-01-16 13:57:31 +08:00
|
|
|
|
} else {
|
|
|
|
|
sumHeight += this.cachedRowHeight[i];
|
2019-01-05 00:09:35 +08:00
|
|
|
|
}
|
2018-12-18 13:59:48 +08:00
|
|
|
|
}
|
|
|
|
|
return sumHeight;
|
|
|
|
|
}
|
2019-01-16 13:57:31 +08:00
|
|
|
|
|
2019-01-03 16:21:24 +08:00
|
|
|
|
/**
|
|
|
|
|
*@description 根据返回的scrollTop计算当前的索引。此处做了两次缓存一个是根据上一次的currentIndex计算当前currentIndex。另一个是根据当前内容区的数据是否在缓存中如果在则不重新render页面
|
|
|
|
|
*@param 最新一次滚动的scrollTop
|
|
|
|
|
*@param treeType是否是树状表
|
2019-08-27 11:10:47 +08:00
|
|
|
|
*@param callback表体滚动过程中触发的回调
|
2019-01-03 16:21:24 +08:00
|
|
|
|
*/
|
2019-08-27 11:10:47 +08:00
|
|
|
|
handleScrollY = (nextScrollTop, treeType, callback) => {
|
2019-01-03 16:21:24 +08:00
|
|
|
|
//树表逻辑
|
|
|
|
|
// 关键点是动态的获取startIndex和endIndex
|
|
|
|
|
// 法子一:子节点也看成普通tr,最开始需要设置一共有多少行,哪行显示哪行不显示如何确定
|
|
|
|
|
// 动态取start = current+buffer对应的父节点、end = start+loadCount+row的height为0的行数 展开节点的下一个节点作为end值,
|
|
|
|
|
const _this = this;
|
|
|
|
|
const { data, height, scroll = {}, loadBuffer } = _this.props;
|
|
|
|
|
const rowHeight = height ? height : defaultHeight;
|
|
|
|
|
const {
|
|
|
|
|
currentIndex = 0,
|
|
|
|
|
loadCount,
|
|
|
|
|
scrollTop,
|
2019-08-31 11:32:29 +08:00
|
|
|
|
currentScrollTop,
|
|
|
|
|
flatTreeData
|
2019-01-03 16:21:24 +08:00
|
|
|
|
} = _this;
|
|
|
|
|
let { endIndex, startIndex } = _this;
|
|
|
|
|
const { needRender } = _this.state;
|
2019-01-09 12:33:53 +08:00
|
|
|
|
_this.scrollTop = nextScrollTop;
|
2019-01-03 16:21:24 +08:00
|
|
|
|
const viewHeight = parseInt(scroll.y);
|
|
|
|
|
_this.treeType = treeType;
|
|
|
|
|
let index = 0;
|
|
|
|
|
let temp = nextScrollTop;
|
|
|
|
|
let currentKey;
|
2019-01-16 13:57:31 +08:00
|
|
|
|
while (temp > 0) {
|
2019-01-03 16:21:24 +08:00
|
|
|
|
let currentRowHeight = this.cachedRowHeight[index];
|
2019-01-16 13:57:31 +08:00
|
|
|
|
if (currentRowHeight === undefined) {
|
|
|
|
|
if (this.treeType) {
|
2019-08-31 11:32:29 +08:00
|
|
|
|
// currentKey = this.keys[index];
|
|
|
|
|
currentKey = this.flatTreeData[index].key;
|
2019-01-03 16:21:24 +08:00
|
|
|
|
currentRowHeight = 0;
|
2019-01-16 13:57:31 +08:00
|
|
|
|
if (
|
2019-08-31 11:32:29 +08:00
|
|
|
|
this.flatTreeKeysMap.hasOwnProperty(currentKey)
|
2019-01-16 13:57:31 +08:00
|
|
|
|
) {
|
2019-01-03 16:21:24 +08:00
|
|
|
|
currentRowHeight = rowHeight;
|
|
|
|
|
}
|
2019-01-16 13:57:31 +08:00
|
|
|
|
} else {
|
2019-01-03 16:21:24 +08:00
|
|
|
|
currentRowHeight = rowHeight;
|
2018-12-26 10:23:51 +08:00
|
|
|
|
}
|
2018-12-18 13:59:48 +08:00
|
|
|
|
}
|
2019-01-03 16:21:24 +08:00
|
|
|
|
temp -= currentRowHeight;
|
|
|
|
|
if (temp > 0) {
|
|
|
|
|
index += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-07 14:57:58 +08:00
|
|
|
|
// console.log('currentIndex****',index);
|
2019-01-03 16:21:24 +08:00
|
|
|
|
const isOrder = index - currentIndex > 0 ? true : false;
|
|
|
|
|
if (index < 0) index = 0;
|
|
|
|
|
//如果之前的索引和下一次的不一样则重置索引和滚动的位置
|
|
|
|
|
if (currentIndex !== index) {
|
|
|
|
|
_this.currentIndex = index;
|
|
|
|
|
let rowsInView = 0; //可视区域显示多少行
|
|
|
|
|
let rowsHeight = 0; //可视区域内容高度
|
2019-01-16 13:57:31 +08:00
|
|
|
|
let tempIndex = index;
|
2019-01-03 16:21:24 +08:00
|
|
|
|
//如果可视区域中需要展示的数据已经在缓存中则不重现render。
|
|
|
|
|
if (viewHeight) {
|
|
|
|
|
//有时滚动过快时this.cachedRowHeight[rowsInView + index]为undifined
|
2019-01-16 13:57:31 +08:00
|
|
|
|
|
|
|
|
|
while (
|
|
|
|
|
rowsHeight < viewHeight &&
|
|
|
|
|
tempIndex < this.cachedRowHeight.length
|
|
|
|
|
) {
|
|
|
|
|
if (this.cachedRowHeight[tempIndex]) {
|
|
|
|
|
rowsHeight += this.cachedRowHeight[tempIndex];
|
2019-08-31 11:32:29 +08:00
|
|
|
|
// if (
|
|
|
|
|
// (treeType &&
|
|
|
|
|
// _this.cachedRowParentIndex[tempIndex] !== tempIndex) ||
|
|
|
|
|
// !treeType
|
|
|
|
|
// ) {
|
2019-01-16 13:57:31 +08:00
|
|
|
|
rowsInView++;
|
2019-08-31 11:32:29 +08:00
|
|
|
|
// }
|
2019-01-03 16:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
tempIndex++;
|
|
|
|
|
}
|
2019-08-31 11:32:29 +08:00
|
|
|
|
// if (treeType) {
|
|
|
|
|
// const treeIndex = index;
|
|
|
|
|
// index = _this.cachedRowParentIndex[treeIndex];
|
|
|
|
|
// if (index === undefined) {
|
|
|
|
|
// // console.log('index is undefined********'+treeIndex);
|
|
|
|
|
// index = this.getParentIndex(treeIndex);
|
|
|
|
|
// // console.log("getParentIndex****"+index);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2019-01-07 14:57:58 +08:00
|
|
|
|
// console.log('parentIndex*********',index);
|
2019-01-03 16:21:24 +08:00
|
|
|
|
// 如果rowsInView 小于 缓存的数据则重新render
|
|
|
|
|
// 向下滚动 下临界值超出缓存的endIndex则重新渲染
|
|
|
|
|
if (rowsInView + index > endIndex - rowDiff && isOrder) {
|
2019-01-16 13:57:31 +08:00
|
|
|
|
startIndex = index - loadBuffer > 0 ? index - loadBuffer : 0;
|
2019-07-02 16:35:33 +08:00
|
|
|
|
// endIndex = startIndex + rowsInView + loadBuffer*2;
|
2019-01-16 13:57:31 +08:00
|
|
|
|
endIndex = startIndex + loadCount;
|
2019-08-31 11:32:29 +08:00
|
|
|
|
if (treeType && endIndex > flatTreeData.length || !treeType && endIndex > data.length) {
|
|
|
|
|
endIndex = treeType ? flatTreeData.length : data.length;
|
2019-01-03 16:21:24 +08:00
|
|
|
|
}
|
2019-07-02 16:35:33 +08:00
|
|
|
|
if (endIndex > this.endIndex ) {
|
2019-01-16 13:57:31 +08:00
|
|
|
|
this.startIndex = startIndex;
|
|
|
|
|
this.endIndex = endIndex;
|
2019-08-31 11:32:29 +08:00
|
|
|
|
if(treeType) {
|
|
|
|
|
this.handleTreeListChange(flatTreeData.slice(startIndex,endIndex), startIndex, endIndex)
|
|
|
|
|
}
|
2019-01-16 13:57:31 +08:00
|
|
|
|
this.setState({ needRender: !needRender });
|
2019-08-27 11:10:47 +08:00
|
|
|
|
callback(parseInt(currentIndex + rowsInView));
|
2019-01-03 16:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 向上滚动,当前的index是否已经加载(currentIndex),若干上临界值小于startIndex则重新渲染
|
|
|
|
|
if (!isOrder && index < startIndex + rowDiff) {
|
|
|
|
|
startIndex = index - loadBuffer;
|
|
|
|
|
if (startIndex < 0) {
|
|
|
|
|
startIndex = 0;
|
|
|
|
|
}
|
2019-07-02 16:35:33 +08:00
|
|
|
|
if (startIndex < this.startIndex) {
|
2019-01-16 13:57:31 +08:00
|
|
|
|
this.startIndex = startIndex;
|
|
|
|
|
this.endIndex = this.startIndex + loadCount;
|
2019-08-31 11:32:29 +08:00
|
|
|
|
if(treeType) {
|
|
|
|
|
this.handleTreeListChange(flatTreeData.slice(startIndex,this.endIndex), startIndex, this.endIndex)
|
|
|
|
|
}
|
2019-01-16 13:57:31 +08:00
|
|
|
|
this.setState({ needRender: !needRender });
|
2019-08-27 11:10:47 +08:00
|
|
|
|
callback(parseInt(currentIndex + rowsInView));
|
2019-01-03 16:21:24 +08:00
|
|
|
|
}
|
2019-01-07 14:57:58 +08:00
|
|
|
|
// console.log(
|
|
|
|
|
// "**index**" + index,
|
|
|
|
|
// "**startIndex**" + this.startIndex,
|
|
|
|
|
// "**endIndex**" + this.endIndex
|
|
|
|
|
// );
|
2019-01-03 16:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setRowHeight(height, index) {
|
|
|
|
|
this.cachedRowHeight[index] = height;
|
|
|
|
|
}
|
2019-01-16 13:57:31 +08:00
|
|
|
|
setRowParentIndex(parentIndex, index) {
|
|
|
|
|
// this.cachedRowParentIndex[index] = parentIndex;
|
2019-01-03 16:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*根据当前行号获取该行的父节点行号
|
|
|
|
|
* @param {*} currentIndex 当前行号
|
|
|
|
|
*/
|
|
|
|
|
getParentIndex(targetIndex) {
|
2019-01-16 13:57:31 +08:00
|
|
|
|
const { data } = this.props;
|
|
|
|
|
let parentIndex = -1;
|
|
|
|
|
parentIndex = this.getIndex(data, -1, targetIndex);
|
|
|
|
|
if (parentIndex < 0) {
|
|
|
|
|
//小于0说明没有展开的子节点
|
|
|
|
|
parentIndex = targetIndex;
|
|
|
|
|
}
|
|
|
|
|
return parentIndex;
|
2019-01-03 16:21:24 +08:00
|
|
|
|
}
|
2019-01-16 13:57:31 +08:00
|
|
|
|
getIndex(data, index, targetIndex) {
|
|
|
|
|
const parentIndex = index;
|
|
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
|
|
|
index++;
|
|
|
|
|
if (targetIndex <= index) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (data[i].children) {
|
|
|
|
|
this.getIndex(data[i].children, index, targetIndex);
|
2018-12-18 13:59:48 +08:00
|
|
|
|
}
|
2019-01-16 13:57:31 +08:00
|
|
|
|
}
|
|
|
|
|
return parentIndex;
|
2018-12-18 13:59:48 +08:00
|
|
|
|
}
|
2019-01-03 16:21:24 +08:00
|
|
|
|
|
2019-02-25 14:40:28 +08:00
|
|
|
|
onExpand = (expandState, record,index) => {
|
2019-01-03 16:21:24 +08:00
|
|
|
|
const _this = this;
|
2019-02-25 14:40:28 +08:00
|
|
|
|
let {expandedRowKeys = []} = _this;
|
|
|
|
|
const {needRender} = _this.state;
|
2019-08-31 11:32:29 +08:00
|
|
|
|
const { data } = _this.props;
|
2019-02-25 14:40:28 +08:00
|
|
|
|
const rowKey = _this.getRowKey(record, index);
|
|
|
|
|
// 记录展开子表行的key
|
2019-01-03 16:21:24 +08:00
|
|
|
|
// 展开
|
2019-02-25 14:40:28 +08:00
|
|
|
|
if( record.children){
|
|
|
|
|
if (expandState) {
|
|
|
|
|
record.children.forEach((item, index) => {
|
|
|
|
|
_this.expandChildRowKeys.push(rowKey);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// 收起
|
|
|
|
|
record.children.forEach((item, index) => {
|
|
|
|
|
_this.expandChildRowKeys.splice(
|
|
|
|
|
_this.expandChildRowKeys.findIndex(
|
|
|
|
|
fitem => fitem.key === item.key
|
|
|
|
|
),
|
|
|
|
|
1
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-01-03 16:21:24 +08:00
|
|
|
|
}
|
2019-02-25 14:40:28 +08:00
|
|
|
|
//滚动加载expandedRowKeys自己维护,否则有展开不全的问题
|
|
|
|
|
if(!_this.props.expandedRowKeys){
|
|
|
|
|
if(expandState){
|
|
|
|
|
expandedRowKeys.push(rowKey);
|
2019-04-17 15:26:27 +08:00
|
|
|
|
this.setState({ needRender: !needRender });
|
2019-11-06 15:03:26 +08:00
|
|
|
|
}else{
|
2019-02-25 14:40:28 +08:00
|
|
|
|
let index = -1;
|
|
|
|
|
expandedRowKeys.forEach((r, i) => {
|
|
|
|
|
if (r === rowKey) {
|
|
|
|
|
index = i;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
expandedRowKeys.splice(index, 1);
|
|
|
|
|
this.setState({ needRender: !needRender });
|
|
|
|
|
}
|
2019-11-06 15:03:26 +08:00
|
|
|
|
}
|
2019-08-31 11:32:29 +08:00
|
|
|
|
}
|
2019-02-25 14:40:28 +08:00
|
|
|
|
|
2019-11-06 15:03:26 +08:00
|
|
|
|
// expandState为true时,记录下
|
2019-01-10 16:07:41 +08:00
|
|
|
|
_this.props.onExpand(expandState, record);
|
2019-11-30 14:16:43 +08:00
|
|
|
|
|
|
|
|
|
if(this.treeType) {
|
|
|
|
|
//收起和展开时,缓存 expandedKeys
|
|
|
|
|
_this.cacheExpandedKeys = new Set(expandedRowKeys);
|
|
|
|
|
//重新递归数据
|
|
|
|
|
let flatTreeData = _this.deepTraversal(data);
|
|
|
|
|
let sliceTreeList = flatTreeData.slice(_this.startIndex, _this.endIndex);
|
|
|
|
|
_this.flatTreeData = flatTreeData;
|
|
|
|
|
_this.handleTreeListChange(sliceTreeList);
|
|
|
|
|
_this.cacheExpandedKeys = null;
|
|
|
|
|
}
|
2019-01-16 13:57:31 +08:00
|
|
|
|
};
|
2019-02-25 14:40:28 +08:00
|
|
|
|
|
|
|
|
|
|
2018-12-18 13:59:48 +08:00
|
|
|
|
render() {
|
|
|
|
|
const { data } = this.props;
|
2018-12-20 18:07:30 +08:00
|
|
|
|
const { scrollTop } = this;
|
2019-08-31 11:32:29 +08:00
|
|
|
|
let { endIndex, startIndex, treeData, treeType, flatTreeData } = this;
|
2019-02-25 14:40:28 +08:00
|
|
|
|
let expandedRowKeys = this.props.expandedRowKeys?this.props.expandedRowKeys: this.expandedRowKeys;
|
2019-02-19 16:08:44 +08:00
|
|
|
|
if(startIndex < 0){
|
|
|
|
|
startIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
if(endIndex < 0 ){
|
|
|
|
|
endIndex = 0;
|
|
|
|
|
}
|
2019-08-31 11:32:29 +08:00
|
|
|
|
if (treeType && endIndex > flatTreeData.length || !treeType && endIndex > data.length) {
|
|
|
|
|
endIndex = treeType ? flatTreeData.length : data.length;
|
2019-02-19 16:08:44 +08:00
|
|
|
|
}
|
2018-12-18 13:59:48 +08:00
|
|
|
|
const lazyLoad = {
|
2019-01-03 16:21:24 +08:00
|
|
|
|
startIndex: startIndex,
|
2019-07-02 16:35:33 +08:00
|
|
|
|
endIndex:endIndex,
|
2019-01-16 13:57:31 +08:00
|
|
|
|
startParentIndex: startIndex //为树状节点做准备
|
2018-12-18 13:59:48 +08:00
|
|
|
|
};
|
2019-08-31 11:32:29 +08:00
|
|
|
|
if (treeType) {
|
|
|
|
|
lazyLoad.preHeight = this.getSumHeight(0, startIndex);
|
|
|
|
|
lazyLoad.sufHeight = this.getSumHeight(endIndex, flatTreeData.length);
|
2019-01-16 13:57:31 +08:00
|
|
|
|
} else {
|
|
|
|
|
lazyLoad.preHeight = this.getSumHeight(0, startIndex);
|
|
|
|
|
lazyLoad.sufHeight = this.getSumHeight(endIndex, data.length);
|
2019-01-03 16:21:24 +08:00
|
|
|
|
}
|
2019-02-25 14:40:28 +08:00
|
|
|
|
// console.log('*******expandedRowKeys*****'+expandedRowKeys);
|
2019-12-05 10:04:21 +08:00
|
|
|
|
const dataSource = (treeType && Array.isArray(treeData) && treeData.length > 0) ? treeData : data.slice(startIndex, endIndex);
|
2018-12-18 13:59:48 +08:00
|
|
|
|
return (
|
|
|
|
|
<Table
|
|
|
|
|
{...this.props}
|
2019-09-11 21:08:18 +08:00
|
|
|
|
data={dataSource}
|
2018-12-18 13:59:48 +08:00
|
|
|
|
lazyLoad={lazyLoad}
|
2021-03-17 17:37:35 +08:00
|
|
|
|
ref={el => this.table = el}
|
2019-01-10 16:07:41 +08:00
|
|
|
|
handleScrollY={this.handleScrollY}
|
2018-12-18 13:59:48 +08:00
|
|
|
|
scrollTop={scrollTop}
|
|
|
|
|
setRowHeight={this.setRowHeight}
|
2019-01-16 13:57:31 +08:00
|
|
|
|
setRowParentIndex={this.setRowParentIndex}
|
|
|
|
|
onExpand={this.onExpand}
|
2019-11-06 15:03:26 +08:00
|
|
|
|
onExpandedRowsChange={this.props.onExpandedRowsChange}
|
2019-02-25 14:40:28 +08:00
|
|
|
|
expandedRowKeys={expandedRowKeys}
|
2019-01-03 16:21:24 +08:00
|
|
|
|
// className={'lazy-table'}
|
2018-12-18 13:59:48 +08:00
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|