SamiHuutoniemi
SamiHuutoniemi

Reputation: 1595

Slickgrid: Using an object's member as a column field?

I have an object which has two members: num which is just an integer and article which is an object that I get through JSON and has a number of members itself. Now I want to use members from article for the name and pris fields in a SlickGrid. The example beneath does not seem to work however:

var columns = [
    { id: "antal", name: "Antal", field: "num", width:  10},
        { id: "namn", name: "Artikelnamn", field: "article.name", width: 50},
        { id: "pris", name: "Totalpris", field: "article.price", width: 50}
];

Can anyone assist me in implementing this correctly based on the code provided?

Upvotes: 1

Views: 1048

Answers (1)

Ryan Lynch
Ryan Lynch

Reputation: 7786

Use article as the field, and employ a column formatter. So:

var columns = [
    { 
        id: "antal", name: "Antal", field: "num", width:  10
    },
    { 
        id: "namn", name: "Artikelnamn", field: "article", width: 50, 
        formatter : function(row, cell, value, columnDef, dataContext){
            return value.name;
        }
     },
    { 
        id: "pris", name: "Totalpris", field: "article", width: 50,
        formatter : function(row, cell, value, columnDef, dataContext){
            return value.price;
        }

    }
];

Upvotes: 3

Related Questions