Home > Back-end >  How to get columns names of jquery:datatable where the columns are created dynamically?
How to get columns names of jquery:datatable where the columns are created dynamically?

Time:02-02

I have a jquery:datatable in my code where its columns are created dynamically in the C# based on some table records in SQL.

In this datatable, I use a custom button called "Save". I need to get the column names of the table here as I get the data in the rows but I couldn't find the correct syntax to get the column names.

...
text: 'Save',
action: function (e, dt, node, config) {
          var json = JSON.stringify(dt.rows().data().toArray());
          // how to get the columns??
          // probably I need to use dt.columns().header() at some point?
        }
...

I believe I need to use dt.columns().header() as it gives me tags with a lot of info, not sure how I can retrieve column name over there.

Any help would be appreciated.

CodePudding user response:

With the method

table.columns().names();

CodePudding user response:

I've found my own answer. Here it is:

var cols = ""

dt.columns().header().each(function (e, i) {
       var col = jQuery(e).html();
       cols  = col   ",";
});

Thanks.

  • Related