PDA

View Full Version : CheckBoxSelectionModel select() problem



siberian
18 Jan 2010, 10:09 PM
I am using a CheckBox SelectionModel in my grid. I can populate it just fine but selecting items is not working well in my case.

Background:
I have many projects.
I have a list of Users in my grid. This grid is accessed from within a project and is used for selecting which Users have access to that project.

In database terms it is a one to many relationship (one project has many users).

So, what I am doing is loading (PagingLoader) the full user list and then I have a load listener that attempts to load the list of selected users for that project.



loader.addLoadListener(new LoadListener() {
public void loaderLoad(LoadEvent le) {
selectUsers();
}
});


selectUsers() kicks off an async call that passes the project to the backend which then returns a list of users valid for that project. The callback then passes this list to a handler that populates the checkboxselectionmodel (using the List based select). This all works programmatically as expected based on debug tracing.

Problem: Nothing ever gets selected. Why? Well, in tracing the GXT code my selected users list is properly iterated through (I pass back BeanModels from a factory) but the list of selected users never finds a match in the store (always returns -1 index).

Why? Probably because I am getting another list of users back so it has no reference to the list in the store currently and its not checking pk's or anything like that (that I can tell, its just doing a List check).

So, my question is : Is there anything I can override to change how it determines if an item is selected or not? Arno made mention of a method here : http://www.extjs.com/forum/showthread.php?t=78852&highlight=CheckBoxSelectionModel but it does not work for me.

My alternative is a lot more database work up-front and a new async method to return a hash of lists (users and selected users).

A preferred alternative would be someway of flagging the User to tell the grid it should be selected (as per Arnos post)

A brute force method would be to iterate over the selected users list and , for each item, iterate over the full users list. If a match is found assign the full user object to a selected list so that they match (n^2, sigh).

Any tips? I am exploring just returning the hashmap for now but would love a better solution (or being told I am dumb with a link to the answer ;) ).

John-

Arno.Nyhm
19 Jan 2010, 8:21 AM
A brute force method would be to iterate over the selected users list and , for each item, iterate over the full users list. If a match is found assign the full user object to a selected list so that they match (n^2, sigh).


for this you can use the store.findModel(String key) or the other findModel stuff - but it is the same: iteration over the full list.


but maybe you can look at this part - i not used it but from sourcecode it looks like your solution:


store.setModelComparer(ModelComparer<M> comparer)

here you can setup how your listmodels and your external models are compared.

you need in this case compare the user id to see if they are equal.


the other idea instead of the ModelComparer is to overwrite the "equals" in your User class - and then the DefaultModelComparer uses this for checking.

siberian
20 Jan 2010, 5:38 PM
I thought I replied to this already .hmm.. My apologies if this duplicates somehow.

Anyhow, this approach worked well for me. My custom comparar compares a specific model attribute for equality (as opposed to the object id which is transient and based on what comes out the the RPC which breaks if the same object is retrieved in two different operations).



package com.pgi.webapp.client.ui.user;

import com.extjs.gxt.ui.client.data.ModelComparer;
import com.extjs.gxt.ui.client.data.ModelData;
import com.pgi.webapp.client.model.PGUser;

public class PGUserModelComparer<PGUser extends ModelData> implements ModelComparer<PGUser> {

public boolean equals(PGUser m1, PGUser m2) {
if(m1.get("id").equals(m2.get("id"))){ return true; }
return false;
}
}



And I add it like this



store.setModelComparer(new PGUserModelComparer());


I mask the grid on initial load and then remask while I load the selected users from the RPC source. This results in a slight flicker but works well enough for us.

Thanks Arno!