Special characters in DataTable column names in JQuery/Javascript

别说谁变了你拦得住时间么 提交于 2019-12-02 13:16:04

If you are using the data option as a string, the brackets get interpreted as array notation. 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. For example: name[, ] would provide a comma-space separated list from the source array. 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. 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.

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.

cols.push({
data: function(row, type, set, meta) {
  return row[theColumns[meta.col].theColumnName];
},
title: theColumns[i].theColumnName,
render: $.fn.dataTable.render.number(",",".",3)
});

Another fiddle.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!