In my previous blog i have articulated how you can create a basic tree using the GXT TreePanel. In the next few blogs i will try to write about some marvelous features which can be easily integrated with TreePanel. Here i state how to add filter functionality with TreePanel.
Create a new project named GxtFilterTree and add GXT library in the project. The tree node objects must possess the functionality of BaseTreeModel of GXT. I have used the same EmployeeTreeNode and Folder class for leaf and parent nodes that i used in my previous blog. And in the TestData class i have populated the data to build the tree. You will find the code for these classes here.
Go to the onModuleLoad method of GxtFilterTree class. Remove all the auto generated code of this method. Now create a TreeLoader, loader.
TreeLoader<ModelData> loader = new BaseTreeLoader<ModelData>( new TreeModelReader<List<ModelData>>());
BaseTreeLoader is default implementation of the TreeLoader interface which also extends the functionality of BaseLoader. Then create TreeStore, store by using the loader and create a TreePanel, tree with the store. Pass the root of the tree to the loader.load method as a load configuration which loads data using the configuration. You can get the root node from getTreeModel method of TestData class.
TreeStore<ModelData> store = new TreeStore<ModelData>(loader); TreePanel<ModelData> tree = new TreePanel<ModelData>(store); tree.setAutoLoad(true); tree.setDisplayProperty("name"); tree.setWidth(250); tree.setIconProvider(new ModelIconProvider<ModelData>() { public AbstractImagePrototype getIcon(ModelData model) { if (((TreeModel) model).isLeaf()) { return ICONS.user_add(); } return null; } }); loader.load(TestData.getTreeModel());
setAutoLoad method sets whether all children should automatically be loaded recursively. Other setter methods of TreePanel are self explanatory.
Now the time for creating a StoreFilterField and bind this to the store.
StoreFilterField<ModelData> filter = new StoreFilterField<ModelData>() { @Override protected boolean doSelect(Store<ModelData> store, ModelData parent, ModelData record, String property, String filter) { // only match leaf nodes if (record instanceof Folder) { return false; } String name = record.get("name"); name = name.toLowerCase(); if (name.startsWith(filter.toLowerCase())) { return true; } return false; } }; filter.bind(store);
StoreFilterField can filter any Store implementation. You just have to implement the doSelect method this class. Here i have matched only the leaf node.
You are just one step behind to run the application. Create a panel to add the filter and the tree and add that panel to the RootPanel. That’s all. Run the application and check the filter functionality.
VerticalPanel panel = new VerticalPanel(); panel.addStyleName("x-small-editor"); panel.setSpacing(8); panel.add(new Html("<span class=text>Enter a search string such as 'dirk'</span>")); panel.add(filter); panel.add(tree); RootPanel.get().add(panel); }To view the live demo of this tutorial Click here.