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

View File

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

View File

@ -63,6 +63,7 @@ function bigData(Table) {
_this2.endIndex = _this2.currentIndex + _this2.loadCount; //数据结束位置
_this2.setRowHeight = _this2.setRowHeight.bind(_this2);
_this2.setRowParentIndex = _this2.setRowParentIndex.bind(_this2);
_this2.expandedRowKeys = [];
return _this2;
}
@ -269,6 +270,7 @@ function bigData(Table) {
var endIndex = this.endIndex,
startIndex = this.startIndex;
var expandedRowKeys = this.props.expandedRowKeys ? this.props.expandedRowKeys : this.expandedRowKeys;
if (startIndex < 0) {
startIndex = 0;
}
@ -299,7 +301,7 @@ function bigData(Table) {
lazyLoad.preHeight = this.getSumHeight(0, startIndex);
lazyLoad.sufHeight = this.getSumHeight(endIndex, data.length);
}
// console.log('*******ScrollTop*****'+scrollTop);
// console.log('*******expandedRowKeys*****'+expandedRowKeys);
return _react2["default"].createElement(Table, _extends({}, this.props, {
data: data.slice(startIndex, endIndex),
lazyLoad: lazyLoad,
@ -308,7 +310,8 @@ function bigData(Table) {
setRowHeight: this.setRowHeight,
setRowParentIndex: this.setRowParentIndex,
onExpand: this.onExpand,
onExpandedRowsChange: this.onExpandedRowsChange
onExpandedRowsChange: this.onExpandedRowsChange,
expandedRowKeys: expandedRowKeys
// className={'lazy-table'}
}));
};
@ -372,30 +375,6 @@ function bigData(Table) {
_this.scrollTop = nextScrollTop;
var viewHeight = parseInt(scroll.y);
_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 temp = nextScrollTop;
var currentKey = void 0;
@ -454,17 +433,6 @@ function bigData(Table) {
if (rowsInView + index > endIndex - rowDiff && isOrder) {
startIndex = index - loadBuffer > 0 ? index - loadBuffer : 0;
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) {
endIndex = data.length;
}
@ -473,12 +441,6 @@ function bigData(Table) {
_this4.endIndex = endIndex;
_this4.setState({ needRender: !needRender });
}
// console.log(
// "===================",
// "**index**" + index,
// " **startIndex**" + this.startIndex,
// "**endIndex**" + this.endIndex
// );
}
// 向上滚动当前的index是否已经加载currentIndex若干上临界值小于startIndex则重新渲染
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$expandedRowKeys = _this.expandedRowKeys,
expandedRowKeys = _this$expandedRowKeys === undefined ? [] : _this$expandedRowKeys;
var needRender = _this.state.needRender;
var rowKey = _this.getRowKey(record, index);
// 记录展开子表行的key
// 展开
if (record.children) {
if (expandState) {
record.children && record.children.forEach(function (item, index) {
_this.expandChildRowKeys.push(_this.getRowKey(item, index));
record.children.forEach(function (item, index) {
_this.expandChildRowKeys.push(rowKey);
});
} else {
// 收起
record.children && record.children.forEach(function (item, index) {
record.children.forEach(function (item, index) {
_this.expandChildRowKeys.splice(_this.expandChildRowKeys.findIndex(function (fitem) {
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);
};
}, _temp;

2
dist/demo.css vendored
View File

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

2
dist/demo.css.map vendored

File diff suppressed because one or more lines are too long

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

View File

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

View File

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

View File

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

View File

@ -42,6 +42,7 @@ export default function bigData(Table) {
this.endIndex = this.currentIndex + this.loadCount; //数据结束位置
this.setRowHeight = this.setRowHeight.bind(this);
this.setRowParentIndex = this.setRowParentIndex.bind(this);
this.expandedRowKeys = [];
}
componentWillReceiveProps(nextProps) {
const props = this.props;
@ -227,30 +228,6 @@ export default function bigData(Table) {
_this.scrollTop = nextScrollTop;
const viewHeight = parseInt(scroll.y);
_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 temp = nextScrollTop;
let currentKey;
@ -319,17 +296,6 @@ export default function bigData(Table) {
if (rowsInView + index > endIndex - rowDiff && isOrder) {
startIndex = index - loadBuffer > 0 ? index - loadBuffer : 0;
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) {
endIndex = data.length;
}
@ -338,12 +304,6 @@ export default function bigData(Table) {
this.endIndex = endIndex;
this.setState({ needRender: !needRender });
}
// console.log(
// "===================",
// "**index**" + index,
// " **startIndex**" + this.startIndex,
// "**endIndex**" + this.endIndex
// );
}
// 向上滚动当前的index是否已经加载currentIndex若干上临界值小于startIndex则重新渲染
if (!isOrder && index < startIndex + rowDiff) {
@ -401,17 +361,21 @@ export default function bigData(Table) {
return parentIndex;
}
onExpand = (expandState, record) => {
onExpand = (expandState, record,index) => {
const _this = this;
let {expandedRowKeys = []} = _this;
const {needRender} = _this.state;
const rowKey = _this.getRowKey(record, index);
// 记录展开子表行的key
// 展开
if( record.children){
if (expandState) {
record.children &&
record.children.forEach((item, index) => {
_this.expandChildRowKeys.push(_this.getRowKey(item, index));
_this.expandChildRowKeys.push(rowKey);
});
} else {
// 收起
record.children &&
record.children.forEach((item, index) => {
_this.expandChildRowKeys.splice(
_this.expandChildRowKeys.findIndex(
@ -421,13 +385,37 @@ export default function bigData(Table) {
);
});
}
}
//滚动加载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);
};
render() {
const { data } = this.props;
const { scrollTop } = this;
let { endIndex, startIndex } = this;
let expandedRowKeys = this.props.expandedRowKeys?this.props.expandedRowKeys: this.expandedRowKeys;
if(startIndex < 0){
startIndex = 0;
}
@ -466,7 +454,7 @@ export default function bigData(Table) {
lazyLoad.preHeight = this.getSumHeight(0, startIndex);
lazyLoad.sufHeight = this.getSumHeight(endIndex, data.length);
}
// console.log('*******ScrollTop*****'+scrollTop);
// console.log('*******expandedRowKeys*****'+expandedRowKeys);
return (
<Table
{...this.props}
@ -478,6 +466,7 @@ export default function bigData(Table) {
setRowParentIndex={this.setRowParentIndex}
onExpand={this.onExpand}
onExpandedRowsChange={this.onExpandedRowsChange}
expandedRowKeys={expandedRowKeys}
// className={'lazy-table'}
/>
);