添加分页,及其回调函数
parent
7a34bb898b
commit
5fcdc77d41
6
pom.xml
6
pom.xml
|
|
@ -50,6 +50,12 @@
|
|||
<version>1.2.83</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>Freedom-Mr</groupId>
|
||||
<artifactId>JavaFx-PaginationPicker</artifactId>
|
||||
<version>0.0.2</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -25,9 +25,6 @@ public class OnlineExamApplication extends Application {
|
|||
stage.setResizable(false);
|
||||
stage.setTitle("在线考试系统");
|
||||
stage.show();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
package com.zhangmeng.online.exam.ui.admin;
|
||||
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
* @date 2025/3/14 12:13
|
||||
* @version: 1.0
|
||||
*/
|
||||
public class HomePage extends AnchorPane {
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -37,7 +37,7 @@ public class IndexPage extends BorderPane {
|
|||
|
||||
this.setLeft(sideMenu);
|
||||
this.setCenter(contentArea);
|
||||
this.setTop(top);
|
||||
// this.setTop(top);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ import com.alibaba.fastjson.JSON;
|
|||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import com.zhangmeng.online.exam.ui.api.model.UserDataLoad;
|
||||
import com.zhangmeng.online.exam.ui.components.callBack.DynamicTableComponentCallBack;
|
||||
import com.zhangmeng.online.exam.ui.layouts.SideMenu;
|
||||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.property.SimpleMapProperty;
|
||||
|
|
@ -52,21 +54,8 @@ public class ApiUtils {
|
|||
}
|
||||
|
||||
public static DataView getUserList() {
|
||||
String userListData = HttpUtils.GET(API_URL + "/user/list");
|
||||
JSONObject jsonObject = JSON.parseObject(userListData);
|
||||
JSONArray data = jsonObject.getJSONArray("data");
|
||||
List<Map<String, SimpleStringProperty>> userMapList = new ArrayList<>();
|
||||
|
||||
for (Object datum : data) {
|
||||
JSONObject user = (JSONObject) datum;
|
||||
Map<String, SimpleStringProperty> userMap = new HashMap<>();
|
||||
userMap.put("序号", new SimpleStringProperty(user.getString("id")));
|
||||
userMap.put("用户名", new SimpleStringProperty(user.getString("username")));
|
||||
userMap.put("电子邮箱", new SimpleStringProperty(user.getString("email")));
|
||||
userMap.put("手机号码", new SimpleStringProperty(user.getString("phone")));
|
||||
userMapList.add(userMap);
|
||||
}
|
||||
return new DataView(Arrays.asList("序号", "用户名", "电子邮箱", "手机号码"), userMapList);
|
||||
DataLoad dataLoad = new UserDataLoad();
|
||||
return dataLoad.loadData(1,10);
|
||||
}
|
||||
|
||||
public static DataView getRoleList() {
|
||||
|
|
@ -130,6 +119,62 @@ public class ApiUtils {
|
|||
|
||||
private List<Map<String, SimpleStringProperty>> dataList;
|
||||
|
||||
private Integer pageNum;
|
||||
private Integer pageSize;
|
||||
private Integer total;
|
||||
|
||||
private DynamicTableComponentCallBack callBack;
|
||||
|
||||
private DataLoad dataLoad;
|
||||
|
||||
public DataLoad getDataLoad() {
|
||||
return dataLoad;
|
||||
}
|
||||
|
||||
public void setDataLoad(DataLoad dataLoad) {
|
||||
this.dataLoad = dataLoad;
|
||||
}
|
||||
|
||||
public DynamicTableComponentCallBack getCallBack() {
|
||||
return callBack;
|
||||
}
|
||||
|
||||
public void setCallBack(DynamicTableComponentCallBack callBack) {
|
||||
this.callBack = callBack;
|
||||
}
|
||||
|
||||
public DataView(List<String> keys, List<Map<String, SimpleStringProperty>> dataList, Integer pageNum, Integer pageSize, Integer total) {
|
||||
this.keys = keys;
|
||||
this.dataList = dataList;
|
||||
this.pageNum = pageNum;
|
||||
this.pageSize = pageSize;
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public Integer getPageNum() {
|
||||
return pageNum;
|
||||
}
|
||||
|
||||
public void setPageNum(Integer pageNum) {
|
||||
this.pageNum = pageNum;
|
||||
}
|
||||
|
||||
public Integer getPageSize() {
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
public void setPageSize(Integer pageSize) {
|
||||
this.pageSize = pageSize;
|
||||
}
|
||||
|
||||
public Integer getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(Integer total) {
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public DataView(List<String> keys, List<Map<String, SimpleStringProperty>> dataList) {
|
||||
this.keys = keys;
|
||||
this.dataList = dataList;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
package com.zhangmeng.online.exam.ui.api;
|
||||
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
* @date 2025/3/14 15:48
|
||||
* @version: 1.0
|
||||
*/
|
||||
public interface DataLoad {
|
||||
|
||||
public ApiUtils.DataView loadData(Integer pageNum, Integer pageSize);
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package com.zhangmeng.online.exam.ui.api.model;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.zhangmeng.online.exam.ui.api.DataLoad;
|
||||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
* @date 2025/3/14 16:01
|
||||
* @version: 1.0
|
||||
*/
|
||||
public class UserDataLoad implements DataLoad {
|
||||
@Override
|
||||
public ApiUtils.DataView loadData(Integer pageNum, Integer pageSize) {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("pageNum", pageNum.toString());
|
||||
params.put("pageSize", pageSize.toString());
|
||||
String userListData = HttpUtils.GET(ApiUtils.API_URL + "/user/list",params);
|
||||
JSONObject jsonObject = JSON.parseObject(userListData);
|
||||
JSONArray data = jsonObject.getJSONArray("data");
|
||||
List<Map<String, SimpleStringProperty>> userMapList = new ArrayList<>();
|
||||
for (Object datum : data) {
|
||||
JSONObject user = (JSONObject) datum;
|
||||
Map<String, SimpleStringProperty> userMap = new HashMap<>();
|
||||
userMap.put("序号", new SimpleStringProperty(user.getString("id")));
|
||||
userMap.put("用户名", new SimpleStringProperty(user.getString("username")));
|
||||
userMap.put("电子邮箱", new SimpleStringProperty(user.getString("email")));
|
||||
userMap.put("手机号码", new SimpleStringProperty(user.getString("phone")));
|
||||
userMapList.add(userMap);
|
||||
}
|
||||
|
||||
ApiUtils.DataView dataView = new ApiUtils.DataView(Arrays.asList("序号", "用户名", "电子邮箱", "手机号码"), userMapList);
|
||||
dataView.setDataLoad(this);
|
||||
return dataView;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +1,28 @@
|
|||
package com.zhangmeng.online.exam.ui.components;
|
||||
|
||||
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
||||
import com.zhangmeng.online.exam.ui.components.callBack.DynamicTableComponentCallBack;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Pagination;
|
||||
import javafx.scene.control.TableColumn;
|
||||
import javafx.scene.control.TableView;
|
||||
import javafx.scene.control.cell.MapValueFactory;
|
||||
import javafx.scene.control.cell.TextFieldTableCell;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.layout.VBox;
|
||||
import org.casic.javafx.control.PaginationPicker;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static cn.hutool.poi.word.TableUtil.createTable;
|
||||
|
||||
/**
|
||||
* 动态表格
|
||||
*
|
||||
|
|
@ -20,10 +31,24 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||
* @version: 1.0
|
||||
*/
|
||||
public class DynamicTableComponent extends VBox {
|
||||
|
||||
private DynamicTableComponentCallBack callBack;
|
||||
|
||||
public DynamicTableComponentCallBack getCallBack() {
|
||||
return callBack;
|
||||
}
|
||||
|
||||
public void setCallBack(DynamicTableComponentCallBack callBack) {
|
||||
this.callBack = callBack;
|
||||
}
|
||||
|
||||
private final ObservableList<Map<String, SimpleStringProperty>> data = FXCollections.observableArrayList();
|
||||
|
||||
public DynamicTableComponent(List<Map<String, SimpleStringProperty>> dataList,List<String> keys) {
|
||||
// public DynamicTableComponent(List<Map<String, SimpleStringProperty>> dataList,List<String> keys) {
|
||||
public DynamicTableComponent(ApiUtils.DataView dataView) {
|
||||
super();
|
||||
List<Map<String, SimpleStringProperty>> dataList = dataView.getDataList();
|
||||
List<String> keys =dataView.getKeys();
|
||||
TableView<Map<String, SimpleStringProperty>> tableView = new TableView<>();
|
||||
tableView.setItems(data);
|
||||
tableView.setEditable(true);
|
||||
|
|
@ -53,6 +78,25 @@ public class DynamicTableComponent extends VBox {
|
|||
});
|
||||
data.addAll(dataList);
|
||||
this.getChildren().addAll(tableView);
|
||||
|
||||
//初始化一个分页
|
||||
PaginationPicker paginationPicker = new PaginationPicker();
|
||||
paginationPicker.setTotal(dataView.getTotal());//设置总数据量,默认0
|
||||
paginationPicker.setPageSize(dataView.getPageSize());//设置每页显示条数,默认30
|
||||
paginationPicker.setPageButtonCount(9);//设置页码按钮的数量,默认7,当总页数超过该值时会折叠,大于等于 5 且小于等于 21 的奇数
|
||||
paginationPicker.setCurrentPage(dataView.getPageNum());//设置当前选择页码,默认第一页(注意:必须放在所有设置条件之后)。不小于0 并且 不大于总页数
|
||||
paginationPicker.setPaginationButtonFontSize(12);//设置分页字体大小,默认10(不小于2)
|
||||
//监听点击动作事件
|
||||
paginationPicker.setOnAction(new EventHandler<ActionEvent>() {
|
||||
@Override
|
||||
public void handle(ActionEvent event) {
|
||||
// System.out.println("当前选择页码:"+paginationPicker.getCurrentPage());
|
||||
data.clear();
|
||||
data.addAll(callBack.flushData(paginationPicker.getCurrentPage(),paginationPicker.getPageSize()).getDataList());
|
||||
}
|
||||
});
|
||||
|
||||
this.getChildren().add(paginationPicker);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
package com.zhangmeng.online.exam.ui.components.callBack;
|
||||
|
||||
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
||||
import com.zhangmeng.online.exam.ui.api.DataLoad;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
* @date 2025/3/14 15:34
|
||||
* @version: 1.0
|
||||
*/
|
||||
public interface DynamicTableComponentCallBack {
|
||||
|
||||
public ApiUtils.DataView flushData(Integer pageNum, Integer pageSize);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package com.zhangmeng.online.exam.ui.components.callBack;
|
||||
|
||||
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
||||
import com.zhangmeng.online.exam.ui.api.DataLoad;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
* @date 2025/3/14 15:35
|
||||
* @version: 1.0
|
||||
*/
|
||||
public class DynamicTableComponentCallBackImpl implements DynamicTableComponentCallBack {
|
||||
|
||||
private DataLoad dataLoad;
|
||||
|
||||
public DynamicTableComponentCallBackImpl(DataLoad dataLoad) {
|
||||
this.dataLoad = dataLoad;
|
||||
}
|
||||
@Override
|
||||
public ApiUtils.DataView flushData(Integer pageNum, Integer pageSize) {
|
||||
// System.out.println("刷新数据:" + pageNum + " " + pageSize);
|
||||
return dataLoad.loadData(pageNum, pageSize);
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@ import com.zhangmeng.online.exam.ui.admin.IndexPage;
|
|||
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
||||
import com.zhangmeng.online.exam.ui.components.DynamicTableComponent;
|
||||
import com.zhangmeng.online.exam.ui.components.ShortAnswerComponent;
|
||||
import com.zhangmeng.online.exam.ui.components.callBack.DynamicTableComponentCallBackImpl;
|
||||
import javafx.beans.property.SimpleMapProperty;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
|
|
@ -163,9 +164,13 @@ public class SideMenu extends VBox {
|
|||
private void init_table(HBox hbox, ApiUtils.DataView dataView) {
|
||||
Scene scene = hbox.getScene();
|
||||
IndexPage root = (IndexPage) scene.getRoot();
|
||||
DynamicTableComponent shortAnswerComponent = new DynamicTableComponent(dataView.getDataList(), dataView.getKeys());
|
||||
shortAnswerComponent.setPadding(new Insets(15));
|
||||
shortAnswerComponent.prefHeightProperty().bind(root.heightProperty().subtract(15));
|
||||
root.setCenter(shortAnswerComponent);
|
||||
dataView.setPageNum(1);
|
||||
dataView.setPageSize(10);
|
||||
dataView.setTotal(100);
|
||||
DynamicTableComponent dynamicTableComponent = new DynamicTableComponent(dataView);
|
||||
dynamicTableComponent.setCallBack(new DynamicTableComponentCallBackImpl(dataView.getDataLoad()));
|
||||
dynamicTableComponent.setPadding(new Insets(15));
|
||||
dynamicTableComponent.prefHeightProperty().bind(root.heightProperty().subtract(15));
|
||||
root.setCenter(dynamicTableComponent);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,9 +28,8 @@ public class HttpUtils<T> {
|
|||
return HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
|
||||
}
|
||||
|
||||
public static <T> T GET(String url, Map<String, Object> params, Class<T> clazz) {
|
||||
String response = HttpUtil.get(url, params);
|
||||
return JSONObject.parseObject(response, clazz);
|
||||
public static String GET(String url, Map<String, Object> params) {
|
||||
return HttpUtil.get(url, params);
|
||||
}
|
||||
|
||||
public static <T> T POST(String url, Map<String, Object> params, Class<T> clazz) {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ module com.zhangmeng.onlineexamui {
|
|||
requires cn.hutool;
|
||||
requires java.sql;
|
||||
requires fastjson;
|
||||
requires JavaFx.PaginationPicker;
|
||||
|
||||
|
||||
opens com.zhangmeng.online.exam.ui to javafx.fxml;
|
||||
|
|
|
|||
Loading…
Reference in New Issue