TableView实现筛选功能

原始问题地址:
https://stackoverflow.com/questions/42519911/execute-code-on-filtering-javafx-tableview-rows
主要思路
用FilteredList类,调用setPredicate函数获取满足条件的数据.
然后使用SortedList排序数据,最后调用comparatorProperty().bind函数将数据绑定到
TableView
步骤说明:
1.使用TableView数据源 distanceDatas 创建filteredData实例
FilteredList<DistanceModel> filteredData = new FilteredList<>(distanceDatas, p -> true);
2.对过滤条件的输入框进行监听
distanceFilterField.textProperty().addListener((observable, oldValue, newValue) -> { filteredData.setPredicate(commande -> { int lowerCaseFilter=0; try { lowerCaseFilter = Integer.valueOf(newValue); } catch(Exception e) { LogUtil.error(newValue+" 过滤条件不是整数"); return true; } //偏移距离大于输入值则显示 if (Double.valueOf(commande.getDistance().get())>lowerCaseFilter) { return true; // Filter matches first name. } }) }
filteredData.setPredicate把满足条件的数据获取出来,返回true

3.对筛选的数据进行排序
SortedList<DistanceModel> sortedData = new SortedList<>(filteredData);
4.将数据绑定到TableView
sortedData.comparatorProperty().bind(distanceTableView.comparatorProperty()); // 5. Add sorted (and filtered) data to the table. distanceTableView.setItems(sortedData);

下面是完整代码:

    //丢星表数据
    private ObservableList<DistanceModel> distanceDatas;
    // 偏移距离表
    @FXML
    TableView distanceTableView;
    //偏移距离大于输入值则显示
    @FXML
    TextField distanceFilterField;
    /**
     * 过滤偏移距离
     * 偏移距离大于输入值则显示
     * 实现表格过滤行:
     * https://stackoverflow.com/questions/42519911/execute-code-on-filtering-javafx-tableview-rows
     */
    public void filterDistance()
    {
        //偏移距离数据
        FilteredList<DistanceModel> filteredData = new FilteredList<>(distanceDatas, p -> true);
        // 2. Set the filter Predicate whenever the filter changes.
        distanceFilterField.textProperty().addListener((observable, oldValue, newValue) -> {
            filteredData.setPredicate(commande -> {
                LogUtil.info("[filterField.textProperty()]");
                // 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.
                int lowerCaseFilter=0;
                try
                {
                    lowerCaseFilter = Integer.valueOf(newValue);
                }
                catch(Exception e)
                {
                    LogUtil.error(newValue+" 过滤条件不是整数");
                    return true;
                }

                //偏移距离大于输入值则显示
                if (Double.valueOf(commande.getDistance().get())>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.
                }
                 */
                return false; // Does not match.
            });

            // 3. Wrap the FilteredList in a SortedList.
            SortedList<DistanceModel> sortedData = new SortedList<>(filteredData);
            // 4. Bind the SortedList comparator to the TableView comparator.
            sortedData.comparatorProperty().bind(distanceTableView.comparatorProperty());
            // 5. Add sorted (and filtered) data to the table.
            distanceTableView.setItems(sortedData);
        });
    }

代码运行效果


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,067评论 19 139
  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,083评论 0 29
  • 下载安装搭建环境 可以选npm安装,或者简单下载一个开发版的vue.js文件 浏览器打开加载有vue的文档时,控制...
    冥冥2017阅读 6,103评论 0 42
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,788评论 0 33
  • 这是我的梦境记录者。10.31 小黑是个胡子老长须发花白的小小英俊少年,被收养之后顽皮的天性渐渐暴露出来,现在正在...
    张企鹅阅读 187评论 0 0