简体   繁体   English

如何获取 jquery 的列名称:动态创建列的数据表?

[英]How to get columns names of jquery:datatable where the columns are created dynamically?

I have a jquery:datatable in my code where its columns are created dynamically in the C# based on some table records in SQL.我的代码中有一个 jquery:datatable,它的列是根据 SQL 中的一些表记录在 C# 中动态创建的。

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.我相信我需要使用dt.columns().header() ,因为它为我提供了包含大量信息的标签,但不确定如何在那里检索列名。

Any help would be appreciated.任何帮助,将不胜感激。

With the method用方法

table.columns().names();

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.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM