How to limit the number of pagination button.

How to limit the number of pagination button.

MuriloNsantos17MuriloNsantos17 Posts: 3Questions: 2Answers: 0

Hi, I have my table, this table has 10 pages with 10 rows each page.

But If i configure the pagination he as 10 buttons with 10 rows each page.

I want to limit the number of these buttons in 5 buttons something like this:

<code>
prev. 1. 2. 3. 4. 5. next.
</code>

If i press next once, the buttons should show like this:
<code>
prev. 2. 3. 4. 5. 6. next.
</code>

Or when the button five is the last the button selected and the next button is pressed, paginate make something like this:

<code>
prev. 6. 7. 8. 9. 10. next.
</code>

I'm using this function:
$.fn.DataTable.ext.pager.numbers_length = 5;

But this function create buttons ... (ellipsis) between the interval, but this is weird for the end user.

Best regards

Answers

  • kthorngrenkthorngren Posts: 15,787Questions: 25Answers: 3,739

    Maybe one of the Paging plugins will do what you want or help you to write your own.

    Kevin

  • MuriloNsantos17MuriloNsantos17 Posts: 3Questions: 2Answers: 0

    Thanks for you asnwer Kthorngren, I found the solution at other forum. I Will leave the funcion:

    /**
    * Plug-in offers the same functionality as simple_numbers pagination type
    * (see pagingType option) but without ellipses.
    *
    * See example for demonstration.
    *
    */

    $.fn.DataTable.ext.pager.simple_numbers_no_ellipses = function(page, pages){
    var numbers = [];
    var buttons = $.fn.DataTable.ext.pager.numbers_length;
    var half = Math.floor( buttons / 2 );

    var _range = function ( len, start ){
       var end;
    
       if ( typeof start === "undefined" ){ 
          start = 0;
          end = len;
    
       } else {
          end = start;
          start = len;
       }
    
       var out = []; 
       for ( var i = start ; i < end; i++ ){ out.push(i); }
    
       return out;
    };
    
    
    if ( pages <= buttons ) {
       numbers = _range( 0, pages );
    
    } else if ( page <= half ) {
       numbers = _range( 0, buttons);
    
    } else if ( page >= pages - 1 - half ) {
       numbers = _range( pages - buttons, pages );
    
    } else {
       numbers = _range( page - half, page + half + 1);
    }
    
    numbers.DT_el = 'span';
    
    return [ 'previous', numbers, 'next' ];
    

    };

Sign In or Register to comment.