I am working on a JavaFX application which requires handling large amount of data. I am using tableview with pagination so that user can page through all data easily.
When I click on tableview column header the default sorting gets applied on the data, that is fine, but the sorting is applied to data on current page only not to the entire data. I want the sorting to be applied to all the data and then the data in the current page to be changed according to the sorted data.
The data comes from a Rest API call in one go and fills an Observable list which backs up the tableview. The tableview page contains only a subset of data from the backing list and sorting is applied to that only, but I want the whole backing list to be sorted and data on the page to be changed according to sorted data.
This is how I create pages:
private void setPullBackDocumentData() {
// TODO Auto-generated method stub
paginationPullbackList = new Pagination();
paginationPullbackList.setPageCount(Utils.getPageCount(data.size(), Common.rowsPerPage));
paginationPullbackList.setCurrentPageIndex(0);
paginationPullbackList.setPageFactory(this::createPage);
setTableData();
vBoxContent.getChildren().clear();
vBoxContent.getChildren().add(new BorderPane(paginationPullbackList));
}
private Node createPage(int pageIndex) {
int fromIndex = pageIndex * Common.rowsPerPage;
int toIndex = Math.min(fromIndex + Common.rowsPerPage, data.size());
tableViewDocumentList.setItems(FXCollections.observableArrayList(data.subList(fromIndex, toIndex)));
Utils.setTableViewCellSize45(tableViewDocumentList);
return new BorderPane(tableViewDocumentList);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void setTableData() {
// TODO Auto-generated method stub
tableViewDocumentList = new TableView<DocumentPullBack>();
tableViewDocumentList.setEditable(true);
tableViewDocumentList.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
TableColumn documentNameCol = new TableColumn(
Utils.getStringFromResourceBundle("pullback.table.document.name"));
documentNameCol.setCellValueFactory(new PropertyValueFactory<DocumentPullBack, String>("finalDocumentName"));
TableColumn stateCol = new TableColumn(Utils.getStringFromResourceBundle("pullback.table.state"));
stateCol.setCellValueFactory(new PropertyValueFactory<DocumentPullBack, String>("documentStateName"));
TableColumn pullBackDateCol = new TableColumn(
Utils.getStringFromResourceBundle("pullback.table.pullback.date"));
pullBackDateCol.setStyle("-fx-alignment: CENTER;");
pullBackDateCol.setCellValueFactory(new PropertyValueFactory<DocumentPullBack, Date>("pullbackDateTimeAsDate"));
pullBackDateCol.setCellFactory(
new Callback<TableColumn<DocumentPullBack, Date>, TableCell<DocumentPullBack, Date>>() {
@Override
public TableCell<DocumentPullBack, Date> call(TableColumn<DocumentPullBack, Date> param) {
TableCell<DocumentPullBack, Date> cell = new TableCell<DocumentPullBack, Date>() {
@Override
public void updateItem(Date item, boolean empty) {
if (empty) {
setGraphic(null);
} else {
DocumentPullBack currentDocument = (DocumentPullBack) getTableView().getItems()
.get(getIndex());
String date = "";
date = DateUtils.convertToLocal(currentDocument.getPullbackDateTime());
setText(date);
}
}
};
return cell;
}
});
pullBackDateCol.setComparator(Comparator.nullsFirst(Comparator.naturalOrder()));
//pullBackDateCol.sortTypeProperty().addListener(o -> System.out.println("sortType changed to: " + pullBackDateCol.getSortType()));
//tableViewDocumentList.setItems(data);
tableViewDocumentList.getColumns().clear();
tableViewDocumentList.getColumns().addAll(documentNameCol, /* sourceCol, */stateCol, pullBackDateCol);
}
I referred to answer here question on filtering and sorting, but it seems to observe pagination's page index property, and in my case this will not change since we are not filtering any data just sorting the same data. I have tried applying sortPolicy and tried to work on getSortOrder()
, but with no luck. Is there a way I can achieve this?