简体   繁体   English

具有搜索功能的TreeModelAdapter

[英]TreeModelAdapter with Search Functionality

I'm currently working with Tapestry 5.3 now, and using a Tree Component to display hierarchy of account list which has 4 levels. 我当前正在使用Tapestry 5.3,并使用树组件显示具有4个级别的帐户列表的层次结构。 But now I'm struggling with search functionality in this scenario: 但是现在在这种情况下,我正在努力使用搜索功能:

Level 1-1
|_ _ _ Level 2-1 
|_ _ _ Level 2-2
       |_ _ _ Level 3-1
       |_ _ _ Level 3-2
       |      |_ _ _ Level 4-1
       |      |_ _ _ Level 4-2
       |_ _ _ Level 3-3

For example. 例如。 User wants to searched account Level 4-1, the tree should displayed like this 用户想要搜索级别4-1的帐户,树应显示如下

Level 1-1
|_ _ _ Level 2-2
       |_ _ _ Level 3-2
              |_ _ _ Level 4-1

Can someone please help me this logic stuff? 有人可以帮我这个逻辑的东西吗? Thanks. 谢谢。

Finally, I've figured it out. 最后,我知道了。

I created a new class AccountTreeModelAdapter which is implements TreeModelAdapter . 我创建了一个新类AccountTreeModelAdapter ,它实现了TreeModelAdapter You can override the getChildren method and put your search logic there. 您可以覆盖getChildren方法并将搜索逻辑放在此处。 For me it works in this flow: 对我来说,它在以下流程中有效:

  • Add setSearchedAccounts(List<Account> searchedAccounts) method in AccountTreeModelAdapter AccountTreeModelAdapter添加setSearchedAccounts(List<Account> searchedAccounts)方法
  • Set it's value from your main class 从您的主要班级设置它的价值
  • In getChildren method, use Iterator to iterate your whole list (all accounts) and compare one by one with each Account's code in searchedAccounts list (using searchedAccounts.stream().filter() in Java 8). getChildren方法中,使用Iterator迭代整个列表(所有帐户),并将其与searchedAccounts列表中每个帐户的代码进行逐一比较searchedAccounts.stream().filter()在Java 8中使用searchedAccounts.stream().filter() )。

Voila, now the tree will only display accounts you desire. 瞧,现在这棵树只会显示您想要的帐户。 But, how to display the parent? 但是,如何显示父级? Until this point, the tree not showing the parent and the parent's parent and the parent's parent again and again until you reach the ROOT. 直到这一点,直到您到达ROOT为止,树一次又一次不显示父级,父级的父级和父级的父级。

To accomplish this, I create a new column for my Account entity in database. 为此,我为数据库中的Account实体创建了一个新列。 Which is accountPath . 这是accountPath So every time user create an account, this accountPath will work like this: 因此,每次用户创建帐户时,此accountPath都将像这样运行:

Account "ABC" (ID: 1)
    |_ _ _ "XYZ" (ID: 2)
        |_ _ _ "OPQ" (ID: 3)
            |_ _ _ "STU" (ID: 4)

Account "ABC" will has no path (since it's a ROOT). 帐户“ ABC”将没有路径(因为它是ROOT)。 Account "XYZ" will has path -1- Account "OPQ" will has path -1-2- Account "STU" will has path -1-2-3- 帐户“ XYZ”的路径为-1-帐户“ OPQ”的路径为-1-2-帐户“ STU”的路径为-1-2-3-

To get each parent's ID, use string.split("-") 要获取每个父母的ID,请使用string.split(“-”)

So, if you want the tree to display the parent, you can simply just add the parent account to searchedAccounts list. 因此,如果希望树显示父级,则只需将父级帐户添加到searchedAccounts列表中即可。

That's all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM