Javascript 将桌子放置在div中
我已经使用下面的代码片段在html表中显示数据,从而缩短了大数据的加载时间 代码段:Javascript 将桌子放置在div中,javascript,jquery,html,Javascript,Jquery,Html,我已经使用下面的代码片段在html表中显示数据,从而缩短了大数据的加载时间 代码段: <body onload="load();"> <div id="Grid" style="overflow:scroll; width:600px;height:300px; onscroll="handleScroll();"></div> <script type="text/javascript">
<body onload="load();">
<div id="Grid" style="overflow:scroll; width:600px;height:300px; onscroll="handleScroll();"></div>
<script type="text/javascript">
var orderArray = [100];
var viewportH = 300;
var prevScrollTop = 0;
var scrollTop = 0;
var vScrollDir = 1;
var rows = [];
var top=0;
var temp=0;
var m = 0;
var prevRenderedRange = {}, prevVisibleRange = {};
var renderedrange, visiblerange;
var rows = [];
var extend;
var tbody = document.createElement('tbody');
for (var i = 0; i < 100; i++) {
orderArray[i] = { "CustomerID": i, "CustomerName": "Name" + i, "City": "City" + i }
}
function load() {
var panel = document.createElement('div');
var h = orderArray.length * 20;
panel.style.height = h + "px";
var table = document.createElement('table');
table.cellSpacing = 0;
//table.style.position = "absolute";
table.className = "table1";
table.appendChild(tbody);
panel.appendChild(table);
var element = document.getElementById("Grid");
element.appendChild(panel);
this.render();
}
function handleScroll() {
var contentdiv = $("#Grid");
scrollTop = contentdiv.scrollTop();
var vScrollDist = Math.abs(scrollTop - prevScrollTop);
if (vScrollDist) {
vScrollDir = prevScrollTop < scrollTop ? 1 : -1;
this.render();
prevScrollTop = scrollTop;
}
}
function render() {
renderedrange = this._getRenderedRowRange();
var datarange = orderArray.slice(renderedrange.top, renderedrange.bottom);
var margin = ((renderedrange.top - visiblerange.top)*20) +
(renderedrange.top * 20) +
-temp +
"px";
console.log("VisibleRange: " + visiblerange.top + " " + visiblerange.bottom);
console.log("RenderedRowRange: " + renderedrange.top + " " + renderedrange.bottom);
console.log(margin);
$(".table1").css({ "top": margin });
this._render(renderedrange);
}
function _render(range) {
if (!range)
return;
console.log("Top: " + range.top + "Bottom: " + range.bottom);
for (var i = range.top; i <= range.bottom; i++) {
if (i > orderArray.length - 1) {
return;
}
if (rows[i] != null)
continue;
var vals = orderArray[i];
var tr = document.createElement('tr');
tr.className = "rowcell";
tr.style.height = "20px";
tr.height = "20px"
var cell1 = document.createElement("td");
cell1.style.border = "0px solid black";
cell1.textContent = vals["CustomerID"];
cell1.style.height = "20px";
tr.appendChild(cell1);
var cell2 = document.createElement("td");
cell2.style.border = "0px solid black";
cell2.textContent = vals["CustomerName"];
cell2.style.height = "20px";
tr.appendChild(cell2);
tbody.appendChild(tr);
rows[i] = i;
}
}
function _getRenderedRowRange() {
visiblerange = this._getVisibleRowRange();
var range = {};
var topIndex = visiblerange.top;
var bottomIndex = visiblerange.bottom;
extend = Math.round(300 / 20);
var minExtend = 3;
if (vScrollDir == 1) {
topIndex -= minExtend;
bottomIndex += extend;
} else {
topIndex -= (extend - minExtend);
bottomIndex += minExtend;
}
topIndex = Math.max(0, topIndex);
bottomIndex = Math.min(orderArray.length, bottomIndex);
range = { top: topIndex, bottom: bottomIndex };
return range;
}
function _getVisibleRowRange() {
var coeff = scrollTop / (20 * orderArray.length);
top = (coeff * orderArray.length);
temp = (top % 1) * 20;
var topIndex = Math.floor(top);
var bottomIndex = Math.ceil(this._getrowposition(scrollTop + 300));
topIndex = Math.max(0, topIndex);
bottomIndex = Math.min(orderArray.length, bottomIndex);
var rang = { top: topIndex, bottom: bottomIndex };
return rang;
}
function _getrowposition (y) {
return Math.floor((y) / 20);
}
</script>
</body>
你的问题毕竟不清楚。
无论如何,如果要将表放置在div内,请将表位置值设置为相对于父div
style="position:relative;"
这样,您的表将具有相对位置您的问题毕竟不清楚。
无论如何,如果要将表放置在div内,请将表位置值设置为相对于父div
style="position:relative;"
这样,您的表将具有相对位置感谢您的回答,前提是解决方案对我有效。同样,我在滚动时也遇到了一些问题,我们无法在定义上边距位置时实现平滑滚动,我尝试创建JS小提琴来解释这个问题。但我无法创建JS小提琴。是否有任何方法可以实现平滑滚动,在这种情况下,在执行滚动时表格将发生抖动(由于顶部的边距发生变化)。我认为这个问题来自您的javascript代码,请在完成滚动代码后尝试返回false,可能会有所帮助。谢谢您的回复,我尝试过在On Scroll事件中使用sugested code return false with,但仍然存在相同的问题。除了更新表格的上边距以达到要求外,还有其他方法吗?请将代码发送给我,以便我可以帮助您解决问题。您可以使用jQuery特效,这样您的滚动运动将更加平滑和用户友好。感谢您的回答,前提是解决方案对我有效。同样,我在滚动时也遇到了一些问题,我们无法在定义上边距位置时实现平滑滚动,我尝试创建JS小提琴来解释这个问题。但我无法创建JS小提琴。是否有任何方法可以实现平滑滚动,在这种情况下,在执行滚动时表格将发生抖动(由于顶部的边距发生变化)。我认为这个问题来自您的javascript代码,请在完成滚动代码后尝试返回false,可能会有所帮助。谢谢您的回复,我尝试过在On Scroll事件中使用sugested code return false with,但仍然存在相同的问题。除了更新表格的上边距以达到要求之外,还有其他方法吗?您能给我发送代码吗?这样我就可以帮助您解决问题了。您可以使用jQuery效果,这样您的滚动运动将更加平滑,用户友好。