import { Space, Table, Tag } from 'antd';
import React, { useEffect,useState } from 'react';
import './index.css';
const columns = [
{
title: 'id',
dataIndex: 'id',
key: 'id',
},
{
title: 'Name',
dataIndex: 'name',
key: 'name',
render: (text) => <a>{text}</a>,
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
},
{
title: 'Address',
dataIndex: 'address',
key: 'address',
},
];
let datas = []
for(let i=0;i<10000;i++){
datas.push({name:''+i,id:i, key:'' + i})
i%2===0 && (datas[i].name = 'llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll')
i%3===0 && (datas[i].name = 'lllllllllllllllllllllllllllllllllllllkkkkkkkkklmmcsllllllllllllllllllllllllllllllllhddddddddddddddddddddddddddddddddddddddlllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll')
i===9998 && (datas[i].name = 'lllllllllllllllllllllllllllllllllllllkkkkkkkkklmmcsllllllllllllllllllllllllllllllllhddddddddddddddddddddddddddddddddddddddlllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll看到基督教基督教的亟待解决的就打架大家基督教基督教的就打架大家巨大的积极的角度亟待解决的大家假的假的假的假的坚决打击就打架大家觉得就打架大家假的假的假的假的的男男女女你就大胆的当年年底iiiiiiiiiiiiiiiiiiiii哦顶顶顶顶顶顶是看可是可是可是姐姐大家都送哦时间参加就参加')
i === 9999 && (datas[i].name = 'lllllllllllllllllllllllllllllllllllllkkkkkkkkklmmcsllllllllllllllllllllllllllllllllhddddddddddddddddddddddddddddddddddddddlllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll看到基督教基督教的亟待解决的就打架大家基督教基督教的就打架大家巨大的积极的角度亟待解决的大家假的假的假的假的坚决打击就打架大家觉得就打架大家假的假的假的假的的男男女女你就大胆的当年年底iiiiiiiiiiiiiiiiiiiii哦顶顶顶顶顶顶是看可是可是可是姐姐大家都送哦时间参加就参加')
}
let lineHeight = 55
let cacheArr = datas.map((item,index) => {
return {
index,
start:index*lineHeight,
end:(index+1)*lineHeight,
height:lineHeight
}
})
const TableHeight = () => {
let allHeight = 500
let lineHeight = 55
let pageCount = Math.round(allHeight / lineHeight)
const [tableData, setTableData] = useState(datas.slice(0, pageCount))
const [listAllHeight, setListAllHeight] = useState(datas.length - 1 + 'px')
const [isAllHeight, setIsAllHeight] = useState(false)
const [allHeightIndex,setAllHeightIndex] = useState(datas.length - 1)
let stopScrollIndex = datas.length - 1
let handleTableScroll = () => {
let dom = document.querySelector('.ant-table-body')
let div = document.createElement("div");
div.style.height= listAllHeight
div.style.position = 'absolute'
div.style.width = '1px'
div.className = 'myDiv'
dom.insertBefore(div, dom.children[0]);
updatePosition(0)
dom.addEventListener('scroll', () => {
let scrollTop = dom.scrollTop
let startIndex = findItemIndex(scrollTop)
if(startIndex >stopScrollIndex){
return
}
const viewCount = Math.ceil(allHeight / lineHeight);
let endIndex = startIndex + viewCount
setTableData(datas.slice(startIndex, endIndex))
updatePosition(startIndex,scrollTop)
})
}
let findItemIndex = (scrollTop) => {
let low = 0;
let high = cacheArr.length - 1;
while(low <= high) {
const mid = Math.floor((low + high) / 2);
const { start, end } = cacheArr[mid];
if (scrollTop >= start && scrollTop <= end) {
high = mid;
break;
} else if (scrollTop > end) {
low = mid + 1;
} else if (scrollTop < start) {
high = mid - 1;
}
}
return high;
}
let getListAllHeight = ()=> {
return (cacheArr && cacheArr.length > 0
? cacheArr[cacheArr.length - 1].end
: 0) + 'px'
}
const updatePosition = (startIndex,scrollTop)=> {
let trNodes = [...document.querySelectorAll('tr')].slice(2)
let itemIndex = startIndex
let endIndex = itemIndex+2
let startRest = cacheArr[startIndex].height - scrollTop%cacheArr[startIndex].height
for(let i=0;i<trNodes.length;i++){
let node = trNodes[i]
let newHeight = node.getBoundingClientRect().height;
console.log(itemIndex)
let oldHeight = cacheArr[itemIndex].height
const dValue = Math.abs(newHeight - oldHeight)
if (dValue) {
cacheArr[itemIndex].height = newHeight
cacheArr[itemIndex].end = itemIndex > 0 ? cacheArr[itemIndex - 1].end + newHeight : cacheArr[itemIndex].height;
cacheArr[itemIndex].start = itemIndex > 0 ? cacheArr[itemIndex - 1].end : 0;
for (let j = itemIndex + 1, len = cacheArr.length; j < len; j++) {
cacheArr[j].start = cacheArr[j - 1].end;
cacheArr[j].end += dValue;
}
}
endIndex = itemIndex+2
let curHeight = 0
curHeight = startIndex && itemIndex>startIndex? startRest + cacheArr[itemIndex].end-cacheArr[startIndex+1].end : startRest
if(curHeight>500){
setTableData(datas.slice(startIndex, endIndex))
!isAllHeight && itemIndex === cacheArr.length-1 && setAllHeightIndex(startIndex)&& setIsAllHeight(true)
break;
}
if (itemIndex >= datas.length-1) {
break;
}
itemIndex++
}
itemIndex = itemIndex>cacheArr.length-1?cacheArr.length-1:itemIndex
if(startIndex<=allHeightIndex){
setListAllHeight(getListAllHeight())
console.log(listAllHeight,getListAllHeight())
document.querySelector('.myDiv').style.height = getListAllHeight()
}
if(scrollTop !== undefined){
let isScrollHeight = startIndex<cacheArr.length-1?(startRest + cacheArr[itemIndex].end-cacheArr[startIndex+1].end):cacheArr[startIndex].height
let isScroll = isScrollHeight>500
if(!isScroll && itemIndex===cacheArr.length-1){
stopScrollIndex = startIndex
}else{
stopScrollIndex = cacheArr.length-1
}
console.log(startIndex)
let startOffset = scrollTop - scrollTop % cacheArr[startIndex].height
let tableBody = document.querySelector('tbody')
tableBody.style.transform = `translate3d(0,${startOffset}px,0)`
}
}
useEffect(() => {
handleTableScroll()
},[])
return (
<Table
columns={columns}
dataSource={tableData}
scroll={{ y: 500 }}
pagination={false}/>
)
};
export default TableHeight
.ant-table-wrapper{
overflow: hidden;
}
.ant-table-body{
position: relative;
height: 500px;
}