Results 1 to 6 of 6

Thread: Strange behavior while avoinding duplicates on store.add

  1. #1
    Sencha User
    Join Date
    Apr 2010
    Location
    Brazil
    Posts
    13
    Answers
    1
    Vote Rating
    0
      0  

    Default Answered: Strange behavior while avoinding duplicates on store.add

    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)
    Code:
            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
    PHP Code:
     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!

  2. 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:

    PHP Code:
    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.

  3. #2
    Ext JS Premium Member
    Join Date
    Nov 2009
    Location
    St Louis,MO
    Posts
    267
    Answers
    20
    Vote Rating
    18
      1  

    Default

    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:

    PHP Code:
    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.

  4. #3
    Sencha User
    Join Date
    Apr 2010
    Location
    Brazil
    Posts
    13
    Answers
    1
    Vote Rating
    0
      0  

    Default

    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!

  5. #4
    Ext JS Premium Member
    Join Date
    Nov 2009
    Location
    St Louis,MO
    Posts
    267
    Answers
    20
    Vote Rating
    18
      0  

    Default

    Quote Originally Posted by andre_mendesc View Post
    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.

    Quote Originally Posted by andre_mendesc View Post
    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.

    Quote Originally Posted by andre_mendesc View Post
    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.

  6. #5
    Sencha User
    Join Date
    Apr 2010
    Location
    Brazil
    Posts
    13
    Answers
    1
    Vote Rating
    0
      0  

    Default

    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.

  7. #6
    Ext JS Premium Member
    Join Date
    Nov 2009
    Location
    St Louis,MO
    Posts
    267
    Answers
    20
    Vote Rating
    18
      0  

    Default

    No problem, glad to help.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •