简体   繁体   中英

How to add pagination to an XML feed?

I am using javascript to make XML to HTML. I have followed this guide:http://www.w3schools.com/xml/xml_to_html.asp

How do I add pagination to a XML feed?

based on the url you provided in your question, here is how you can paginate the data.

The html content:

<div id="content"></div>
<div id="pagination"></div>

The Javascript code:

var page = 1, perPage = 4, content = document.getElementById('content'),
pagination = document.getElementById('pagination'), records;

function paganation(page)
{
    var nextMaxItem = perPage * page;
    var fromItem = (page - 1) * perPage;
    var maxPages = records.length / perPage;

    var xmlContent = "<table border='1'>";
    for (var i = fromItem; i < nextMaxItem; i++) {
        xmlContent += "<tr><td>";
        xmlContent += records[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue;
        xmlContent += "</td><td>";
        xmlContent += records[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue;
        xmlContent += "</td></tr>";
    }
    xmlContent += "</table>";
    content.innerHTML = xmlContent;

    var paginationContent = "";
    var previous = page - 1;
    if (page > 1) {
        paginationContent += '<a href="javascript:paganation('+previous+');">Back</a>';
    } else {
        paginationContent += "Back";
    }

    for (var j = 1; j < Math.ceil(maxPages); j++) {
        paginationContent += " ";
        paginationContent += '<a href="javascript:paganation('+j+');">'+j+'</a>';
        paginationContent += " ";
    }

    var next = page + 1;
    if (next <= maxPages) {
        paginationContent += '<a href="javascript:paganation('+next+');">Next</a>';
    } else {
        paginationContent += "Next";
    }
    pagination.innerHTML = paginationContent;
}

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","cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 
records = xmlDoc.getElementsByTagName("CD");
paganation(1);

One option is the Ruby gem, will_paginate ( this page explains the basics of using it).

If you're developing in Rails 3, this blog post might prove useful.

If you need to paginate the XML output itself, regardless of the solution you choose, you will still have to break it into parts, so you will have some basis for breaking it into pages. In other words, deal with each row in your XML data as a distinct record, the same way a DB would deal with it, and paginate off of that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM