直接上代码
1,定义的过滤器
@FXML private TextField filterTxt;
2,绑定过滤器的共通方法
/**
* <pre>
* 为TableColumn绑定过滤器
* <pre/>
*
* @param tableView TableView
* @param observableList 筛选的数据集合
* @param filterValue 用于获取筛选状态为true或false的接口,此处为了
* 提共通方法,定义了接口
* @param newValue 输入的值
* @param <T> 表的泛型类型
*/
public static final <T> void bindTableViewFilter(TableView<T> tableView, ObservableList<T> observableList, FilterValue<T> filterValue, String newValue){
FilteredList<T> filteredData = new FilteredList<>(observableList, p->true);
filteredData.setPredicate(entity -> filterValue.compare(entity, newValue));
SortedList<T> sortedData = new SortedList<>(filteredData);
sortedData.comparatorProperty().bind(tableView.comparatorProperty());
tableView.setItems(sortedData);
}
3,判断是否是筛选结果的方法
private boolean compareKnowhowId(筛选的数据对象 entity, String newValue){
return .判断逻辑省略;
}
4,接口类定义
package xx.xx.xx.xx.xx;
public interface FilterValue<T> {
boolean compare(T t, String newValue);
}
5,进行绑定处理
filterTxt.textProperty().addListener((observable, oldValue, newValue) -> ControlUtil.bindTableViewFilter(tableView,observableList, (entity, currentValue)->compareKnowhowId(entity, currentValue), newValue));