Question

I am using Dojo 1.7 and I have a field in a datagrid that can have no values, one value or multiple values. I am trying to use data formatted this way:

//data
var store2 = new dojo.data.ItemFileReadStore({
    data: {
        identifier: "id",
        items: [
            {id: 1, 'personNames': ['Steve', 'Roy', 'Gary']},
            {id: 2, 'personNames': ''} //blank, no person names

        ]
    }
});

//formater
function formatPersonNames(value){
 if (value == '') {
   return '<p>Nobody here</p>';
 } else {
   return value + '<p style="margin-top:10px;">Check out the names above!</p>';
 };
};

and this is the layout:

// layout
var layout2 = [
    {name: 'Display Order', field: 'id', noresize:true, 'width': '50px'},
    {name: 'Person Names', field: 'personNames', formatter: formatPersonNames, noresize:true}
];

The issue is that only the first name 'Steve' is showing up. I tried using value[0] as a test and that only made the first letter show up. I am new to this kind of stuff so any advice would be appreciated.

Was it helpful?

Solution

The problem is, you formatting function receives only a first of array items. Dojo doesn't handle arrays as you would like. You need to format data before passing to dojo:

for (var i=0;i<items.length;i++) {
  if (items[i].personNames instanceof Array)
    items[i].personNames = items[i].personNames.join(', ')
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top