Dojo – DataGrid – Combining Field values in Formatter

I faced an interesting problem today. I was using a dojo Datagrid and wanted to display a different value in “Author” column if the author value was empty. All the samples I came across on the net were simple formatter examples which dealt with a single column.
Finally I found that the formatter function call has an “undocumented” 2nd argument “rowIndex”. Once I had a handle to this rowIndex, I could retrieve all the fields in the grid row using var rowdata = this.grid.getItem(rowIndex).

var layout = [{
field: “a_content_type”,
name: ‘ ‘,
width: “20px”,
formatter: getIcon
},
{
field: “author”,
name: ‘Author‘,
width: “25%”,
styles: ‘text-decoration:underline;’,
formatter: getAuthor
},……

//You can name the arguments anyway you want.
//You can call this function getAuthor(writer, rowNum), Javascript will pass the values to your arguments when formatter is called.
function getAuthor(author, rowIndex){
if( dojo.string.trim(author) == “”) { //author field was empty so use value from another field
console.debug(“Author was empty = ” + rowIndex);
var rowdata = this.grid.getItem(rowIndex);
return rowdata.owner_name;
} else {
return author;
}
}

Advertisement

4 Responses to Dojo – DataGrid – Combining Field values in Formatter

  1. juanros13 says:

    Thanks helps me a lot

  2. Ron says:

    You sir, are awesome! Thanks.

  3. Thor says:

    THANKS!!

  4. thiswayup says:

    doh! Wish I knew about this a few weeks ago!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.