How to reduce table width when removing columns

How to reduce table width when removing columns

gre99orygre99ory Posts: 1Questions: 1Answers: 0

I'm using "colvis" in the Buttons extension.

There are many columns on my table. When I hiden a column using colvis, the availble space is reused and the table keeps its initial size.
Is there a way that the removing of a column will reduce the width of the table ?

Regards,

Answers

  • rf1234rf1234 Posts: 3,004Questions: 87Answers: 421

    something like this could work: Changing the table width using CSS. $('.container') is the data table in this case. If you have it differently change that please.
    Assuming that you want full-screen width when all columns are visible. columns.adjust and responsive.recalc are only to be used if you are using the responsive extension.

    table.on('column-visibility', function (e, settings, column, state) {
       var totalCols = table.columns().count();
       var visibleCols = table.columns( { visible: true } ).count();
       var percent = visibleCols / totalCols * 100;
       var css = { 'width': percent + '%' };
       $('.container').css(css);
       table.columns.adjust()
            .responsive.recalc();
    });
    
  • allanallan Posts: 63,572Questions: 1Answers: 10,482 Site admin

    https://live.datatables.net/sulidoke/1/edit

    Disable the autoWidth option and also make the table's width auto.

    Allan

Sign In or Register to comment.