我有一个非常相似的问题this one https://stackoverflow.com/questions/24325786/table-columns-is-not-a-function-in-datatable-js.
我创建一个数据表,然后用户可以使用搜索功能,单击按钮,所选数据将被发送到另一个函数,在其中进行进一步处理。初始化表工作正常,也发送选定的数据按预期工作 https://stackoverflow.com/questions/33169649/how-to-get-filtered-data-result-set-from-jquery-datatable。但是,我无法正确访问列名。
这是我使用的数据表版本:
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js" type="text/javascript"></script>
一些背景知识:
通过点击链接(#calculate
),创建了一个表,并且列标题看起来不错,例如像这样:
然后用户可以选择条目(此处88
),然后使用第二个链接(#send_data
)。用于初始化列标题的数据如下所示(取自console.log(data.columns);
):
[…]
0: Object { name: "C1", title: "C1", sName: "C1", … }
1: Object { name: "C2", title: "C2", sName: "C2", … }
length: 2
__proto__: Array []
我还可以访问table.columns()
,但是,当我尝试table.columns().names()
or table.columns().title()
我收到
类型错误:table.columns(...).names 不是函数。
如何访问和传递显示的列标题,即内容是什么col_names
在下面的代码中?
相关代码如下:
<script type="text/javascript">
$(document).ready(function() {
var table = null;
$('#calculate').bind('click', function() {
$.getJSON('/_get_table', {
a: $('#a').val(),
b: $('#b').val()
}, function(data) {
console.log(data.columns);
$("#elements").text(data.number_elements);
if (table !== null) {
table.destroy();
table = null;
$("#a_nice_table").empty();
}
table = $("#a_nice_table").DataTable({
data: data.my_table,
columns: data.columns
});
});
return false;
});
$('#send_data').bind('click', function() {
//console.log(table.columns());
//console.log(table.columns().title());
//console.log(table.columns().names());
$.getJSON('/_selected_data', {
sel_data: JSON.stringify(table.rows( { filter : 'applied'} ).data().toArray()),
col_names: //what goes here?
}, function(data) {
alert('This worked')
});
return false;
});
});
</script>