Figuring out row numbers for pages after pagination

Dec 10, 2014 at 1:55 PM
OK, I finally got pagination to work, ... it seems. I'm now wondering how you figure out the list's numbering. For example, if I view page 1, and I have a record limit of 25, I want it to number them 1 to 25 on the left. If I then go to page 2, I would like it to number them 26 to 50. I can easily create a loop or variable that does the count based upon what page number you are on, but I'm not certain how to figure out what page I'm on based upon the "<QueryOptions><Paging ListItemCollectionPositionNext=" variable passed in. If I could discern the page number I can go from there. All I'm certain of is that, to get to page 1, a variable of null or "" gets you there. Is there a place for documentation on this, and how can I go about figuring out what page I'm on?

Thank You
Dec 10, 2014 at 4:22 PM
Maybe nevermind.

I figured out that as I'm stepping through the code that builds the page index at the bottom, it can increase the page count. I can then go from there.

The code I have is:
function buildNav(rowLimit) {
    // Unbind old objects
    $("libraryPrev a").unbind('click');
    $("libraryNext a").unbind('click');
    $(".pager a").unbind('click');
    // Clear current nav
    $("#libraryPages").html("");

    var recordCount = 0;
    var firstPos = "";

    // execute once to get all items
    // not using rowLimit to return all items
    $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: list,
        CAMLViewFields: camlFields,
        CAMLQuery: camlQuery,
        completefunc: function (xData, Status) {
            recordCount = parseFloat($(xData.responseXML).SPFilterNode("rs:data").attr("ItemCount"));

        }
    });

    // execute again to retrieve first position
    // this uses rowLimit
    $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: list,
        CAMLViewFields: camlFields,
        CAMLQuery: camlQuery,
        CAMLRowLimit: rowLimit,
        completefunc: function (xData, Status) {
            firstPos = $(xData.responseXML).SPFilterNode("rs:data").attr("ListItemCollectionPositionNext");
        }
    });

    if(eval(recordCount > rowLimit)) {
        $("#libraryPages").append('<div id=\'library-prev\'><a href="#">Prev</a></div>');

        var pages = Math.ceil(recordCount / rowLimit);
        var nextPos = "";

        for (i=1; i <= pages; i++) {
            if (i == 1) {
                $("#libraryPages").append("<div id='row-" + i + "' class='pager'><a class='selected' href='javascript:getItems(\"\",\"" + rowLimit + "\")'>" + i + "</a></div>");
            } else {
                nextPos = getNextPos(nextPos, rowLimit, camlQuery);
                    $("#libraryPages").append("<div id='row-" + i + "' class='pager'><a class='selected' href='javascript:getItems(\"" + nextPos + "\",\"" + rowLimit + "\")'>" + i + "</a></div>");
            }
        }

        $("#libraryPages").append('<div id=\'library-next\'><a href="#">Next</a></div>');
    }
}
the 'i' in the for loop gives me the page number. I can go from there I think.
Dec 10, 2014 at 4:26 PM
No, darn it, I just realized that's not going to do it. I have to find out how this code is working:
function getNextPos(pos, rowLimit, camlQuery) {
    try {
        pos = pos.replace(/&/g,'&amp;').replace(/"/g,'&quot;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
    }
    catch(e) {
    }

    $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: list,
        CAMLQuery: camlQuery,
        CAMLQueryOptions: "<QueryOptions><Paging ListItemCollectionPositionNext='" + pos + "' /></QueryOptions>",
        CAMLRowLimit: rowLimit,
        completefunc: function (xData, Status) {
            pos = $(xData.responseXML).SPFilterNode("rs:data").attr("ListItemCollectionPositionNext");
        }
    });

    return pos;
}
I can't seem to find any documentation on the ListItemCollectionPositionNext. Can you help Marc, or anyone else?

Thank You,
Dec 10, 2014 at 5:21 PM
OK,I might be getting somewhere thanks to Marc's article, http://sympmarc.com/2013/10/16/single-page-applications-spas-in-sharepoint-using-spservices-part-2-getlistitems/, and link in it pointing to MSDN, http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems.aspx

I'll keep at it. Any quick summation by anyone would help, though.
Dec 10, 2014 at 9:21 PM
Edited Dec 10, 2014 at 9:27 PM
OK, I did it the hard way. Just by stepping through. :)
function getPageNum(pos, rowLimit) {
    try {
        pos = pos.replace(/&/g,'&amp;').replace(/"/g,'&quot;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
    }
    catch(e) {
    }

    var tryPos = "";
    var pageNum = 1;
    var notFound = true;

    while (pos != "" && notFound) {

        pageNum++;

        tryPos = tryPos.replace(/&amp;amp;/g,'&').replace(/&quot;/g,'"').replace(/&lt;/g,'<').replace(/&gt;/g,'>');

        tryPos = getNextPos(tryPos, rowLimit, camlQuery);
        tryPos = unescape(tryPos);
        tryPos = tryPos.replace(/&/g,'&amp;amp;').replace(/"/g,'&quot;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
        if (tryPos === pos) {
            notFound = false;
        } else {
            notFound = true;
        }
    }

    return pageNum;
}
This gives me the page number to multiple the rowLimit by (minus one on the pageNum) to get the count start, and then just go from there.

If anyone knows a faster better way, please reply.

Thank You
Marked as answer by trout0525 on 12/10/2014 at 1:27 PM
Dec 11, 2014 at 2:54 AM
That's how I do it. I keep an object with Token=pageNumber, and then use that to figure out what page I'm displaying. If I get a token not yet in the object, the it's a new page. You then have what you need to calculate the row numbers.



--
Paul T.

-- Sent from Mobile