Selecting treeGrid checkboxes

Hi,

I am not able to select the checkboxes of treeGrid when i pass the records using treeGrid.selectRecords(record) or when passing a particular record,

The scenario is Roles are displayed in a list grid and services in treegrid (autofetch).
Two datasources are used - (servicesDataSource and minimalServiceDs).

On loading treeGrid has to display all the available services and uses ServiceDS.

On clicking on a role in the list grid eg : role1, the checkboxes of the corresponding services for role1 needs to be selected and other services in the tree are left as it is and the user may change that. To fetch set of services based on roles the minimalServiceDs is used.

So i have defined fetchData on minimalServiceDs and in callback of it trying to select the services treeGrid based on the datasource response. the response seems to be correct yet not working.

  1. The User Interface:
public class ServicesTree {
	public void onModuleLoad() {

		final ServicesDatasource serviceDS = ServicesDatasource.getInstance();
		final MinServicesDatasource minSerDS = MinServicesDatasource
				.getInstance();
		final RolesDatasource rolesDS = RolesDatasource.getInstance();

		final TreeGrid treeGrid = new TreeGrid();
		final ListGrid listGrid = new ListGrid();
		listGrid.setDataSource(rolesDS);
		listGrid.setWidth(500);
		listGrid.setHeight(100);
		listGrid.setAutoFetchData(true);

		listGrid.addRecordClickHandler(new RecordClickHandler() {
			public void onRecordClick(RecordClickEvent event) {
				ListGridRecord record = listGrid.getSelectedRecord();
				if (record != null) {

					minSerDS.fetchData(null, new DSCallback() {
						public void execute(DSResponse response,
								Object rawData, DSRequest request) {
							if (response.getStatus() < 0) {
								Map<String, String> errorMap = new HashMap<String, String>();
								String errorDesc = errorMap.get("error");
								SC.say("View Services", errorDesc);
							} else {
								String services = "";
								Record[] record = response.getData();
								// SC.say("records.length " + record.length +
								// " " +record[3].getAttribute("serviceName"));
								// treeGrid.selectRecord(record[3]);
								treeGrid.selectRecords(record);
							}
						}
					});
				}
			}
		});

		treeGrid.setLoadDataOnDemand(false);
		treeGrid.setWidth(600);
		treeGrid.setHeight(400);
		treeGrid.setDataSource(serviceDS);
		treeGrid.setAutoFetchData(true);
		treeGrid.setSelectionType(SelectionStyle.SIMPLE);
		treeGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);

		TreeGridField serviceField = new TreeGridField("serviceName");
		TreeGridField descField = new TreeGridField("serviceDesc");

		treeGrid.setFields(serviceField, descField);

		VLayout layout = new VLayout();
		layout.addMember(listGrid);
		layout.addMember(treeGrid);
		layout.draw();

	}
}
  1. Services DataSource
 public ServicesDatasource(String id) {  
		   
		         setID(id);  
		         setRecordXPath("/XDocument/XEService");  
		     		   
		         DataSourceIntegerField serviceIdField = new DataSourceIntegerField("serviceID", "Service ID");  
		         serviceIdField.setPrimaryKey(true);  
		         serviceIdField.setRequired(true);  
		        
		         DataSourceTextField serviceNameField = new DataSourceTextField("serviceName", "Service Name", 50);  
		        
		         DataSourceIntegerField parentIdField = new DataSourceIntegerField("serviceParentID", "Service ParentID");  
		         parentIdField.setRequired(true);  
		         parentIdField.setForeignKey(id + ".serviceID");  
		         parentIdField.setRootValue("1");  
		         
		         DataSourceTextField serviceDescField = new DataSourceTextField("serviceDesc", "Description", 128);
		         
		     	       
		         setFields(serviceIdField, serviceNameField, parentIdField, serviceDescField);  
		   
		         setDataURL("ServiceList.xml");  
		         setClientOnly(true);  
		     }
  1. MinimalServiceDS
    same as above and points to MinServiceList.xml

  2. ServiceList.xml

<XDocument>
	<XStatus>Success</XStatus>
	<XAction>GET</XAction>
	<XService>ServiceList</XService>
	
<XEService>
	<serviceID>1</serviceID>
	<serviceName>Service1</serviceName>
</XEService>

<XEService>
	<serviceID>1001</serviceID>
	<serviceName>Service2</serviceName>
	<serviceParentID>1</serviceParentID>
</XEService>

<XEService>
	<serviceID>1002</serviceID>
	<serviceName>Service3</serviceName>
	<serviceParentID>1</serviceParentID>
</XEService>

<XEService>
	<serviceID>1003</serviceID>
	<serviceName>Service3</serviceName>
	<serviceParentID>1</serviceParentID>
</XEService>

...
</XDocument>

4.MinServiceList.xml

<XDocument>
	<XStatus>Success</XStatus>
	<XAction>GET</XAction>
	<XService>ServiceList</XService>
	
<XEService>
	<serviceID>1</serviceID>
	<serviceName>Service1</serviceName>
</XEService>

<XEService>
	<serviceID>1001</serviceID>
	<serviceName>Service2</serviceName>
	<serviceParentID>1</serviceParentID>
</XEService>

<XEService>
	<serviceID>1002</serviceID>
	<serviceName>Service3</serviceName>
	<serviceParentID>1</serviceParentID>
</XEService>

...
</XDocument>

Please tell me whats wrong and how to correct this?

See the docs for DataSource.fetchData() - it doesn’t update UI components. You seem to be trying to call selectRecords() on records that aren’t even in the tree?

If they are corresponding records in the tree, then realize the records you are passing records are different objects, not the same records. You can find the corresponding records by key using Tree.findById(). However if the data is already in the Tree, then there in no need to call fetchData() in the first place, just access them via Tree APIs.

Selecting TreeGrid checkboxes

Hi,

Thank you for your reply. Now i am looping through the nodes in the tree using findById(Id); and type casted the nodes to Records. Now its working!

Record[] record = response.getData();
   					   							 
Tree serviceTree = new Tree();
serviceTree=treeGrid.getTree();
								
for (int i=0; i<record.length;i++)
{
	String serviceId = record[i].getAttribute(serviceTree.getIdField());
	TreeNode node = serviceTree.findById(serviceId);
	treeGrid.selectRecord( (Record) node);
}

Two xml responses are used , one for displaying treeGrid and the other used for treegrid checkbox selection – comes from different datasource , though the xml structure and datasource definition of both are same. Why does directly passing record array doesn’t work, while typecasting works (node to Record)?

Record[] record = response.getData();
treeGrid.selectRecords(record);

For testing, i did fetchData() to the same DataSource pointed by treeGrid. Even the records returned by that response is not working.

Thanks
Ramya

Nothing to do with type casts. The records you are passing in are simply unrelated objects, not the same as the record in the tree. You can have more than one copy of a record and they are distinct objects (as they should be; the same record frequently needs to appear in different places in different states).