Hide column in jQuery dataTables?

I want to hide a column in jQuery DataTablescontaining Geo Zone in th. This is what I do:

$(document).ready(function(){

        if(geo_zone_on_off==0){
            var _index=$("#datatable_ajax .heading th:contains(GeoZone)").index();
            var oTable=$("#datatable_ajax").DataTable();
            if(_index != -1){
                 oTable.column(_index).visible(false);
     }
        }
    });

A dataTable is loaded, but the column is not hidden. Before doing this, I tried to hide it when the table was processed and it worked fine. Then I did the following:

 "initComplete": function(settings, json) {
                       if(geo_zone_on_off==0){
                        var _index=$("th.sorting:contains(GeoZone),th.no-sort:contains(GeoZone)").index();

                           if(_index != -1){
                             grid.getDataTable().column(_index).visible(false);
                           }
                       }
                       },

But he had a problem with displaying hidden columns when loading a table. To avoid this problem, I used the solution mentioned above. But it does not work, although I understand the index correctly. This does not give any errors.

+4
source share
3 answers

. a name. visible ?

columnDefs: [
  { targets: <index>, name: 'geozone', visible: geo_zone_on_off == 1 }
]

, , , :

table.column('geozone:name').visible(false);

table.column('geozone:name').visible( geo_zone_on_off == 1 );

โ†’ https://datatables.net/reference/type/column-selector

+1

, th.

-

$('table').DataTable();
    $('button').on('click',function(){
        $('th').each(function(i,e){
        if($(this).text()=='No') {
         $(this).hide();
         $('tr').each(function(){
          $(this).find('td').each(function(index,element){
            if(index==i) {
              $(this).hide();
            }
          });
         });
        }
      });
    });

0

Get Datatable Object

var table = $('#table').DataTable();

Get target column for visibility change

var target = //Get target of column to hide for eg for third column target = 2
var column = table.column( target );

Visibility Change

column.visible( false );

DataTable Documentation

0
source

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


All Articles