我目前正在解析一個xml文件,並獲取內容顯示在一個5列的表和x行數取決於項目的數量是在xml文件中。不幸的是,一切都出現在頁面上,我需要在我的JavaScript中實現分頁以處理來自xml文件的數百條記錄。我想每頁50個項目或每列5列20行。下面是我的代碼至今:javascript和xml分頁
的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>
眼下這是工作的,因爲它會顯示所有記錄在正確的行/列中。我只需要用我迄今爲止的分頁來實現分頁。有任何想法嗎?
不創建一個適當的'XMLHttpRequest',應該使用'onreadystatechange'事件來再檢查'如果(xmlhttp.readyState == && xmlhttp.status == 200)'。另外,你的'xmlhttp'變量是全局變量。 – PHPglue