Keep pagination on last page

Keep pagination on last page

xanabobanaxanabobana Posts: 12Questions: 4Answers: 0

Link to test case: https://dev.vmc.w3.uvm.edu/xana/climate_indicators/table
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

I added some custom code to make it so the pagination disappears when there are less than 10 rows on my table (found here: https://stackoverflow.com/questions/11050826/disable-pagination-if-there-is-only-one-page-in-datatables )

The issue is that if you have multiple pages of rows in the table and then navigate to the last page, the pagination disappears so there's no way to go back to the previous page. Any ideas on how to remedy this?

Thanks!

This question has an accepted answers - jump to answer

Answers

  • xanabobanaxanabobana Posts: 12Questions: 4Answers: 0

    This is the code I used, taken from that stackoverflow question:

         "fnDrawCallback": function(oSettings) {
                if ($('#studyTable tr').length < 11) {
                    $('.dataTables_paginate').hide();
                }
                if ($('#studyTable tr').length >= 11) {
                    $('.dataTables_paginate').show();
                }
            }
    
  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769

    Use the rows() API along with the count() API, something like this:

     if (table.rows().count() < 11) {
            $('.dataTables_paginate').hide();
           } else {
            $('.dataTables_paginate').show();
           }
    

    The reason your code doesn't work is only the rows shown on the page are in the DOM.

    Kevin

  • xanabobanaxanabobana Posts: 12Questions: 4Answers: 0
    edited May 2021

    I'm not sure how to use that? I get the error Uncaught TypeError: $(...).rows is not a function

    I'm putting it in the callback like this:

    "fnDrawCallback": function(oSettings) {
                  if ($('#studyTable').rows().count() < 11) {
           $('.dataTables_paginate').hide();
          } else {
           $('.dataTables_paginate').show();
          }
    
  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769
    edited May 2021

    You need an instance of the Datatables API. Try:
    $('#studyTable').DataTable().rows().count().

    Kevin

  • xanabobanaxanabobana Posts: 12Questions: 4Answers: 0

    Hm, that doesn't hide the pagination at all anymore, even when there are less than 11 rows

  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769
    edited May 2021

    The code seems to work here:
    http://live.datatables.net/nupokuse/1/edit

    The paging element is hidden. Comment out the first Datatable and uncomment the second and you will see that the paging element remains. If you still have issues please provide or update my test case to show the problem.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Note the use of this.api() in drawCallback. You can access the API this way in most of the Datatables Callbacks.

    Kevin

  • xanabobanaxanabobana Posts: 12Questions: 4Answers: 0
    edited May 2021

    If you use the #example2 table in your test case and search for "Ramos", only one entry shows up in the table but the pagination still shows. This is my issue- I'm filtering the table and want the pagination not to show when there are fewer than 10 entries.

  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769
    edited May 2021 Answer ✓

    You can use the selector-modifier of {search: 'applied'} with the rows() API, like this:
    http://live.datatables.net/nupokuse/4/edit

    Kevin

  • colincolin Posts: 15,143Questions: 1Answers: 2,586

    Yep, it just needs the search filter to be considered when counting the rows, see here: http://live.datatables.net/nupokuse/5/edit

    Colin

  • xanabobanaxanabobana Posts: 12Questions: 4Answers: 0

    that did it- thank you so much!

This discussion has been closed.