Add or remove column dynamically

Add or remove column dynamically

dspoonia7dspoonia7 Posts: 16Questions: 0Answers: 0

Hi Alan,

Is it possible in latest version 1.10.11, to add / remove columns from the table dynamically.

Replies

  • allanallan Posts: 63,451Questions: 1Answers: 10,465 Site admin

    You can chance a column's visibility dynamically, but you can't actually add and remove columns I'm afraid.

    Allan

  • dspoonia7dspoonia7 Posts: 16Questions: 0Answers: 0
    edited April 2016

    Thanks Alan for quick reply.

    But I have like hundreds of columns and user usually picks only 10 columns at once. So instead of loading all the columns in browser and make it slow, what i have done is I have custom show/hide column settings. When user update columns, I refresh the table with new columns. Will try some more things.

    I could have used server side rendering but that is too complicated in backend side :P

    Thanks anyways!!

  • dspoonia7dspoonia7 Posts: 16Questions: 0Answers: 0

    Hey Alan

    Can I use https://datatables.net/reference/api/destroy() method to make it work?

    I need to render different set of columns without reloading the page. Is that possible somehow?

  • allanallan Posts: 63,451Questions: 1Answers: 10,465 Site admin

    Yes - destroy() will work. You need to destroy the old table and then create the new one and initialise that.

    Adding and removing columns dynamically from a DataTables during "runtime" is not possible (I should have qualified that above).

    Allan

  • dspoonia7dspoonia7 Posts: 16Questions: 0Answers: 0

    Hey Alan, finally did it.

    Followed a fiddle solution https://jsfiddle.net/cr78t379/3/

    if ( $.fn.DataTable.isDataTable( '#table' ) ) {
    $('#table').DataTable().destroy();
    $('#table').empty();
    };

    Thanks!!

  • allanallan Posts: 63,451Questions: 1Answers: 10,465 Site admin

    Super. Thanks for posting back with your solution!

    Allan

This discussion has been closed.