DBTreeView unwanted sorting (and useful code snippets)

Hello everyone,

I am using the DBTreeView bean and want to prevent the nodes from being re-sorted when another form sorts the data that was used to create the Tree. The initialization code for the DBTreeView bean is posted below.

function onLoad()
 {

 var servername = databaseManager.getDataSourceServerName(controller.getDataSource());
 var tablename = databaseManager.getDataSourceTableName(controller.getDataSource());

 var _binding;

 _binding = elements.school_hierarchy_tree.createBinding(servername, tablename);
 _binding.setImageURLDataprovider('school_icon');
 _binding.setNRelationDataprovider('n_relation');
 _binding.setTextDataprovider('name');
 
 _binding.setCallBackInfo(node_selected,'school_id');

 
 _binding = elements.school_hierarchy_tree.createBinding(servername, 'buildings');
 _binding.setImageURLDataprovider('building_icon');
 _binding.setNRelationDataprovider('n_relation');
 _binding.setTextDataprovider('name');
 
 _binding.setCallBackInfo(node_selected,'building_id');

 
 _binding = elements.school_hierarchy_tree.createBinding(servername, 'spaces');
 _binding.setImageURLDataprovider('space_icon');
 _binding.setNRelationDataprovider('n_relation');
 _binding.setTextDataprovider('name');

 _binding.setCallBackInfo(node_selected,'space_id');
 
 
 _binding = elements.school_hierarchy_tree.createBinding(servername, 'elements');
 _binding.setImageURLDataprovider('element_icon');
 _binding.setTextDataprovider('name');

 _binding.setCallBackInfo(node_selected,'element_id');

 var _fs = databaseManager.getFoundSet(servername, tablename);
 _fs.loadRecords();
 elements.school_hierarchy_tree.addRoots(_fs);
 
 }

Now the problem occurs when another form contains a sortable table (like a portal) that allows the user to change the sorting any of the columns. This has the effect that the affected nodes on the tree get sorted too! I really want to keep the DBTreeView navigation totally independent from the rest of the application so I tried using the unrelate() function to create a copy of the foundset and use that copy in “addRoots(unrelatedFoundset)”, but this had no effect.

For completeness sake and because I appreciated the examples in the other posts on the DBTreeView so much, here is also the code for handling callBacks and selecting Records in the foundset:

function node_selected(arg0)
 {

	//Select Records based on selection Path of Tree Node
	var path = elements.school_hierarchy_tree.selectionPath;
	
 	if(path[0])foundset.selectRecord(path[0]);//school
 	if(path[1])foundset.schools_to_buildings.selectRecord(path[1]);//building
 	if(path[2])foundset.schools_to_buildings.buildings_to_spaces.selectRecord(path[2]);//space
 	if(path[3])foundset.schools_to_buildings.buildings_to_spaces.spaces_to_elements.selectRecord(path[3]);//element
 	
 	
 	//Switch the Form display depending on the type of Node clicked
 	switch(path.length){
 		case 1:
 		forms.mainTabs.elements.tabLayout.setRightForm('frm_buildings', 'schools_to_buildings');
 		break;
 		case 2:
 		forms.mainTabs.elements.tabLayout.setRightForm('frm_spaces', 'schools_to_buildings.buildings_to_spaces');
 	 	break;
 		case 3:
 		forms.mainTabs.elements.tabLayout.setRightForm('frm_elements', 'schools_to_buildings.buildings_to_spaces.spaces_to_elements');
 		break;
 		case 4:
 		forms.mainTabs.elements.tabLayout.setRightForm('frm_elements', 'schools_to_buildings.buildings_to_spaces.spaces_to_elements');
 		break;
 	}
}

Note that I use Record selection instead of searches (as is the case in the TreeBeanSample example provided by Servoy). Since the selectionPath property of the tree handily contains the private keys of the records I also ignore the callback values (arg0) and effectively simply (ab)use the callback as an event listener (please tell me if this is really bad!).

I also found that it is now possible to directly call the local function for the callback instead of going the long way via the Global function call. Thus the comment from the sample “//since the dbtreeview only can call global methods,we forward the call to a specific form” seems outdated.

I hope someone can help me with the sorting problem and that the code I posted is helpful to others. Thanks up front to anyone replying and to the Servoy Dev team.

Micah

This is Servoy Developer Version 6.0.1 build 1219.

hi Micah,

  • is the sorting of nodes changed on the first level ?
  • do you refresh the tree after it’s foundset is sorted ?
  • can you provide a sample where you see this behavior ? (the best would be if
    you create a case in our support system http://support.servoy.com )

regards

Gabi Boros:
hi Micah,

  • is the sorting of nodes changed on the first level ?
  • do you refresh the tree after it’s foundset is sorted ?
  • can you provide a sample where you see this behavior ? (the best would be if
    you create a case in our support system http://support.servoy.com )

regards

  • I’m not exactly sure what you mean with the first question, but in practice I don’t want the hierarchy to update only when nodes are added by another form and not update at all when another form changes the sorting of the relations that I have made bindings too.

  • The “onSort” Event of the form doesn’t fire and I haven’t been able to find another event that fires when the sorting occurs. I probably could call a refresh of the hierarchy from the other form that triggers the sorting, but this would seem like a very un-elegant solution. Also, like I said above I would prefer it if the table would be unaffected by sorting.

  • If this turns out to be a very complicated problem I’ll sign up to the support system, thanks for the pointer.

Thanks for your help

with the first question I was asking if the nodes that are on the
topmost level, those that are shown first, got sorted;
I think the best will be if you open a case in our support system, with a sample

Hi Gabi,

I have created the issue in the support system under bugs (although I believe it is rather an error of logic on my part) and attached a sample solution to it.
Key: SVY-1109
Title: “Preventing Sorting of Tree-View Bean Nodes by related Form”
Thanks for your help so far and I will post the solution here on the forum for everyone as soon as we have resolved the problem in the support system.

Micah