我有一个TableView,需要在服务器端完成分页和排序。我开始使用一个简单的thread.sleep来创建一个模拟服务调用的例子。
我看到我可以创建一个页面工厂,它返回不同的页面并调用后端服务来检索页面。
我看到我可以覆盖setSortPolicy方法,当我点击对列进行排序时会调用此方法。
我不知道的是如何从TableView对象中检索,单击了哪些列以应用正确的排序并将其反映在列标题上,目前我的示例下面将始终返回相同的排序顺序而不是设置列标题。
如果有人能够纠正这个例子以正确反映排序,我将非常感激。
package example;
import javafx.application.Application;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class TestTablePagination extends Application {
private final static int dataSize = 10_023;
private final static int rowsPerPage = 1000;
private final TableView<Sample> table = createTable();
private final List<Sample> data = createData();
private Pagination pagination = null;
private List<Sample> createData() {
List<Sample> data = new ArrayList<>(dataSize);
for (int i = 0; i < dataSize; i++) {
data.add(new Sample(i, "foo " + i, "bar " + i));
}
return data;
}
private TableView<Sample> createTable() {
TableView<Sample> table = new TableView<>();
TableColumn<Sample, Integer> column1 = new TableColumn<>("Id");
column1.setCellValueFactory(param -> param.getValue().id);
TableColumn<Sample, String> column2 = new TableColumn<>("Foo");
column2.setCellValueFactory(param -> param.getValue().foo);
TableColumn<Sample, String> column3 = new TableColumn<>("Bar");
column3.setCellValueFactory(param -> param.getValue().bar);
table.getColumns().addAll(column1, column2, column3);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
return table;
}
private void simulateSlowServeSideCall() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private Node createPage(int pageIndex) {
table.getItems().clear();
table.setSortPolicy( new Callback<TableView<Sample>, Boolean>() {
@Override
public Boolean call(TableView<Sample> tableView) {
System.out.println("call called - Start");
simulateSlowServeSideCall();
Comparator<Sample> comparator = new Comparator<Sample>() {
@Override
public int compare(Sample r1, Sample r2) {
if (tableView.getComparator() == null) {
return 0;
} else {
return tableView.getComparator().compare(r1, r2);
}
}
};
int fromIndex = pagination.getCurrentPageIndex() * rowsPerPage;
int toIndex = Math.min(fromIndex + rowsPerPage, data.size());
System.out.println("call called - " + fromIndex + " " + toIndex);
table.setItems(FXCollections.observableArrayList(data.subList(fromIndex, toIndex)));
FXCollections.sort(table.getItems(), comparator);
System.out.println("call called - Finish");
return true;
}
});
Thread thread = new Thread() {
@Override
public void run() {
System.out.println("createPage called - Start");
simulateSlowServeSideCall();
int fromIndex = pageIndex * rowsPerPage;
int toIndex = Math.min(fromIndex + rowsPerPage, data.size());
table.setItems(FXCollections.observableArrayList(data.subList(fromIndex, toIndex)));
System.out.println("createPage called - Finish");
}
};
thread.start();
return new BorderPane(table);
}
@Override
public void start(final Stage stage) throws Exception {
pagination = new Pagination((data.size() / rowsPerPage + 1), 0);
pagination.setPageFactory(this::createPage);
Scene scene = new Scene(new BorderPane(pagination), 1024, 768);
stage.setScene(scene);
stage.setTitle("Table pager");
stage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
public static class Sample {
private final ObservableValue<Integer> id;
private final SimpleStringProperty foo;
private final SimpleStringProperty bar;
private Sample(int id, String foo, String bar) {
this.id = new SimpleObjectProperty<>(id);
this.foo = new SimpleStringProperty(foo);
this.bar = new SimpleStringProperty(bar);
}
}
}