Reputation: 6426
Say the input data is a json like:
{ "facet_fields":{
"cat":[
"electronics",3,
"card",2,
"graphics",2,
"music",1
]}}
The array "cat" is recovered and should be displayed using a for loop. I'm stuck at this point :)
The code so far:
Ext.define('Sandbox.view.FacetList', {
extend: 'Ext.List',
xtype: 'facet-list',
config: {
baseCls: 'article-list',
itemTpl: '' +
'<div class="article">' +
'<tpl for="categories">' +
'<div>Item {#}</div>' +
'</tpl>' +
'</div>'
}
});
This outputs: Item 1 Item 2 Item 3 Item 4
and I'd like to see the following output:
electronics (3), card (2), graphics (2), music (1)
Can't find the right way. thanks for your time :-) J.
Upvotes: 0
Views: 4242
Reputation: 7225
I'm assuming you are using a model and a store for your data? If so, I suggest you use the convert method available in fields. It will allow you to convert that cat
field into better data that the tpl
can understand.
Ext.define('MyModel', {
extend: 'Ext.data.Model',
config: {
fields: [
{
name: 'cat',
convert: function(values, record) {
var data = [],
ln = values.length,
i, name, value;
// loop through each of the array values
for (i = 0; i < ln; i++) {
// if it is a name, save it
if (i % 2 == 0) {
name = values[i];
}
// if it is a value, save the value and push
// to the data array
if (i % 2 == 1) {
value = values[i];
data.push({
name: name,
value: value
});
}
}
// return the data array for this field
return data;
}
},
...
]
}
});
And then you can use it in your tpl
like this:
tpl: [
'<tpl for="cat">',
'{name}: {value}, '
'</tpl>'
].join()
Upvotes: 4