DataTables column name containing brakets

DataTables column name containing brakets

depauladepaula Posts: 2Questions: 1Answers: 0

First of all I'm using DataTables version 1.10.12

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?

This question is asked too in Stack Overflow:
http://stackoverflow.com/questions/38624131/datatables-column-name-containing-brakets?noredirect=1#comment64634463_38624131

Answers

  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin

    That's a good one - I'm afraid you've run into a limitation in DataTables' data read options there. With a . you can escape it in columns.data to have it work, but that hasn't been implemented for [].

    I've just put a little test case together here: http://live.datatables.net/jalemizu/1/edit .

    Until I get a chance to implement that, I'm afraid the only option is to modify the JSON data property names.

    Allan

  • depauladepaula Posts: 2Questions: 1Answers: 0

    I will share a solution given by User Gyrocode.com.

    var cols = [{
        "id":"1",
        "Person[name]":"Foo Bar",
        "Person[gender]":"M"
    }];
    
    var keys = [];
    for (var i in cols) {
    if (cols[i].data !== 'id') {
        keys[i] = cols[i].data;
        cols[i].data = function (row, type, val, meta) {
            if (type === 'set') {
                row[keys[meta.col]] = val;
            } else {
                return row[keys[meta.col]];
            }
        }
    }
    
This discussion has been closed.