Search code examples
jqueryjsonslickgrid

SlickGrid- How can I pass parameters into a formatter, from the columns loop (including long detailed answer)


I have a given grid. I want to place in each row, inside an attribute, an id value. (in order to get the row identity, to perform delete & edit actions).

The best way I thought about is to print it into an attrubute, using a formatter, but I cannot find the way to pass the id to the formatter function, cause the formatter call is columns deceleration level, and I'm getting each row id, inside the data loop.

How to do that?

function NameFormatter(row, cell, value, columnDef, dataContext) {
    return '<span data-user-id="'+id+'">"'+text+'"</span>';
}

columns.push({ 
   id: "name", name: "Name", field: "name", width: 180, 
   cssClass: "cellName", sortable: true, formatter:NameFormatter
})

for (var i = 0; i < list_users.length; i++) {
    data[i] = {
        /*
           What I do here?
           How do I pass list_users[i].id & 
           list_users[i].name to the formatter from here? 
        */
        name: list_users[i].name,
        role: returnRole(list_users[i].role),
        email: list_users[i].email,
        portfolios: 'no attr for now'
    };
}

Solution

  • The formatter is called at render time. So all your data is already loaded in the grid.

    Why not use an exclusive column for the ID (you could exclude it from showing after render by calling grid.setColumns)? Or better yet, use the dataView - it already has a mandatory column for ID - which you can use for editing/deleting.

    Hope this helps!