Ragen Dazs Ragen Dazs - 4 months ago 21
jQuery Question

DataTables column data property containing brackets

I'm using jQuery DataTables version 1.10.12 from https://datatables.net/

Follow an example:

Columns:

columns: [
{title: "Id", data: "id"},
{title: "Name", data: "Person[name]"},
{title: "Gender", data: "Person[gender]"},
]


Data

[{
"id":"1",
"Person[name]":"Foo Bar",
"Person[gender]":"M"
}]


The result of this grid will render only
id
field, how can I use brackets as key values?

Answer

Characters [] are specially treated when string is specified for columns.data option.

You could try using function instead for columns.data option.

For example:

columns: [
    {  title: "Id", data: "id"},
    {  
        title: "Name", 
        data: function(row, type, val, meta){
           if (type === 'set') {
              row['Person[name]'] = val;
              return;
           } else {
              return row['Person[name]'];
           }
        }
    },
    {  
        title: "Gender", 
        data: function(row, type, val, meta){
           if (type === 'set') {
              row['Person[gender]'] = val;
              return;
           } else {
              return row['Person[gender]'];
           }
        }
    }
]