I have a script that paginates pages in a table, which works fine. I also have a dropdown to select how many records to show in the table. Although this works, if I select say 20 records the pagination does not update to reflect the change.
When the table is run, the default records to show is 8 whih makes 9 pages. But if I select 20 in the drop down it still says 9 and if I go to page 9 it is obviously empty. I am also wondering if it is possible to have the script appear in both before and after the table. the reson for this is if a user gets to the bottom of say 100 records, then I would like this feature so they don’t have to go all the way back to the top.
I would appreciate any help as my javascript is not 100%. Many thanks
PS: From what i can gather, i have to re-rendering the inside the repaginate() function. Keep the<div class="pager"> where it is but move the bit that renders the actual pages inside the repaginate() function. Tried various options but not working. Any idea how I can do this as I am really struggling with it. Thanks
Oh sorry, my brain didnt actually read all of the OP because i’m a derp. Forgive me.
The code as listed doesnt actually have anything to renumerate the pages, but it should be easy enough to do… i’d wrap the for block on line 13 of the javascript into a function, something like ‘drawpages’. Take the numPages calculation with it. Call it on line 13 (so it still does the initial setup), and then also call it in the dropdown trigger, after expunging the current elements with a .remove()…
Just in the interests of practising for fun I had a go at this and the result is below.
I was mainly concerned in getting something to work firstly and the next step would be to tidy up the code as frankly its a bit of a mess. I’m sure one of the JS experts here could tidy it up considerably or indeed offer a better method to do the pagination?
Thanks very much. Works a treat. Just one thing. When the page first loads, the dropdown is above the number of pages. However, when you select an option, it drops below the number of pages. Is this an easy fix. Also, would it be possible to have the dropdown the same as the pages top and bottom. Many Thanks
Hey Paul, sure thing. I’m in the middle of reinstalling my OS right now, so won’t be around for a couple of days, but I’ll check back in when I’m done and would be glad to have a look at the code
I had a look at your code, Paul and the thing I missed most was some kind of control flow — i.e. I wasn’t really able to read the code and have an idea of what was happening where. This means that when you revisit it in a few months, nor will you
To that end, I rewrote things a little. You’ll notice I’m making use of ES6.
It still needs some tweaking and is not as feature complete as your version, but hopeflly the rest shouldn’t be too hard to add back in. I would also consider adding a query string to the pagination links in case the user wants to link to a section of the table.
As a final thought, if JS isn’t really the OP’s bag, it might be an idea to look at one of the plugins out there that does this kind of thing for you.
Thanks @James_Hibbard that’s very useful and I will work my way through it and try to improve the way I code js. I do tend to just try and make things work first and does often seem like magic later on
I believe the OP has moved on now with more help in the JS forum but for learning purposes the revision will be very useful.
No worries Paul, I was just having a look at this now and have made a couple of tweaks so that it works with multiple tables and adds an active class to the current pagination link.
I hear ya, but I got bitten by that so often, that regardless of how messy the rest of the code is, I always try to have some kind of structure that one can follow to have a rough idea of what is going on where. Even if it’s just:
$(() => {
function do_this() { ... }
function do_that() { ... }
function the_other() { ... }
do_this()
do_that()
do_the_other()
});