PDA

View Full Version : Strange behavior while avoinding duplicates on store.add



andre_mendesc
17 Oct 2012, 7:13 AM
Good morning people,
I have two grids side by side, when I double click on a item in the first one, the selected item is added to the second grid.
The rule is: no duplicates. If i double-click on a item that's already on the second grid, it shall not be added to the second one.
This is the solution I found:
(The listener of the second grid)


listeners:{
itemdblclick: function(dv,record,item,index,e){
if(gridStore.find("name",this.getSelectionModel().getSelection()) == -1)
{
gridStore.add( this.getSelectionModel().getSelection() );
}
}


This doesn't work properly, the item still added, the strange behavior is that when I click on the item name on the top of the grid (the feature to sort the items) the duplicates magically disappear! and if I don't do it and instead try to do something with the duplicates, Ext tell's me "Uncaught TypeError: Cannot read property 'internalId' of undefined"
Crazy, hum? Is it there or not?

I even tried
gridStore.sort('name'); but this doesn't get rid of the duplicates as I expected, they only disappear when I manually tells ext to sort them by clicking on the top of the grid.

What I want is: when I double click something of the first grid that's already on the second grid, I don't want it to be added again.

Can someone help me with this?
Thanks!

James Goddard
17 Oct 2012, 8:01 AM
getSelection returns an array of records.

Your find is trying to compare a name (presuably a string) property to an array so 'foo' != [...].

Not sure why you are using getSelection at all, the function has a record passed to it so I would just use that record. Something along the lines of:



if (gridStore.find ('name', record.get ('name')) == -1) { gridStore.add (record); }You probably want a "copy" of the record which you can easily do with gridStore.add (record.data) instead.

andre_mendesc
17 Oct 2012, 8:16 AM
Thanks for the reply Mr. Goddard.


Let me explain why I use getSelection:
I have two grids. One is filled with content (peoples names) retrieved from database when the application starts.
The second starts empty and gets filled each time the user double-click on items from grid One.
That's why I use the getSelection.


In the code I posted, 'name' is not what it's being compared against. It's the label of the property to look for records..


The comparison works because it properly adds. The behavior differ's from the expected when I try do add an value that's already added. But you are right, I just saw that getSelection returns an array, so maybe the problem is because this confuses the find method.
I will lunch now and try something later, thanks!

James Goddard
17 Oct 2012, 8:50 AM
Thanks for the reply Mr. Goddard.
The second starts empty and gets filled each time the user double-click on items from grid One.
That's why I use the getSelection.


The "record" parameter of the itemdblclick event is the item that was dblclicked. Which will also be the selected record so the getSelection is redundant.



In the code I posted, 'name' is not what it's being compared against. It's the label of the property to look for records..


'name' is the name of the field in the model you want to compare against, which is almost certainly not an array of records.



The comparison works because it properly adds.


No the comparison fails because it ALWAYS adds, at least that is what you indicated in your first post.

andre_mendesc
17 Oct 2012, 10:36 AM
That's it, you're completely right.
I just tried the code from your first reply and it works perfectly and most important I understand why.



No the comparison fails because it ALWAYS adds, at least that is what you indicated in your first post.


Yes yes, I mean, it adds but the repeated records disappears after I click to sort them and also I get errors if I try to work with those repeated lines.. But that's because I was on the wrong approach.

Thank you for your patience.

James Goddard
17 Oct 2012, 10:38 AM
No problem, glad to help.