[英]Special characters in DataTable column names in JQuery/Javascript
EDIT : I have created this fiddle that shows the issue. 编辑 :我创建了这个小提琴 ,以显示问题。
I define my columns the following way (pseudo-code, please don't tell me to declare the variables) 我通过以下方式定义列(伪代码,请不要告诉我声明变量)
var cols = [];
cols.push({
data:theNameWithSpecialCharacters,
title:theNameWithSpecialCharacters,
render: $.fn.dataTable.render.number(",",".",3)
});
myDataTable = $('#theDataTable').DataTable({
columns: cols,
data: someDataInCorrectFormat
}).draw();
The datatable is correctly rendered but it doesn't show the values for the columns where there are some special characters ( [
and ]
in my case) in the column names. 数据表已正确呈现,但在列名中有一些特殊字符(在我的情况下为
[
和]
)中,它没有显示列的值。
Any and all help welcome. 任何帮助都欢迎。
If you are using the data
option as a string, the brackets get interpreted as array notation. 如果您将
data
选项用作字符串,则括号将被解释为数组符号。 The datatables documentation states the following: 数据表文档指出以下内容:
DataTables can automatically combine data from an array source, joining the data with the characters provided between the two brackets.
DataTables可以自动合并来自数组源的数据,并将数据与两个方括号之间提供的字符结合在一起。 For example: name[, ] would provide a comma-space separated list from the source array.
例如:name [,]将提供一个与源数组逗号分隔的列表。 If no characters are provided between the brackets, the original array source is returned.
如果方括号之间未提供任何字符,则返回原始数组源。
So your data: "[bla]"
is looking for a unnamed array (which does not exist hence the empty column) to display bla's between its elements. 因此,您的
data: "[bla]"
正在寻找一个未命名的数组(该数组不存在,因此为空列)以在其元素之间显示bla。 Unfortunately there seems to be no way to escape the brackets in the string. 不幸的是,似乎没有办法转义字符串中的括号。 The easiest workaround would be to use
data
as a function and get the value from the object. 最简单的解决方法是将
data
用作函数并从对象获取值。
cols.push({
data: function(row) {
return row["[bla]"];
},
title:"[bla]",
render: $.fn.dataTable.render.number(",",".",3)
});
This is your updated fiddle . 这是您更新的小提琴 。
Edit: If you don't know the columns name like the OP mentioned in the comments, the data function can be used with the index of the column. 编辑:如果您不知道注释中提到的OP之类的列名,则可以将data函数与该列的索引一起使用。
cols.push({
data: function(row, type, set, meta) {
return row[theColumns[meta.col].theColumnName];
},
title: theColumns[i].theColumnName,
render: $.fn.dataTable.render.number(",",".",3)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.