Hide Pagination and Page Limits unless more than 1 page is loaded

Hide Pagination and Page Limits unless more than 1 page is loaded

jLinuxjLinux Posts: 981Questions: 73Answers: 75

I have DT all over my app, and since im still just developing it, a lot of the tables just have a few rows in it, and they all have the pagination buttons and the page limit dropdown. I was wondering, is there a way to have DT hide both of those, unless the minimum limit has been passed?

Meaning if the pagination is set to 10 rows per a page, and theres 10 or less rows, hide the two features.

I know I could use some JS to get the row count and disable the features, which I might do, was just wondering if this was already a feature that I overlooked.

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    P.S. I saw some posts about this in the forums, but they were from 2009, so thought id ask again.

    http://datatables.net/forums/discussion/131/disabling-pagination-navigation-when-returning-only-one-page-of-results

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited August 2015

    What I did was just use a callback to determine the settings based off of the rows and pagination settings...

    If not enough rows in the table to do anything (Meaning less than the minimum display length), hide filter, pagination, display length)
    Otherwise, toggle the pagination based on the current page length setting

    var $data_table = $( '#data-table' );
    
    // Initialize DataTable
    var $assets_dt = $data_table.DataTable( {
        lengthMenu: [
            [5, 10, 15, 20, 25, 30, 40, 50, -1],
            [5, 10, 15, 20, 25, 30, 40, 50, "All"]
        ],
        drawCallback:function(){
            var page_min = 5;
            var $api = this.api();
            var pages = $api.page.info().pages;
            var rows = $api.data().length;
    
            // Tailor the settings based on the row count
            if(rows <= page_min){
                // Not enough rows for really any features, hide filter/pagination/length
                $data_table
                    .next('.dataTables_info').css('display','none')
                    .next('.dataTables_paginate').css('display','none');
    
                $data_table
                    .prev('.dataTables_filter').css('display', 'none')
                    .prev('.dataTables_length').css('display', 'none')
            } else if(pages === 1){
                // With this current length setting, not more than 1 page, hide pagination
                $data_table
                    .next('.dataTables_info').css('display','none')
                    .next('.dataTables_paginate').css('display','none');
            } else {
                // SHow everything
                $data_table
                    .next('.dataTables_info').css('display','block')
                    .next('.dataTables_paginate').css('display','block');
            }
        }
    } );
    

    If theres a dynamic way to set page_min by getting the lengthMenu settings somehow via the API, that would be neat.. as opposed to having to change page_min every time the DT settings are changed.

    I know you can do something like...

    $('select[name="data-table_length"]').find('option').eq(0).val()
    

    But what if its not ordered in the right way, or ... other stuff

This discussion has been closed.