Java FX过滤器表视图

时间:2015-04-30 19:46:45

标签: java javafx javafx-2 javafx-8

我尝试使用文本字段来过滤表格视图,我想要一个文本字段(txtSearch)来搜索' nhs数字','名字&# 39;,'姓氏'和'分类类别'。我尝试过在网上实施各种解决方案而且没有运气,如果这个问题很糟糕的话,我仍然对所有这些都很陌生。任何帮助将不胜感激,我的代码如下。

公共类QueueTabPageController实现Initializable {

@FXML
private TableView<Patient> tableView;

@FXML
private TableColumn<Patient, String> NHSNumberColumn;

@FXML
private TableColumn<Patient, String> firstNameColumn;

@FXML
private TableColumn<Patient, String> lastNameColumn;

@FXML
private TableColumn<Patient, String> timeEnteredColumn;

@FXML
private TableColumn<Patient, String> triageAssessmentColumn;

@FXML
private TextField filterField;

@FXML
private QueueTabPageController queueTabPageController;

private ObservableList<Patient> tableData;

// public static LinkedList<Patient> displayQueue;


/**
     * Initializes the controller class. This method is automatically called
     * after the fxml file has been loaded.
     * 
     * Initializes the table columns and sets up sorting and filtering.
     */
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {

        assert tableView != null : "fx:id=\"tableView\" was not injected: check your FXML file 'FXMLQueueTabPage.fxml'";

        NHSNumberColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("nhsNumber"));
        firstNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("firstName"));
        lastNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("lastName"));
        timeEnteredColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("timeEnteredString"));
        triageAssessmentColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("triage"));

        // display the current queue to screen when opening page each time
        displayQueue(Queue.queue);

        // 0. Initialize the columns.
        //firstNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("firstName"));
        //lastNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("lastName"));

        // 1. Wrap the ObservableList in a FilteredList (initially display all
        // data).
        FilteredList<Patient> filteredData = new FilteredList<>(tableData,
                p -> true);

        // 2. Set the filter Predicate whenever the filter changes.
        filterField.textProperty().addListener(
                (observable, oldValue, newValue) -> {
                    filteredData.setPredicate(Patient -> {
                        // 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 (Patient.getFirstName().toLowerCase()
                                    .contains(lowerCaseFilter)) {
                                return true; // Filter matches first name.
                            } else if (Patient.getLastName().toLowerCase()
                                    .contains(lowerCaseFilter)) {
                                return true; // Filter matches last name.
                            }
                            return false; // Does not match.
                        });
                });

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

        // 4. Bind the SortedList comparator to the TableView comparator.
        sortedData.comparatorProperty().bind(tableView.comparatorProperty());

        // 5. Add sorted (and filtered) data to the table.
        tableView.setItems(sortedData);

    }

我在这里收到错误(第4步):

(TableView.comparatorProperty());

和(步骤5)

TableView.setItems(sortedData);

话说: 无法从类型TableView

中对非静态方法setItems(ObservableList)进行静态引用

1 个答案:

答案 0 :(得分:1)

您的TableView定义为:

@FXML
private TableView<Patient> tableView;

...所以

尝试放置:(tableView.comparatorProperty());

代替:(TableView.comparatorProperty());

并执行相同的操作:TableView.setItems(sortedData);

我建议您将tableView更改为与TableView不同的patientTable,例如function get_calender_data($year,$month) { $query = $this->db->select('date, data')->from('calendar') ->like('date',"$year-$month",'after')->get(); $cal_data = array(); foreach ($query->result() as $row) { if(substr($row->date,8,1)==0) { $cal_data[substr($row->date,9,1)] = $row->data; } else{ $cal_data[substr($row->date,8,2)] = $row->data; } } return $cal_data; }

让我知道它是否有效!