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

UI Components for JSF
Post Reply
User avatar
tefron
Posts: 94
Joined: 15 Sep 2010, 23:54
Location: Canada
Contact:

20 Apr 2017, 19:39

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.
PrimeFaces: primefaces-8.0
Mojara2.3
Netbeans8.2
wildfly-17.0.1.Final

User avatar
tefron
Posts: 94
Joined: 15 Sep 2010, 23:54
Location: Canada
Contact:

20 Apr 2017, 22:11

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.
PrimeFaces: primefaces-8.0
Mojara2.3
Netbeans8.2
wildfly-17.0.1.Final

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

21 Apr 2017, 10:26

Good find, please file an issue in github

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

22 Apr 2017, 14:39

If you file the issue in Github I will fix it with a PR.
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

24 Apr 2017, 17:08

PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 19 guests