1

I don't have much experience with Javascript, Node.js or Socket.io. I'm trying to create an app with a jQuery pagination plugin, http://beneverard.github.com/jqPagination/, that queries a mysql database (with keywords that the user enters into a textbox), gets the total number of results back along with the first 15 results for the first page (using LIMIT) then uses the total number of results to determine how many pages will be needed.

Here is the code snippet (client-side javascript for setting the maximum number of pages for pagination) that I can't get to work:

var maxpagenumber;

socket.on('resultsnumber', function(count)
{
    maxpagenumber = ~ ~(this.count / 15) + 1;
});

$('.pagination').jqPagination({
    max_page: maxpagenumber,
    paged: function(page)
    {
        ...
    }
});

I looked at the variable maxpagenumber in Chrome and noticed that it is undefined. The query for the first 15 results works it's just that the pagination only shows one maximum page.

As I was looking around online for help I found a question on accessing a variable outside an asynchronous callback function: Using variable outside of ajax callback function. I used the idea from the marked answer and applied it to my code:

var setPagination = function(maxpagenum)
{
    $('.pagination').jqPagination({
        max_page: maxpagenum,
        paged: function(page)
        {
            ...
        }
    });
}

socket.on('databasenumber', function(count)
{
    var maxpagenumber = ~ ~(this.count / 15) + 1;
    setPagination(maxpagenumber);
});

This gives the same result as the previous code snippet. The only working option I have so far is this:

socket.on('databasenumber', function(count)
{
    var maxpagenumber = ~ ~(this.count / 15) + 1;
    $('.pagination').jqPagination({
        max_page: maxpagenumber,
        paged: function(page)
        {
            ...
        }
    });
});

This works perfectly until the user enters new keywords into a textbox to search again. Once the user does a second search many bugs arise. Anyone have any ideas on how to make this work? Should I show more code to explain my problem? Maybe I'm just not understanding this snippet of Javascript properly? Any help or advise would be much appreciated.

Community
  • 1
  • 1
Gabe Martin
  • 61
  • 2
  • 10

1 Answers1

0

The problem seems to be that you're only setting the maxpagenumber once. You really need to set that each time your dataset changes. I think some combination of your second set reworked will be best, but I'm not 100% familiar with the jqPagination control, so that is probably where things aren't working out. You may need to feed it a new maxPageNum via some API call?

jcolebrand
  • 15,889
  • 12
  • 75
  • 121
  • Thanks for the response! I ended up going down a different route because the jqPagination plugin doesn't have the desired functionality to dynamically change the maxpagenumber. I just decided that for the purpose of the web application that I'm creating I could get away without a maximum number of pages. Should I mark your answer as accepted? Thanks again! – Gabe Martin Aug 03 '12 at 02:53
  • If you like, or ask the question to be deleted (I think you can just delete it yourself) if you don't think it will ever help anyone ever again. I have a feeling it will, however, help people in the future, as it helped you here. – jcolebrand Aug 03 '12 at 15:34