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.