PDA

View Full Version : How can I merge entries in a store



ci11111
29 Mar 2012, 8:50 AM
lets says my store items look like this



[
{ a: 'mike', b:1, c: 0},
{ a: 'mike', b:1, c: 1},
{ a: 'jack', b:1, c: 1},
...

]

I would like to merge according to key 'a', (and set its 'c' value to 2^0+2^1)


[
{ a: 'mike', b:1, c: 4},
{ a: 'jack', b:1, c: 1},
...

]


edit: Shoudl I do it during the load, it requires some comptuing for each entries, and it's not recommended.
I would it to do it 'afterload' and before the dataview is rendered

edit2:
I'm doing this sort of loop very heavy


beforerender: function(){
store.each(function(record) {

// need to find in store other entry with entry.get('a') = record.get('a')
and then merge


});
}

How is working the aggregate function please

Shehul
29 Mar 2012, 10:15 AM
Hi,

Its your choice where you want to call the merge,

but basically you don't need to loop, you can use

var tests = store.findExact('SearchColumnName', KeyValue, StartPosition);

Once you find the record (if not found method will return -1) , you can set appropriate value like,

store.getAt(tests).data.KeyValue = NewValue

I am using these methods and they are faster than loops.

Shehul

ci11111
29 Mar 2012, 10:33 AM
I need to do that for each duplicate 'a' entries, I think I need to have a groupField: 'a' in the store and then run the aggregate on the store

my problem is I already another grouping set on then store, How can I manage that?

It would be really nicer to run this aggregate function,

than having to loop throught the store, and searching if findExact('a', record.get('a'))!=record.index and then perfom the merging

I have a problem with aggregate, I put groupField: 'a' in the store
but when calling store.getGroups() there's only one array
or if I run


store.aggregate(function(){ console.log('pop'); });

I see only one message,

but my store contains 6 items, and each pair have the same 'a' field

My data are taken via proxy, it seems the grouping is not done in this case

Shehul
29 Mar 2012, 10:49 AM
Hi,

If you read the documentation of findExact, it returns you the first found record index.

In your situation you have 2 options,

You can use the index return by findexact to query again store with that index being the start position(third parameter).

Or

I haven't tried grouping but try queryBy (http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Store-method-queryBy) function which will give you your 2 records and than for sure here you have to loop for those found records.

Shehul




findExact('a','mike') will return the first 2 records of my example?

I need to do that for each duplicate 'a' entries, I think I need to have a groupField: 'a' in the store and then run the aggregate on the store

my problem is I already another grouping set on then store, How can I manage that?