执行过滤JavaFX TableView行的代码

时间:2017-02-28 22:27:33

标签: java javafx filter tableview rows

我使用TextFiled来过滤JavaFX TableView的行,这会产生一个设置为TableView的SortedList并且它可以工作,我也创建了一个函数,它将SortedList作为参数来做其他事情,问题是尽管过滤在表中生效,但SortedList不会改变值,这是否意味着过滤隐藏了行而不是从TableView初始列表中删除它们,请帮忙,这里有#s我的代码:

FilteredList<CommandeFX> filteredData = new FilteredList<>(data, p -> true);

    // 2. Set the filter Predicate whenever the filter changes.
    filterField.textProperty().addListener((observable, oldValue, newValue) -> {
        filteredData.setPredicate(commande -> {
            // If filter text is empty, display all persons.
            if (newValue == null || newValue.isEmpty()) {
                return true;
            }

            // Compare first name and last name of every person with filter text.
            String lowerCaseFilter = newValue.toLowerCase();

            if (String.valueOf(commande.getCodeCommande()).toLowerCase().contains(lowerCaseFilter)) {
                return true;
                 // Filter matches first name.

            } else if (String.valueOf(commande.getCMD()).toLowerCase().contains(lowerCaseFilter)) {
                return true; // Filter matches last name.
            }
            else if (String.valueOf(commande.getClient()).toLowerCase().contains(lowerCaseFilter)) {                                  
                return true; // Filter matches last name.
            }
            else if (String.valueOf(commande.getFournisseur()).toLowerCase().contains(lowerCaseFilter)) {
                return true; // Filter matches last name.
            }
            else if (String.valueOf(commande.getQuantite()).toLowerCase().contains(lowerCaseFilter)) {
                return true; // Filter matches last name.
            }
            else if (String.valueOf(commande.getPrixCommande()).toLowerCase().contains(lowerCaseFilter)) {
                return true; // Filter matches last name.
            }
            else if (String.valueOf(commande.getSommeProperty()).toLowerCase().contains(lowerCaseFilter)) {
                return true; // Filter matches last name.
            }
            else if (String.valueOf(commande.getResteCommandeProperty()).toLowerCase().contains(lowerCaseFilter)) {
                return true; // Filter matches last name.
            }
            else if (String.valueOf(commande.getDateCommande()).toLowerCase().contains(lowerCaseFilter)) {
                return true; // Filter matches last name.
            }
            else if (String.valueOf(commande.getDateReception()).toLowerCase().contains(lowerCaseFilter)) {
                return true; // Filter matches last name.
            }
            else if (String.valueOf(commande.getModified()).toLowerCase().contains(lowerCaseFilter)) {
                return true; // Filter matches last name.
            }
            else if (String.valueOf(commande.getValide()).toLowerCase().contains(lowerCaseFilter)) {
                return true; // Filter matches last name.
            }

            return false; // Does not match.
        });
    });


    // 3. Wrap the FilteredList in a SortedList. 
    SortedList<CommandeFX> sortedData = new SortedList<>(filteredData);    

    // 4. Bind the SortedList comparator to the TableView comparator.
    sortedData.comparatorProperty().bind(tableCommandes.comparatorProperty());
    // 5. Add sorted (and filtered) data to the table.
    tableCommandes.setItems(sortedData);
    // the function 
    setSum(sortedData);

1 个答案:

答案 0 :(得分:0)

实际上我没注意每次输入过滤器时我想调用的函数Text-field不在正确的位置。现在,当我输入过滤器时,实时调用函数getSum(SortedList Commande<FX> list)。这是正确的代码:

FilteredList<CommandeFX> filteredData = new FilteredList<>(data, p -> true);

// 2. Set the filter Predicate whenever the filter changes.
filterField.textProperty().addListener((observable, oldValue, newValue) -> {
    filteredData.setPredicate(commande -> {
        // If filter text is empty, display all persons.
        if (newValue == null || newValue.isEmpty()) {
            return true;
        }

        // Compare first name and last name of every person with filter text.
        String lowerCaseFilter = newValue.toLowerCase();

        if (String.valueOf(commande.getCodeCommande()).toLowerCase().contains(lowerCaseFilter)) {
            return true;
             // Filter matches first name.

        } else if (String.valueOf(commande.getCMD()).toLowerCase().contains(lowerCaseFilter)) {
            return true; // Filter matches last name.
        }
        else if (String.valueOf(commande.getClient()).toLowerCase().contains(lowerCaseFilter)) {                                  
            return true; // Filter matches last name.
        }
        else if (String.valueOf(commande.getFournisseur()).toLowerCase().contains(lowerCaseFilter)) {
            return true; // Filter matches last name.
        }
        else if (String.valueOf(commande.getQuantite()).toLowerCase().contains(lowerCaseFilter)) {
            return true; // Filter matches last name.
        }
        else if (String.valueOf(commande.getPrixCommande()).toLowerCase().contains(lowerCaseFilter)) {
            return true; // Filter matches last name.
        }
        else if (String.valueOf(commande.getSommeProperty()).toLowerCase().contains(lowerCaseFilter)) {
            return true; // Filter matches last name.
        }
        else if (String.valueOf(commande.getResteCommandeProperty()).toLowerCase().contains(lowerCaseFilter)) {
            return true; // Filter matches last name.
        }
        else if (String.valueOf(commande.getDateCommande()).toLowerCase().contains(lowerCaseFilter)) {
            return true; // Filter matches last name.
        }
        else if (String.valueOf(commande.getDateReception()).toLowerCase().contains(lowerCaseFilter)) {
            return true; // Filter matches last name.
        }
        else if (String.valueOf(commande.getModified()).toLowerCase().contains(lowerCaseFilter)) {
            return true; // Filter matches last name.
        }
        else if (String.valueOf(commande.getValide()).toLowerCase().contains(lowerCaseFilter)) {
            return true; // Filter matches last name.
        }

        return false; // Does not match.
    });



      // 3. Wrap the FilteredList in a SortedList. 
      SortedList<CommandeFX> sortedData = new SortedList<>(filteredData);    

      // 4. Bind the SortedList comparator to the TableView comparator.
  sortedData.comparatorProperty().bind(tableCommandes.comparatorProperty());
     // 5. Add sorted (and filtered) data to the table.
     tableCommandes.setItems(sortedData);
     // the function 
     setSum(sortedData);

});