Search code examples
filtersmartgwttreegrid

How to filter a TreeGrid?


I currently have a TreeGrid which shows nodes with names. The data is coming from a manually populated DataSource.

When setting the filter on the nodeName field, The filter is not done recursevily and thus I can only filter the Root node.

How can I tell the filter to search for matches in child nodes?

PS: in the code below, I have 3 nodes Root > Run > Child1. If i try the filter and type "R", I get Root and Run. But if i Type "C", I get "no results found"

Code

DataSource:

package murex.otk.gwt.gui.client.ui.datasource;

import java.util.List;

import murex.otk.gwt.gui.client.ui.record.TreeRecord;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;

public class ClassesDataSource extends DataSource {

    private static ClassesDataSource instance = null;

    public static ClassesDataSource getInstance() {
        if (instance == null) {
            instance = new ClassesDataSource("classesDataSource");
        }
        return instance;
    }

    private ClassesDataSource(String id) {
        setID(id);

        DataSourceTextField nodeNameField = new DataSourceTextField("nodeName");
        nodeNameField.setCanFilter(true);
        nodeNameField.setRequired(true);

        DataSourceIntegerField nodeIdField = new DataSourceIntegerField("nodeId");
        nodeIdField.setPrimaryKey(true);
        nodeIdField.setRequired(true);

        DataSourceIntegerField nodeParentField = new DataSourceIntegerField("nodeParent");
        nodeParentField.setRequired(true);
        nodeParentField.setForeignKey(id + ".nodeId");
        nodeParentField.setRootValue(0);

        setFields(nodeIdField, nodeNameField, nodeParentField);
        setClientOnly(true);
    }

    public void populateDataSource(List<String> classNames) {
        TreeRecord root = new TreeRecord("Root", 0);
        addData(root);
        TreeRecord child1 = new TreeRecord("Child1", root.getNodeId());
        addData(child1);
        TreeRecord child2 = new TreeRecord("Run", child1.getNodeId());
        addData(child2);
    }

}

Main

public void onModuleLoad() {
    ClassesDataSource.getInstance().populateDataSource(new ArrayList<String>());
    final TreeGrid employeeTree = new TreeGrid();
    employeeTree.setHeight(350);
    employeeTree.setDataSource(ClassesDataSource.getInstance());
    employeeTree.setAutoFetchData(true);
    TreeGridField field = new TreeGridField("nodeName");
    field.setCanFilter(true);
    employeeTree.setFields(field);
    employeeTree.setShowFilterEditor(true);     
    employeeTree.setAutoFetchAsFilter(true);
    employeeTree.setFilterOnKeypress(true);
    employeeTree.draw();
}

Solution

  • I solved this.

    The problem was that the filter was calling the server to fetch data whereas my datasource was set to Client Only. To fix this, the employeeTree must have employeeTree.setLoadDataOnDemand(false);