mpsbhat
mpsbhat

Reputation: 2773

Enable disable fixed column in DataTable using a button

Myself created a html table with fixed columns using jquery DataTable with reference to This example as,

$(document).ready(function() {
    var table = $('#example').DataTable( {
        scrollY:        "300px",
        scrollX:        true,
        scrollCollapse: true,
        paging:         false
    } );
    new $.fn.dataTable.FixedColumns( table, {
        leftColumns: 2
    } );
} );

UPDATE: Myself trying to enable/disable the fixed column using a button as given in this fiddle as,

var columnNumber = 2;
  $('#ToggleColumns').click(function () { 
  if(columnNumber == 2)
  {
columnNumber = 0;  
  }
  else {
  columnNumber = 2; 
  }
foo();

where the foo() contains the columnNumber to be fixed. What is the correct syntax to toggle the column number based on the button?

Upvotes: 0

Views: 2512

Answers (3)

Mahesh Dama
Mahesh Dama

Reputation: 103

For me, it worked.

$(".DTFC_Cloned").addClass('d-none');

.DTFC_Cloned is a class datatables gives to a cloned table with fixed column data.

Upvotes: 0

mpsbhat
mpsbhat

Reputation: 2773

It is solved by using table.destroy(); method of datatable and initializing new options with new columnNumber.

Sample Link: http://jsfiddle.net/eqsadgez/1/

Please suggest if any better ways.

Upvotes: 1

BPT
BPT

Reputation: 36

I think all you can do is add or remove "sorting" class on each click

Upvotes: 0

Related Questions