GXT re-arrange data array index in TreeStore -
currently, i'm using gxt 3.0.6
i have treestore let's called "treestore", model data "parentdto".
private treestore<parentdto> treestore; treestore = new treestore<parentdto>(new modelkeyprovider<parentdto>(){ @override public string getkey(parentdto item){ return string.valueof(item.getparentid()); } });
inside parentdto there list of childdto. if there parentdto data has list of childdto, want show in tree grid. use basic tree grid link https://www.sencha.com/examples/#exampleplace:basictreegrid
using reference, if try add 1 parentdto works fine, when problem when add many parent dto. here code adding data treestore
public void filltreestore(list<parentdto) listparent){ treestore.clear(); for(parentdto parentdto : listparent){ treestore.add(parentdto); if(parentdto.getlistchild().size() > 0){ for(childdto childdto : parent.getlistchild()){ treestore.add(parentdto,childdto); } } }
in case, need 1 level parent , child tree code enough. try debug code use expression
treestore.getall().get(index);
when add 1 parentdto (parenta) has 1 child (childa). result be
treestore.getall().get(0) -> contain parenta treestore.getall().get(1) -> contain childa
but if add 2 parentdto (parenta, parentb) , each of them have 1 child (childa,childb). result be
treestore.getall().get(0) -> contain parenta treestore.getall().get(1) -> contain parentb treestore.getall().get(2) -> contain childa treestore.getall().get(3) -> contain childb
but in grid, data shown fine :
row 1 : parenta (this row can expand) row 2 : childa (the expanded row form parenta) row 3 : parentb (this row can expand) row 4 : childb (the expanded row form parentb)
i need render icon if data "parent" use code :
(icon_variable).addbeforerendericoncelleventhandler(new beforerendericoncelleventhandler() { @override public void onbeforerendericoncell(beforerendericoncellevent event) { if(treestore.getparent(treestore.get(event.getselectedrowindex())) == null){ //#render icon here } } });
the problem @ code
treestore.get(event.getselectedrowindex())
when parentb added trigger addbeforerendericoncelleventhandler method. event.getselectedrowindex()
row index based on "grid's perspective". @ second row, grid's perspective (childa), event.getselectedrowindex()
return 1. "treestore's perspective", index 1 "parentb", icon render messed up.
that's why, result need in treestore this
treestore.getall().get(0) -> contain parenta treestore.getall().get(1) -> contain childa treestore.getall().get(2) -> contain parentb treestore.getall().get(3) -> contain childb
my solution : solve problem, now, use 2 stores, first 1 treestore, , second 1 liststore. each time parent , child added, insert them @ treestore , liststore. in liststore, keep parent's , child's index match grid's perspective, whenever addbeforerendericoncelleventhandler triggered, use liststore data.
in opinion, solution not enough because in case, maximum data can added store less 50, it's enough.
it looks default behavior. didn't trying guess can methods provide. i'm guessing trying traverse tree looking @ parent , of it's children before moving on next parent. it.
for (parentdto parent : treestore.getrootitems()){ (childdto child : treestore.getchildren(parent)){ } }
Comments
Post a Comment