增加currentIndex属性,支持用户自定义滚动位置

This commit is contained in:
wanghaoo 2019-01-22 11:03:53 +08:00
parent 208ff4b001
commit 7bb4033427
8 changed files with 336 additions and 149 deletions

View File

@ -284,7 +284,7 @@ var Table = function (_Component) {
}
//适配lazyload
if (nextProps.scrollTop) {
this.refs.bodyTable.scrollTop = nextProps.scrollTop;
// this.refs.bodyTable.scrollTop = nextProps.scrollTop;
this.scrollTop = nextProps.scrollTop;
}
if (!nextProps.originWidth) {
@ -308,6 +308,7 @@ var Table = function (_Component) {
if (this.scrollTop) {
this.refs.fixedColumnsBodyLeft && (this.refs.fixedColumnsBodyLeft.scrollTop = this.scrollTop);
this.refs.fixedColumnsBodyRight && (this.refs.fixedColumnsBodyRight.scrollTop = this.scrollTop);
this.refs.bodyTable.scrollTop = this.scrollTop;
this.scrollTop = 0;
}
};

View File

@ -68,17 +68,29 @@ function bigData(Table) {
BigData.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
var props = this.props;
var currentIndex = nextProps.currentIndex,
data = nextProps.data;
var _this = this,
dataLen = data.length;
if (nextProps.scroll.y !== props.scroll.y) {
var rowHeight = nextProps.height ? nextProps.height : defaultHeight;
var scrollY = nextProps.scroll.y ? parseInt(nextProps.scroll.y) : 0;
this.rowsInView = scrollY ? Math.ceil(scrollY / rowHeight) : 20;
this.loadCount = props.loadBuffer ? this.rowsInView + props.loadBuffer * 2 : 26; //一次加载多少数据
this.currentIndex = 0;
this.startIndex = this.currentIndex; //数据开始位置
this.endIndex = this.currentIndex + this.loadCount; //数据结束位置
_this.rowsInView = scrollY ? Math.ceil(scrollY / rowHeight) : 20;
_this.loadCount = props.loadBuffer ? _this.rowsInView + props.loadBuffer * 2 : 26; //一次加载多少数据
_this.currentIndex = 0;
_this.startIndex = _this.currentIndex; //数据开始位置
_this.endIndex = _this.currentIndex + _this.loadCount; //数据结束位置
if (_this.endIndex > dataLen) {
_this.endIndex = dataLen;
}
}
if (nextProps.data !== props.data) {
this.computeCachedRowParentIndex(nextProps.data);
_this.computeCachedRowParentIndex(nextProps.data);
}
//如果传currentIndex会判断该条数据是否在可视区域如果没有的话则重新计算startIndex和endIndex
if (currentIndex !== -1 && currentIndex !== this.currentIndex) {
_this.setStartAndEndIndex(currentIndex, dataLen);
}
};
@ -94,6 +106,33 @@ function bigData(Table) {
*/
BigData.prototype.setStartAndEndIndex = function setStartAndEndIndex(currentIndex, dataLen) {
var _this = this;
if (currentIndex > _this.endIndex) {
_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;
}
} else if (currentIndex < _this.startIndex) {
_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值
_this.scrollTop = _this.getSumHeight(0, _this.endIndex - _this.rowsInView + 2);
};
BigData.prototype.getRowKey = function getRowKey(record, index) {
var rowKey = this.props.rowKey;
var key = typeof rowKey === "function" ? rowKey(record, index) : record[rowKey];
@ -270,7 +309,8 @@ function bigData(Table) {
rowKey: "key",
onExpand: function onExpand() {},
scroll: {}
scroll: {},
currentIndex: -1
}, _class.propTypes = {
loadBuffer: _propTypes2["default"].number
}, _initialiseProps = function _initialiseProps() {

View File

@ -10,57 +10,7 @@ import Tooltip from "bee-tooltip";
import Table from "../../src";
import BigData from "../../src/lib/bigData";
const BigDataTable = BigData(Table);
const columns = [
{
title:'序号',
dataIndex:'index',
width:'50',
render:(text,record,index)=>{
return index
},
fixed:'left'
},
{
title: "用户名", dataIndex: "a", key: "a", width: 580, className: "rowClassName",
render: (text, record, index) => {
return (
<Tooltip inverse overlay={text}>
<span tootip={text} style={{
display: "inline-block",
width: "80px",
textOverflow: "ellipsis",
overflow: "hidden",
whiteSpace: "nowrap",
verticalAlign: "middle",
}}>{text}</span>
</Tooltip>
);
}
},
{ id: "123", title: "性别", dataIndex: "b", key: "b", width: 80},
{ title: "年龄", dataIndex: "c", key: "c", width: 200 },
{
title: "操作",
dataIndex: "d",
key: "d",
fixed:'right',
render(text, record, index) {
return (
<div style={{ position: 'relative' }} title={text} >
<a
href="javascript:;"
tooltip={text}
onClick={() => {
alert('这是第' + index + '列,内容为:' + text);
}}
>
一些操作
</a>
</div>
);
}
}
];
const data = [ ...new Array(10000) ].map((e, i) => {
const rs = { a: i + 'a', b: i + 'b', c: i + 'c', d: i + 'd', key: i };
@ -75,25 +25,100 @@ class Demo30 extends Component {
constructor(props) {
super(props);
const _this = this;
this.state = {
data: data,
selectedRowIndex: 0
selectedRowIndex: 0,
currentIndex:-1
}
this.columns = [
{
title:'序号',
dataIndex:'index',
width:'50',
render:(text,record,index)=>{
return index
},
fixed:'left'
},
{
title: "用户名", dataIndex: "a", key: "a", width: 580, className: "rowClassName",
render: (text, record, index) => {
return (
<Tooltip inverse overlay={text}>
<span tootip={text} style={{
display: "inline-block",
width: "80px",
textOverflow: "ellipsis",
overflow: "hidden",
whiteSpace: "nowrap",
verticalAlign: "middle",
}}>{text}</span>
</Tooltip>
);
}
},
{ id: "123", title: "性别", dataIndex: "b", key: "b", width: 80},
{ title: "年龄", dataIndex: "c", key: "c", width: 200 },
{
title: "操作",
dataIndex: "d",
key: "d",
fixed:'right',
render(text, record, index) {
return (
<div style={{ position: 'relative' }} title={text} >
<a
href="javascript:;"
tooltip={text}
onClick={() => {
alert(record.key+'这是第' + index + '列,内容为:' + text);
}}
>
{record.key+'这是第' + index}
</a>
<button onClick={_this.del(record,index) }>删除</button>
</div>
);
}
}
];
}
add=()=>{
let {currentIndex} = this.state;
let data = this.state.data;
const key = data.length;
data.push({a: key + 'a', b: key + 'b', c: key + 'c', d: key + 'd', key: key});
currentIndex = data.length
this.setState({data,currentIndex});
}
del=(record,index)=>(record,index)=>{
let data = this.state.data;
data.splice(index,1);
this.setState(data);
}
render() {
const {currentIndex} = this.state;
return (
<div>
<button onClick = {this.add}>增加</button>
<BigDataTable
columns={columns}
columns={this.columns}
data={data}
parentNodeId='parent'
scroll={{y:300}}
currentIndex = {currentIndex}
height={40}
onRowClick={(record, index, indent) => {
console.log('currentIndex--'+index);
}}
/>
</div>
);
}

File diff suppressed because one or more lines are too long

232
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

@ -184,7 +184,7 @@ class Table extends Component {
}
//适配lazyload
if(nextProps.scrollTop){
this.refs.bodyTable.scrollTop = nextProps.scrollTop;
// this.refs.bodyTable.scrollTop = nextProps.scrollTop;
this.scrollTop = nextProps.scrollTop;
}
if (!nextProps.originWidth) {
@ -209,6 +209,7 @@ class Table extends Component {
if(this.scrollTop){
this.refs.fixedColumnsBodyLeft && ( this.refs.fixedColumnsBodyLeft.scrollTop = this.scrollTop);
this.refs.fixedColumnsBodyRight && ( this.refs.fixedColumnsBodyRight.scrollTop = this.scrollTop);
this.refs.bodyTable.scrollTop = this.scrollTop;
this.scrollTop = 0;
}

View File

@ -10,7 +10,8 @@ export default function bigData(Table) {
loadBuffer: 5,
rowKey: "key",
onExpand() {},
scroll: {}
scroll: {},
currentIndex:-1
};
static propTypes = {
loadBuffer: PropTypes.number
@ -43,19 +44,28 @@ export default function bigData(Table) {
}
componentWillReceiveProps(nextProps) {
const props = this.props;
const {currentIndex ,data} = nextProps;
const _this = this,dataLen = data.length;
if (nextProps.scroll.y !== props.scroll.y) {
const rowHeight = nextProps.height ? nextProps.height : defaultHeight;
const scrollY = nextProps.scroll.y ? parseInt(nextProps.scroll.y) : 0;
this.rowsInView = scrollY ? Math.ceil(scrollY / rowHeight) : 20;
this.loadCount = props.loadBuffer
? this.rowsInView + props.loadBuffer * 2
_this.rowsInView = scrollY ? Math.ceil(scrollY / rowHeight) : 20;
_this.loadCount = props.loadBuffer
? _this.rowsInView + props.loadBuffer * 2
: 26; //一次加载多少数据
this.currentIndex = 0;
this.startIndex = this.currentIndex; //数据开始位置
this.endIndex = this.currentIndex + this.loadCount; //数据结束位置
_this.currentIndex = 0;
_this.startIndex = _this.currentIndex; //数据开始位置
_this.endIndex = _this.currentIndex + _this.loadCount; //数据结束位置
if(_this.endIndex > dataLen){
_this.endIndex = dataLen;
}
}
if (nextProps.data !== props.data) {
this.computeCachedRowParentIndex(nextProps.data);
_this.computeCachedRowParentIndex(nextProps.data);
}
//如果传currentIndex会判断该条数据是否在可视区域如果没有的话则重新计算startIndex和endIndex
if(currentIndex!==-1 && currentIndex !== this.currentIndex){
_this.setStartAndEndIndex(currentIndex,dataLen);
}
}
@ -84,6 +94,34 @@ export default function bigData(Table) {
}
};
setStartAndEndIndex(currentIndex,dataLen){
const _this = this;
if(currentIndex > _this.endIndex){
_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;
}
}else if(currentIndex < _this.startIndex){
_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值
_this.scrollTop = _this.getSumHeight(0, _this.endIndex - _this.rowsInView +2);
}
getRowKey(record, index) {
const rowKey = this.props.rowKey;
const key =