In a datatable, more than 10 records show pagination, otherwise do not display pagination using data

In my website, I use data tables to display data. Now the problem is that there are 10 entries than the displayed page is not displayed by default, but if there are more than 10 entries, a breakdown on the data tables should be displayed.

This is how I initialize a datatable

$(document).ready(function(){ $('#tbl_member').dataTable({ "iDisplayLength": 10, "bAutoWidth": false, "aoColumnDefs": [ {"bSortable": true, "aTargets": [0,2]} ] }); }); 

This data code, when I was doing the server side processing: -

 var save_method; var table; $(document).ready(function() { table = $('#table').DataTable({ oLanguage: { sProcessing: "<img src='<?php echo base_url();?>assets/img/loader.gif'>" }, "processing": true, "serverSide": true, // Load data for the table content from an Ajax source "ajax": { "url": "<?php echo base_url();?>Technology/technology_list", "type": "POST" }, "columnDefs": [ { "targets": [ -1 ], "orderable": false, }, ], }); }); 
+5
source share
1 answer

Use bPaginate (the old Hungarian style) or paginate to enable or disable pagination. You can use expressions to define parameters:

 $('#tbl_member').dataTable({ "bPaginate" : $('#tbl_member tbody tr').length>10, "iDisplayLength": 10, "bAutoWidth": false, "aoColumnDefs": [ {"bSortable": true, "aTargets": [0,2]} ] }); 

This works both in data versions 1.9.x and in 1.10.x. The demo shows two tables with one with fewer than 10 entries and the other with a lot more → http://jsfiddle.net/t2xcfLap/3/


Hide pagination controls after updating AJAX. Assuming the JSON response is in the form

 { "draw": 1, "recordsTotal": 3, "recordsFiltered": 3, "data": [ [...], ] } 

then

 table.on('xhr', function(e, settings, json, xhr) { if (json.recordsTotal<10) { $("#example_paginate").hide(); $("#example_length").hide(); } else { $("#example_paginate").show(); $("#example_length").show(); } }) 

demo → http://jsfiddle.net/yyo5231z/

The controls entered are named in the form <tableId>_length , <tableId>_paginate . Therefore, if your table has an id table , then the above should be $("#table_paginate").hide(); etc.

The reason for the different approach compared to the first answer using a static table is that you cannot change pagination "on the fly" without reinitializing the table.

+8
source

Source: https://habr.com/ru/post/1235015/


All Articles