views:

125

answers:

2

Hello, I created a dojox.Grid successfully, but in one case I need to pass two fields to a formatter function instead of just one. For instance:

   {
        field: 'id',
        name: 'Id',
        formatter: formatterFunction,
    },

I need to pass to formatterFunction() both 'id' and 'name' for instance. How can I do this? Thank you.

A: 

Are you sure that you want to format and maybe not use get instead? When you use a formatter the only value that is passed to the function is the value that field represents.

However, if you were to use get instead, you could use the item to access the other values. (However then you will lose sorting).

So for your column have

   {
        field: 'id',
        name: 'Id',
        get: getFunction
    },

Then have

getFunction: function(index,row) {
    return row.id + row.name;
}
Laykes
edited below see
Claudio
A: 

I know this was already mentioned in the IRC channel, but I'm answering here so others are aware, and also to address your further question that I'm not sure anyone answered.

New in 1.4 If you set the value of the field to "_item", then your formatter will be called with the entire item from the store - instead of just one field value

This makes it possible to do what you want using a formatter as well.

http://www.dojotoolkit.org/reference-guide/dojox/grid/DataGrid.html#usage

In the simplest case, without setting the grid's formatterScope, the grid's store can be accessed from within formatters via this.grid.store, e.g.:

function fmtItem(value) {
  var store = this.grid.store;
  return store.getValue(value, 'id') + ': ' + store.getValue(value, 'name');
}

Here's a really simple example of the above formatter in action:

http://jsbin.com/upico4/edit

There's also an example of this in one of the test pages, which creates an object to hold and scope the formatters:

http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/grid/tests/test_grid_formatters.html

Ken