Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show and hide columns of using datatable jquery

I need to show and hide the columns of the datatable after doing a javascript test, but it doesn't work good.

This is my test javascript:

if ( $('#commune_to_display').val()==""){
    $('#utable td:nth-child(2)').hide();
    $('#commune_to ').hide();
}

This test works just in the first page of the datatable, after pagination the column is still visible.

How can I fix it? thanks for help

like image 750
youssef hassoun Avatar asked Jul 25 '17 11:07

youssef hassoun


People also ask

How do I hide and show columns in DataTable?

To hide and show columns use columns() and visible() method. Call it on dataTables instance and pass column index in columns() method and false to visible() method. Similarly, pass true to visible() if you want to show the columns.

How to hide the column in DataTable?

DataTable(); //hide the first column dt. column(0). visible(false); To hide multiple columns, columns().

What is Aocolumns in jquery DataTable?

aoColumnDefs: This array allows you to target a specific column, multiple columns, or all columns, using the aTargets property of each object in the array (please note that aoColumnDefs was introduced in DataTables 1.7).

How do you get the dynamic column header and result from Ajax call in jquery DataTable?

how to get the dynamic column header and result from ajax call in jquery datatable. var $table=$('#MSRRes'). dataTable( { "bFilter": false, "bDestroy": true, "bDeferRender": false, "bJQueryUI": true, "oTableTools": { "sSwfPath": "swf/copy_cvs_xls_pdf.


1 Answers

You can show/hide columns as shown below. Just replace 3 and 4 with your actual column indexes.

var table = $('#utable').DataTable();
table.column(3).visible(true);    // To show
table.column(4).visible(false);   // To hide
like image 185
jignesh patel Avatar answered Oct 04 '22 05:10

jignesh patel