skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. With the method

    table.columns().names();

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search