views:

34

answers:

1

I use jquery pagination plugin, unfortunately it doesn't have method to set current page from outside the object. I've really looked into this object (the function is not long), but as a jquery beginner I can not even find how the event binds (by click obviously). I have no idea how to modify this function so I can call something like: current_page(10) to set current page to 10. I have everything else working for history (using the change hash event), back now loads previous page OK, but visually the paginator doesn't move or change active page of course. I really need a trigget to make this happen.

I would be very happy if someone at least points me to the part of the function where I can modify this...

+1  A: 

The pagination plugin actually does have this built-in, it's just in an unconventional and not-obvious way, the key is this bit:

var panel = jQuery(this);
// Attach control functions to the DOM element 
this.selectPage = function(page_id){ pageSelected(page_id);}

This creates a .selectPage() method on the DOM element the pagination links are in, for example in their demo page you'd call it like this:

$("#Pagination")[0].selectPage(9); //0-based, 9 = page 10

You can give it a try here, the setup is just replicating the demo page, but literally all I've added is the .selectPage() call above. In the interest of full disclose there's also .prevPage() and .nextPage() functions available in the same way, e.g. $("#Pagination")[0].nextPage();

Nick Craver
You are INCREDIBLE! I study his plugin for a week and I wan't able to achieve anything :-( And you tell me now this... WOW! Man you are good...Can you please tell me what is [0] here? I understand the page, but what is 0? I have 5 paginations on my page so I guessed 0 is one of them (the first), but the container ID should define them, shouldn't it? I even tried triggering click from my function but didn't work ;-) Jerry
Jerry2
@Jerry2 - `[0]` gets the first **DOM element**, you could do `document.getElementById('Pagination').selectPage(9)` for example. The important thing is this isn't a jQuery method, but a property/method on the DOM element the pagination was created on. If you had a link to your page, I could show you the right syntax.
Nick Craver
Wierd, it is not working to me. I have to use [0] as other values say undefined. Maybe 5 paginatiors on a page is a problem. I use the hashchange plugin to detect change of a hash and in case it is changed I'd like to set a new page (maybe browser back is pressed). Seems the setpage function also triggers callback for the paginator, and that I don't want. All I am trying to do is a history support for paginator :-8
Jerry2
Is there a way to send you the private message with a link in Stack overflow?
Jerry2
@Jerry2 - The callback is how it refreshes the item list to be the correct items shown...I'm curious as to why you *wouldn't* want this? It seems that'd make the page links not match the displayed content. The 5 paginators shouldn't be a problem, do they have unique IDs, or all the same class?
Nick Craver
@Jerry2 - There isn't, the creators weren't after a social network :)
Nick Craver
Unique ID's yes... Well, this is all a long story I am pulling my hear out for a month. I hate people click back and don't get old contents. i found out that back in browser initiates the page as a refresh, so I have added #hash support (kind of). On callback I load new page. On hash change i can see the hash and setpage that is correct (if we want to go back), but that makes 2 laods instead of one if I click the paginator (first load is a callback second one is a hash change), and on top of that the page number is undefined now I called your function.
Jerry2
It is very mixed up. This is if beginner want's to make a history with paginator. I have idea now for every load to hide it in a div once it is loaded and somehow check when user clicks on the old page or goes back, just to show that div and hide the rest, that would be good, but I am weeks of that...
Jerry2
@Jerry2 - taking a look, you can remove the comment :)
Nick Craver
Ok, as it is it should load contents twice, but it should load the same page not NaN which I guess means undefined. I don't get it why selectPage function would act differently than clicking on link (that works). Thank you for taking the time, really appreciate it.
Jerry2
I may not know much jquery and javascript yet, but it seems to me calling the selectPage is NOT the same as clicking on paginator link. It seems that thecallback doesn't have the current_page variable with it, which spoils everything as hashchange is triggered again this time with NaN as page number.
Jerry2
@Jerry2 - The problem as I said is that you haven't defined `varPage` so when you do `current_page: parseInt(varPage)`, `parseInt` gets NaN, you need to define what `varPage` is, I assume you want the page number from the hash there...but currently it's `undefined`, that's the root issue :)
Nick Craver
@Jerry2 - Also take a look here and validate your page, there are many markup bugs that may cause all sorts of weird behavior even after all the script is perfect :) http://validator.w3.org/
Nick Craver
thanx fot he tip. So initPagination2 is called after EVRY setPage? I thought it is only called once ;-(
Jerry2
@Jerry2 - well you're changing the hash, and you've bound an event to that that runs `.pagination()` on all the elements again, *that's* what's calling it :)
Nick Craver
So this: $("#forum-pagination")[0].selectPage(page_no); doesn't only select current page, it INITIALIZES the pagination object again? this is not the same as if I click the link in paginator :-( Clicking link only set's current page. The selectPage seems to reinitialize the plugin and then it doesn't work.I need a function to just change the current page.PS - I have tested, the initPagination2 is run just at load time, not after. Something else must be calling the NaN.
Jerry2
@Jerry2 - You're not listening, `varPage` is **undefined**, you're setting the current page to an **undefined** value, this will cause all sorts of issues, including breaking the plugin and all it's methods, you have to create and have `varPage`. *You're* the one initializing the plugin again with your `onhashchange` handler, you need to remove it there and *only* call `selectPage()`, not `.pagination()` all over again. Even still you don't have the page you *want* to go to there, I presume that's what `varPage` is, but you never create that variable.
Nick Craver
Thanx, but I do create it on page initialize:initPagination2(stran); stran is passed to the init function as varPage. It works if I don't use function: $("#forum-pagination")[0].selectPage(page_no); so the varPage is defined. What happens seem to be that the selectPage initializes the paginatior AGAIN and this time I don't have the variable.
Jerry2
onhashchange does not initialize the plugin, it only loads the contents and what I was trying to do unsucessfully is find a way to render paginator again so the selected page would be the proper value I give it.
Jerry2