动态表格修改 2025年3月7日11:22:44
parent
94e5f343b1
commit
431cf74b95
|
|
@ -9,11 +9,10 @@ import com.alibaba.fastjson.JSONObject;
|
|||
|
||||
import com.zhangmeng.online.exam.ui.layouts.SideMenu;
|
||||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.property.SimpleMapProperty;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
|
|
@ -52,41 +51,58 @@ public class ApiUtils {
|
|||
return menuItems;
|
||||
}
|
||||
|
||||
public static Map<String, String> getUserList(){
|
||||
public static List<Map<String, SimpleStringProperty>> getUserList(){
|
||||
String userListData = HttpUtils.GET(API_URL + "/user/list");
|
||||
JSONObject jsonObject = JSON.parseObject(userListData);
|
||||
JSONArray data = jsonObject.getJSONArray("data");
|
||||
Map<String, String> userMap = new HashMap<>();
|
||||
List<Map<String, SimpleStringProperty>> userMapList = new ArrayList<>();
|
||||
for (Object datum : data) {
|
||||
|
||||
JSONObject user = (JSONObject) datum;
|
||||
userMap.put("序号", user.getString("id"));
|
||||
userMap.put("用户名", user.getString("username"));
|
||||
userMap.put("电子邮箱", user.getString("email"));
|
||||
userMap.put("手机号码", user.getString("phone"));
|
||||
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 userMap;
|
||||
return userMapList;
|
||||
}
|
||||
|
||||
public static Map<String, String> getRoleList() {
|
||||
public static List<Map<String, SimpleStringProperty>> getRoleList() {
|
||||
String userListData = HttpUtils.GET(API_URL + "/role/list");
|
||||
JSONObject jsonObject = JSON.parseObject(userListData);
|
||||
JSONArray data = jsonObject.getJSONArray("data");
|
||||
Map<String, String> userMap = new HashMap<>();
|
||||
List<Map<String, SimpleStringProperty>> roleMapList = new ArrayList<>();
|
||||
for (Object datum : data) {
|
||||
|
||||
JSONObject user = (JSONObject) datum;
|
||||
userMap.put("序号", user.getString("id"));
|
||||
userMap.put("角色名称", user.getString("name"));
|
||||
userMap.put("描述", user.getString("desc"));
|
||||
userMap.put("角色类型", user.getString("type_name"));
|
||||
JSONObject role = (JSONObject) datum;
|
||||
Map<String, SimpleStringProperty> roleMap = new HashMap<>();
|
||||
roleMap.put("序号", new SimpleStringProperty(role.getString("id")));
|
||||
roleMap.put("角色名称",new SimpleStringProperty( role.getString("name")));
|
||||
roleMap.put("描述",new SimpleStringProperty( role.getString("desc")));
|
||||
roleMap.put("角色类型",new SimpleStringProperty( role.getString("type_name")));
|
||||
roleMapList.add(roleMap);
|
||||
}
|
||||
|
||||
return userMap;
|
||||
return roleMapList;
|
||||
}
|
||||
|
||||
public static Map<String, String> getPermissionList() {
|
||||
return null;
|
||||
public static List<Map<String, SimpleStringProperty>> getPermissionList() {
|
||||
|
||||
String userListData = HttpUtils.GET(API_URL + "/permission/list");
|
||||
JSONObject jsonObject = JSON.parseObject(userListData);
|
||||
JSONArray data = jsonObject.getJSONArray("data");
|
||||
|
||||
List<Map<String, SimpleStringProperty>> permissionMapList = new ArrayList<>();
|
||||
for (Object datum : data) {
|
||||
JSONObject permission = (JSONObject) datum;
|
||||
Map<String, SimpleStringProperty> userMap = new HashMap<>();
|
||||
userMap.put("序号", new SimpleStringProperty(permission.getString("id")));
|
||||
userMap.put("权限名称",new SimpleStringProperty( permission.getString("name")));
|
||||
userMap.put("描述",new SimpleStringProperty( permission.getString("desc")));
|
||||
userMap.put("权限路径",new SimpleStringProperty( permission.getString("url")));
|
||||
permissionMapList.add(userMap);
|
||||
}
|
||||
return permissionMapList;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,16 @@
|
|||
package com.zhangmeng.online.exam.ui.components;
|
||||
|
||||
import javafx.beans.property.SimpleMapProperty;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
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.VBox;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 动态表格
|
||||
|
|
@ -21,59 +20,31 @@ import java.util.Map;
|
|||
* @version: 1.0
|
||||
*/
|
||||
public class DynamicTableComponent extends VBox {
|
||||
private final ObservableList<DynamicDataModel> data = FXCollections.observableArrayList();
|
||||
private final ObservableList<Map<String, SimpleStringProperty>> data = FXCollections.observableArrayList();
|
||||
|
||||
// 数据模型定义(使用JavaFX属性):ml-citation{ref="1,3" data="citationList"}
|
||||
public static class DynamicDataModel {
|
||||
private final Map<String, SimpleStringProperty> properties = new HashMap<>();
|
||||
|
||||
public DynamicDataModel(Map<String, String> data) {
|
||||
data.forEach((k, v) ->
|
||||
properties.put(k, new SimpleStringProperty(v))
|
||||
);
|
||||
}
|
||||
|
||||
public SimpleStringProperty getProperty(String key) {
|
||||
return properties.get(key);
|
||||
}
|
||||
}
|
||||
|
||||
public DynamicTableComponent(Map<String, String> dataList) {
|
||||
public DynamicTableComponent(List<Map<String, SimpleStringProperty>> dataList) {
|
||||
super();
|
||||
TableView<DynamicDataModel> tableView = new TableView<>();
|
||||
TableView<Map<String, SimpleStringProperty>> tableView = new TableView<>();
|
||||
tableView.setItems(data);
|
||||
tableView.prefHeightProperty().bind(this.heightProperty());
|
||||
tableView.prefWidthProperty().bind(this.widthProperty());
|
||||
// 动态生成列(示例字段):ml-citation{ref="3,4" data="citationList"}
|
||||
List<String> columns = Arrays.asList(dataList.keySet().toArray(new String[0]));
|
||||
List<String> columns = Arrays.asList(dataList.get(0).keySet().toArray(new String[0]));
|
||||
columns.forEach(col -> {
|
||||
TableColumn<DynamicDataModel, String> column = new TableColumn<>(col);
|
||||
|
||||
TableColumn<Map<String,SimpleStringProperty>, String> column = new TableColumn<>(col);
|
||||
// 数据绑定:ml-citation{ref="1,3" data="citationList"}
|
||||
column.setCellValueFactory(param ->
|
||||
param.getValue().getProperty(col)
|
||||
);
|
||||
|
||||
column.setCellValueFactory(new MapValueFactory(col));
|
||||
// 启用单元格编辑:ml-citation{ref="4,5" data="citationList"}
|
||||
column.setCellFactory(TextFieldTableCell.forTableColumn());
|
||||
column.setOnEditCommit(event -> {
|
||||
DynamicDataModel row = event.getRowValue();
|
||||
row.getProperty(col).set(event.getNewValue());
|
||||
Map<String, SimpleStringProperty> row = event.getRowValue();
|
||||
row.get(col).set(event.getNewValue());
|
||||
});
|
||||
|
||||
column.prefWidthProperty().bind(tableView.widthProperty().divide(columns.size()));
|
||||
|
||||
tableView.getColumns().add(column);
|
||||
});
|
||||
|
||||
// 填充示例数据:ml-citation{ref="2,3" data="citationList"}
|
||||
// data.add(new DynamicDataModel(Map.of(
|
||||
// "姓名", "张三",
|
||||
// "年龄", "28",
|
||||
// "城市", "北京"
|
||||
// )));
|
||||
data.add(new DynamicDataModel(dataList));
|
||||
|
||||
data.addAll(dataList);
|
||||
this.getChildren().addAll(tableView);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ 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 javafx.beans.property.SimpleMapProperty;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.geometry.Insets;
|
||||
|
|
@ -16,8 +18,10 @@ import javafx.scene.control.TitledPane;
|
|||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.layout.VBox;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -118,9 +122,7 @@ public class SideMenu extends VBox {
|
|||
});
|
||||
|
||||
hbox.setOnMouseClicked(event -> {
|
||||
|
||||
System.out.println("hbox.setOnMouseClicked====>" + data.getUrl());
|
||||
|
||||
//System.out.println("hbox.setOnMouseClicked====>" + data.getUrl());
|
||||
switch (data.getUrl()) {
|
||||
case "/user/list" -> init_table(hbox, ApiUtils.getUserList());
|
||||
case "/role/list" -> init_table(hbox,ApiUtils.getRoleList());
|
||||
|
|
@ -157,7 +159,7 @@ public class SideMenu extends VBox {
|
|||
}
|
||||
|
||||
|
||||
private void init_table(HBox hbox, Map<String, String> dataList) {
|
||||
private void init_table(HBox hbox, List<Map<String, SimpleStringProperty>> dataList) {
|
||||
Scene scene = hbox.getScene();
|
||||
IndexPage root = (IndexPage) scene.getRoot();
|
||||
DynamicTableComponent shortAnswerComponent = new DynamicTableComponent(dataList);
|
||||
|
|
|
|||
Loading…
Reference in New Issue