我目前正在解析一个 xml 文件并获取要在 5 列和 x 行的表中显示的内容,具体取决于 xml 文件中的项目数。不幸的是,一切都出现在页面上,我需要在我的 javascript 中实现分页来处理来自 xml 文件的数百条记录。我在想每页 50 个项目或 20 行,每行 5 列。以下是我到目前为止的代码:
Javascript:
function generateTables(){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","xml/test_102.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var content = document.getElementById('content');
var x=xmlDoc.getElementsByTagName("vids");
var xmlContent = "<table class='table' id='videos' border='0'>";
var rows = parseInt(x.length/5 + .6)
var count = 0;
var z = 0;
//Pagination idea. We can get the x.length value which is the total tags that we have. We can divide that by 50 to get teh number of pages we have. Then we can simply simply each on a pge and we will knwo the # of pages in a variable.
//rows
for (i=0;i<rows;i++) {
//This will handle if there is an uneven amount of columns.
if ((count + 5) > x.length)
{
z = x.length - count
}
else
{
(z = 5)
}
xmlContent += '<tr>'
//columns
for (y=0;y<z;y++) {
var title = x[count].getElementsByTagName("title")[0].childNodes[0].nodeValue;
var vidPath = x[count].getElementsByTagName("vidPath")[0].childNodes[0].nodeValue;
var png = x[count].getElementsByTagName("pngPath")[0].childNodes[0].nodeValue;
var gif = x[count].getElementsByTagName("gifPath")[0].childNodes[0].nodeValue;
xmlContent += "<td align='center'>" + title;
xmlContent += "<br /><a href='"+ vidPath;
xmlContent+="'><img src='"+ png +"' width='50%'";
xmlContent+= "onmouseover=\"this.src='"+gif+"'\" onmouseout=\"this.src='"+png+"'\"/></a></td>";
count++
}
xmlContent += '</tr>'
}
xmlContent += "</table>";
//document.write(rows)
content.innerHTML = xmlContent;
}
这是我到目前为止的html:
<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My tables</title>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<script src="js/records.js"></script>
</head>
<body>
<div id="title">Page Title</div>
<div id="content"></div>
<script type="text/javascript">
window.onload = function() {
generateTables();
};
</script>
</body>
</html>
现在这正在起作用,它将在正确的行/列中显示所有记录。我只需要用我目前所拥有的实现分页。有任何想法吗?