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;
}
}

Advertisements

14 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. thiswayup says:

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

  4. multi_io says:

    I was about to also use the rowIndex parameter for this purpose (which *is* documented btw), but people on IRC pointed out that a more elegant way is to define the row’s field name as “_item”, in which case the formatter function will receive the whole item (rowdata) rather than a specific field of it. You can define as many such _item columns as you want, in case you need more than one such column.

    And btw., I think direct field access as in “rowdata.owner_name” is theoretically unsafe because the grid’s store may employ some lazy-loading scheme. You should use store.getValue(rowdata,”owner_name”) instead.

  5. Arcanis says:

    You can use this:
    {
    ‘name’ : ‘BlaBla’,
    ‘width’ : ’24px’,
    ‘fields’ : [ ‘Name’, ‘Surname’, ‘Age’ ],
    ‘formatter’ : this.documentCustomFormatter
    }

    and then in formatter sometnig like this:

    documentNameFormatter : function(fields) {
    return “” + fields[0] + “” + fields[1];
    },

  6. Papa Kosta says:

    Thanx! Where is normal documentation over DataGrid? I need to this rowIndex

  7. Somebody essentially lend a hand to make critically articles I might state.
    That is the very first time I frequented your website
    page and up to now? I surprised with the research you made to create this
    particular publish incredible. Great process!

  8. Karry says:

    If some one needs expert view regarding blogging and site-building after that i suggest him/her to visit this webpage, Keep
    up the pleasant job.

  9. It’s a pity you don’t have a donate button! I’d most certainly donate to this superb blog! I suppose for now i’ll settle for book-marking and adding
    your RSS feed to my Google account. I look forward to
    fresh updates and will talk about this site with my Facebook group.
    Chat soon!

  10. Darwin says:

    I actually was initially looking for points for my weblog and observed ur
    blog, “Dojo – DataGrid – Combining Field values in Formatter | Documentum Cookbook”, will
    you care in the event that I really employ several of your own suggestions?
    Thx ,Katharina

  11. Lorie says:

    You constructed quite a few outstanding tips inside ur post, “Dojo – DataGrid – Combining
    Field values in Formatter | Documentum Cookbook”. I may
    remain returning to ur site shortly. Many thanks ,Rudolf

  12. It worked fro me after I put the name of the formatter in quotes, such as
    formatter: “getAuthor”

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 )

Google photo

You are commenting using your Google 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

%d bloggers like this: