PDA

View Full Version : Add new dynamic item to store



Misiu
17 Dec 2013, 1:52 PM
I have simple Model like this:


Ext.define('Item', {
extend: 'Ext.data.Model',
fields: ['path', 'name']});


Later in my code I'm adding new fields to it:


Item.prototype.fields.clear();
for (var i = 0; i < 5; i++) {
Item.prototype.fields.add(new Ext.data.Field({
name: 'path' + i,
type: 'string'
}));
}
And finally I want to add new item to my store:


grid_store.add({
path0: n.getDepth(),
name: n.data.text
});

above code works fine, but I would like to set field dynamically like this:



var index = 4;
grid_store.add({
path+index: n.getDepth(),//this isn't working
name: n.data.text
});


Is there a way to do that?
My example code: http://jsfiddle.net/Misiu/p7XkU/ (line 111)

evant
17 Dec 2013, 2:31 PM
var o = {
name: 'foo'
};
o['path' + index] = n.getDepth();


Although your code looks very strange. If you're dynamically clearing the model fields and adding them at runtime I'd say you'll probably want to revisit your architecture.

Misiu
18 Dec 2013, 12:23 AM
Thanks evant for help :)
I know my code looks strange, this is work in progress and I'm aware that clearing all fields isn't good.
I won't be doing this many times, this will be done only once.

What I'm trying to do is to convert TreeStore to "Flat" Store.
I imagine it like this:
-I'll create copy of model that is in TreeStore
-For every level of depth I'm creating new field in my copy model
-Reconfiguring grid to show new store with my clone model.

Please see my jsfiddle from my previous post and click on tree node. Hope this is more clear now :)
Maybe there are other ways of doing this?

I need this to be able to flip treegrid 90 degrees, so rows will become columns. I didn't found any solutions for this so I'm trying to create something by my own.