Page 1 of 1

tree filter is not working right when there are more than 10 nodes

Posted: 20 Apr 2017, 19:39
by tefron
The tree filter is very nice, however, when the tree contain many nodes it doesn't filter them right.

The code:

Code: Select all

	public void encodeTreeNode(FacesContext context, Tree tree, TreeNode node, String clientId, boolean dynamic, boolean checkbox, boolean dragdrop) throws IOException {
        //preselection
        String rowKey = node.getRowKey();
        boolean selected = node.isSelected();
        boolean partialSelected = node.isPartialSelected();
        boolean filter = (tree.getValueExpression("filterBy") != null);

        UITreeNode uiTreeNode = tree.getUITreeNodeByType(node.getType());
        if(!uiTreeNode.isRendered()) {
            return;
        }
        
        List<String> filteredRowKeys = tree.getFilteredRowKeys();
        boolean match = false;
        if(filter && filteredRowKeys.size() > 0) {
            for(String rk : filteredRowKeys) {
                if(rk.startsWith(rowKey) || rowKey.startsWith(rk)) {
                    match = true;
                    if(!node.isLeaf() && !rowKey.startsWith(rk)) {
                        node.setExpanded(true);
                    }
                    break;
                }
            }
            
            if(!match) {
                return;
            }
        }   

The check:
if(rk.startsWith(rowKey) || rowKey.startsWith(rk))

is wrong in my case there is a node that was matched with id: 0_101 and this check match also node with other ids e.g.: 0_1

I understand the logic of this check (matching parents of matched nodes) but it doesn't work in this case and in general I don't thing its a very good idea to put to many assumptions and logic into the id structure. The id should be unique and 'dumb' so it could be overridden easily by applications.

Re: tree filter is not working right when there are more than 10 nodes

Posted: 20 Apr 2017, 22:11
by tefron
The following code change works for me:

Code: Select all

        if (filter && filteredRowKeys.size() > 0) {
            for (String rk : filteredRowKeys) {
                String rowKeyExt = rowKey + "_";
                String rkExt = rk + "_";
                if (rk.startsWith(rowKeyExt) || rowKey.startsWith(rkExt) || rk.equals(rowKey)) {
                    match = true;
                    if (!node.isLeaf() && !rowKey.startsWith(rk)) {
                        node.setExpanded(true);
                    }
                    break;
                }
            }
You should test it for other use case of course.

Re: tree filter is not working right when there are more than 10 nodes

Posted: 21 Apr 2017, 10:26
by kukeltje
Good find, please file an issue in github

Re: tree filter is not working right when there are more than 10 nodes

Posted: 22 Apr 2017, 14:39
by Melloware
If you file the issue in Github I will fix it with a PR.

Re: tree filter is not working right when there are more than 10 nodes

Posted: 24 Apr 2017, 17:08
by Melloware