表格添加自定义回调 2025年3月19日12:26:51
parent
7e9e164d91
commit
114181a823
|
|
@ -7,14 +7,19 @@ import com.alibaba.fastjson.JSON;
|
|||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import com.zhangmeng.online.exam.ui.admin.IndexPage;
|
||||
import com.zhangmeng.online.exam.ui.api.form.RoleForm;
|
||||
import com.zhangmeng.online.exam.ui.api.form.UserForm;
|
||||
import com.zhangmeng.online.exam.ui.api.model.*;
|
||||
import com.zhangmeng.online.exam.ui.components.DynamicTableComponent;
|
||||
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;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.scene.control.ContextMenu;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.scene.control.TableView;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
|
@ -101,6 +106,14 @@ public class ApiUtils {
|
|||
return dataLoad.loadData(PAGE_NUM,PAGE_SIZE);
|
||||
}
|
||||
|
||||
public static Map<String, SimpleStringProperty> selectedItem(MenuItem menuItem){
|
||||
ContextMenu parentPopup = menuItem.getParentPopup();
|
||||
IndexPage indexPage = (IndexPage)parentPopup.getStyleableParent();
|
||||
DynamicTableComponent center = (DynamicTableComponent)indexPage.getCenter();
|
||||
TableView<Map<String, SimpleStringProperty>> tableView = center.getTableView();
|
||||
return tableView.getSelectionModel().getSelectedItem();
|
||||
}
|
||||
|
||||
public static class DataView {
|
||||
|
||||
private List<String> keys;
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@ package com.zhangmeng.online.exam.ui.api;
|
|||
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
|
@ -27,4 +29,6 @@ public interface DataLoad {
|
|||
public void editData(String s, Stage stage);
|
||||
|
||||
public void updateData(String id, String name, String oldValue, String newValue);
|
||||
|
||||
List<MenuItem> getContextMenu();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import com.zhangmeng.online.exam.ui.utils.AlertUtils;
|
|||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.util.*;
|
||||
|
|
@ -128,4 +129,9 @@ public class AnswerDataLoad implements DataLoad {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuItem> getContextMenu() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import com.zhangmeng.online.exam.ui.utils.AlertUtils;
|
|||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.util.*;
|
||||
|
|
@ -110,4 +111,9 @@ public class OptionDataLoad implements DataLoad {
|
|||
public void updateData(String id, String name, String oldValue, String newValue) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuItem> getContextMenu() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,13 +3,20 @@ 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.admin.IndexPage;
|
||||
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
||||
import com.zhangmeng.online.exam.ui.api.DataLoad;
|
||||
import com.zhangmeng.online.exam.ui.api.form.UserForm;
|
||||
import com.zhangmeng.online.exam.ui.components.DynamicTableComponent;
|
||||
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
|
||||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.css.Styleable;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.control.ContextMenu;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.scene.control.TableView;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.util.*;
|
||||
|
|
@ -34,23 +41,21 @@ public class PaperDataLoad implements DataLoad {
|
|||
int total = jsonObject.getIntValue("total");
|
||||
List<Map<String, SimpleStringProperty>> userMapList = new ArrayList<>();
|
||||
for (Object datum : data) {
|
||||
JSONObject user = (JSONObject) datum;
|
||||
JSONObject paper = (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("phone")));
|
||||
userMap.put("电子邮箱", new SimpleStringProperty(user.getString("email")));
|
||||
|
||||
userMap.put("用户名_username", new SimpleStringProperty(user.getString("username")));
|
||||
userMap.put("手机号码_phone", new SimpleStringProperty(user.getString("phone")));
|
||||
userMap.put("电子邮箱_email", new SimpleStringProperty(user.getString("email")));
|
||||
|
||||
userMap.put("id", new SimpleStringProperty(user.getString("id")));
|
||||
userMap.put("序号", new SimpleStringProperty(paper.getString("id")));
|
||||
userMap.put("试卷名称", new SimpleStringProperty(paper.getString("name")));
|
||||
userMap.put("创建人", new SimpleStringProperty(paper.getString("creator_username")));
|
||||
userMap.put("学科名称", new SimpleStringProperty(paper.getString("subject_name")));
|
||||
userMap.put("总分", new SimpleStringProperty(paper.getString("totalScore")));
|
||||
userMap.put("考试时间", new SimpleStringProperty(paper.getString("examTime")));
|
||||
userMap.put("id", new SimpleStringProperty(paper.getString("id")));
|
||||
userMapList.add(userMap);
|
||||
}
|
||||
|
||||
ApiUtils.DataView dataView = new ApiUtils.DataView(Arrays.asList("序号", "用户名","手机号码", "电子邮箱" ), userMapList).setDataLoad(this).setPageNum(pageNum).setPageSize(pageSize).setTotal(total);
|
||||
ApiUtils.DataView dataView = new ApiUtils.DataView(Arrays.asList("序号", "试卷名称", "创建人", "学科名称", "总分", "考试时间"), userMapList).setDataLoad(this).setPageNum(pageNum).setPageSize(pageSize).setTotal(total);
|
||||
dataView.setDataLoad(this);
|
||||
|
||||
return dataView;
|
||||
}
|
||||
|
||||
|
|
@ -68,9 +73,9 @@ public class PaperDataLoad implements DataLoad {
|
|||
public Map<String, Object> saveForm(Map<String, Object> map) {
|
||||
String result = HttpUtils.POST(ApiUtils.API_URL + "/user/save", map);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
if (jsonObject.getIntValue("code") == 200){
|
||||
if (jsonObject.getIntValue("code") == 200) {
|
||||
AlertUtils.alert_msg("保存成功!");
|
||||
}else {
|
||||
} else {
|
||||
AlertUtils.alert_warning("保存失败!");
|
||||
}
|
||||
return null;
|
||||
|
|
@ -82,9 +87,9 @@ public class PaperDataLoad implements DataLoad {
|
|||
map.put("id", id);
|
||||
String result = HttpUtils.POST(ApiUtils.API_URL + "/user/delete", map);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
if (jsonObject.getIntValue("code") == 200){
|
||||
if (jsonObject.getIntValue("code") == 200) {
|
||||
AlertUtils.alert_msg("删除成功!");
|
||||
}else {
|
||||
} else {
|
||||
AlertUtils.alert_warning("删除失败!");
|
||||
}
|
||||
}
|
||||
|
|
@ -121,11 +126,25 @@ public class PaperDataLoad implements DataLoad {
|
|||
map.put("newValue", newValue);
|
||||
String result = HttpUtils.POST(ApiUtils.API_URL + "/user/update", map);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
if (jsonObject.getIntValue("code") == 200){
|
||||
if (jsonObject.getIntValue("code") == 200) {
|
||||
AlertUtils.alert_msg("更新成功!");
|
||||
}else {
|
||||
} else {
|
||||
AlertUtils.alert_warning("更新失败!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuItem> getContextMenu() {
|
||||
MenuItem group_volumes = new MenuItem("组卷");
|
||||
group_volumes.setOnAction(event -> {
|
||||
Map<String, SimpleStringProperty> map = ApiUtils.selectedItem(group_volumes);
|
||||
String id = map.get("id").getValue();
|
||||
AlertUtils.alert_msg("组卷正在开发中..." + id);
|
||||
});
|
||||
|
||||
return Arrays.asList(group_volumes);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import com.zhangmeng.online.exam.ui.api.DataLoad;
|
|||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.util.*;
|
||||
|
|
@ -73,4 +74,9 @@ public class PermissionDataLoad implements DataLoad {
|
|||
public void updateData(String id, String name, String oldValue, String newValue) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuItem> getContextMenu() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import com.zhangmeng.online.exam.ui.api.DataLoad;
|
|||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.util.*;
|
||||
|
|
@ -73,4 +74,10 @@ public class QuestionDataLoad implements DataLoad {
|
|||
public void updateData(String id, String name, String oldValue, String newValue) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<MenuItem> getContextMenu() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import com.zhangmeng.online.exam.ui.utils.AlertUtils;
|
|||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.util.*;
|
||||
|
|
@ -110,4 +111,9 @@ public class QuestionOptionDataLoad implements DataLoad {
|
|||
public void updateData(String id, String name, String oldValue, String newValue) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuItem> getContextMenu() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import com.zhangmeng.online.exam.ui.api.DataLoad;
|
|||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.util.*;
|
||||
|
|
@ -74,4 +75,9 @@ public class RoleDataLoad implements DataLoad {
|
|||
public void updateData(String id, String name, String oldValue, String newValue) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuItem> getContextMenu() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import com.zhangmeng.online.exam.ui.utils.AlertUtils;
|
|||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.util.*;
|
||||
|
|
@ -109,4 +110,9 @@ public class SubjectDataLoad implements DataLoad {
|
|||
public void updateData(String id, String name, String oldValue, String newValue) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuItem> getContextMenu() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import java.util.*;
|
|||
|
||||
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
|
|
@ -129,4 +130,13 @@ public class UserDataLoad implements DataLoad {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<MenuItem> getContextMenu() {
|
||||
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.zhangmeng.online.exam.ui.components;
|
||||
|
||||
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
||||
import com.zhangmeng.online.exam.ui.api.DataLoad;
|
||||
import com.zhangmeng.online.exam.ui.components.callBack.DynamicTableComponentCallBack;
|
||||
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
|
|
@ -35,6 +36,26 @@ public class DynamicTableComponent extends VBox {
|
|||
|
||||
private DynamicTableComponentCallBack callBack;
|
||||
|
||||
private TableView<Map<String, SimpleStringProperty>> tableView;
|
||||
|
||||
public TableView<Map<String, SimpleStringProperty>> getTableView() {
|
||||
return tableView;
|
||||
}
|
||||
|
||||
public void setTableView(TableView<Map<String, SimpleStringProperty>> tableView) {
|
||||
this.tableView = tableView;
|
||||
}
|
||||
|
||||
private DataLoad dataLoad;
|
||||
|
||||
public DataLoad getDataLoad() {
|
||||
return dataLoad;
|
||||
}
|
||||
|
||||
public void setDataLoad(DataLoad dataLoad) {
|
||||
this.dataLoad = dataLoad;
|
||||
}
|
||||
|
||||
public DynamicTableComponentCallBack getCallBack() {
|
||||
return callBack;
|
||||
}
|
||||
|
|
@ -52,22 +73,23 @@ public class DynamicTableComponent extends VBox {
|
|||
// public DynamicTableComponent(List<Map<String, SimpleStringProperty>> dataList,List<String> keys) {
|
||||
public DynamicTableComponent(ApiUtils.DataView dataView) {
|
||||
super();
|
||||
this.dataLoad = dataView.getDataLoad();
|
||||
List<Map<String, SimpleStringProperty>> dataList = dataView.getDataList();
|
||||
List<String> keys =dataView.getKeys();
|
||||
TableView<Map<String, SimpleStringProperty>> tableView = new TableView<>();
|
||||
List<String> keys = dataView.getKeys();
|
||||
tableView = new TableView<>();
|
||||
tableView.setItems(data);
|
||||
tableView.setEditable(true);
|
||||
tableView.prefHeightProperty().bind(this.heightProperty());
|
||||
tableView.prefWidthProperty().bind(this.widthProperty());
|
||||
// 动态生成列(示例字段):ml-citation{ref="3,4" data="citationList"}
|
||||
List<String> columns = keys.stream().filter(key -> !key.equals("id")).toList() ;
|
||||
List<String> columns = keys.stream().filter(key -> !key.equals("id")).toList();
|
||||
// if (!dataList.isEmpty()){
|
||||
// columns= Arrays.asList(dataList.get(0).keySet().toArray(new String[0])).stream().filter(key -> !key.equals("id")).toList();
|
||||
// }else {
|
||||
// columns=keys;
|
||||
// }
|
||||
columns.forEach(col -> {
|
||||
TableColumn<Map<String,SimpleStringProperty>, String> column = new TableColumn<>(col);
|
||||
TableColumn<Map<String, SimpleStringProperty>, String> column = new TableColumn<>(col);
|
||||
// 数据绑定:ml-citation{ref="1,3" data="citationList"}
|
||||
column.setCellValueFactory(new MapValueFactory(col));
|
||||
// 启用单元格编辑:ml-citation{ref="4,5" data="citationList"}
|
||||
|
|
@ -79,21 +101,21 @@ public class DynamicTableComponent extends VBox {
|
|||
for (Map.Entry<String, SimpleStringProperty> entry : entries) {
|
||||
String key = entry.getKey();
|
||||
SimpleStringProperty value = entry.getValue();
|
||||
if (key.startsWith(col+"_")){
|
||||
if (key.startsWith(col + "_")) {
|
||||
name = key.split("_")[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (name != null){
|
||||
if (name != null) {
|
||||
// 刷新数据
|
||||
String newValue = event.getNewValue();
|
||||
String oldValue = event.getOldValue();
|
||||
// 调用接口修改数据
|
||||
callBack.update(DynamicTableComponent.this,row.get("id").get(),name, oldValue, newValue);
|
||||
callBack.update(DynamicTableComponent.this, row.get("id").get(), name, oldValue, newValue);
|
||||
// 刷新界面
|
||||
row.get(col).set(event.getNewValue());
|
||||
|
||||
}else {
|
||||
} else {
|
||||
AlertUtils.alert_msg("该列不允许修改!");
|
||||
}
|
||||
});
|
||||
|
|
@ -112,9 +134,9 @@ public class DynamicTableComponent extends VBox {
|
|||
@Override
|
||||
public void handle(ActionEvent event) {
|
||||
Map<String, SimpleStringProperty> selectedItem = tableView.getSelectionModel().getSelectedItem();
|
||||
if (selectedItem!= null) {
|
||||
if (selectedItem != null) {
|
||||
SimpleStringProperty id = selectedItem.get("id");
|
||||
if (id!= null) {
|
||||
if (id != null) {
|
||||
callBack.edit(id.get(), DynamicTableComponent.this);
|
||||
}
|
||||
}
|
||||
|
|
@ -126,16 +148,20 @@ public class DynamicTableComponent extends VBox {
|
|||
@Override
|
||||
public void handle(ActionEvent event) {
|
||||
Map<String, SimpleStringProperty> selectedItem = tableView.getSelectionModel().getSelectedItem();
|
||||
if (selectedItem!= null) {
|
||||
if (selectedItem != null) {
|
||||
SimpleStringProperty id = selectedItem.get("id");
|
||||
if (id!= null) {
|
||||
if (id != null) {
|
||||
callBack.delete(id.get());
|
||||
}
|
||||
}
|
||||
data.remove(tableView.getSelectionModel().getSelectedItem());
|
||||
}
|
||||
});
|
||||
contextMenu.getItems().addAll(editItem,deleteItem);
|
||||
|
||||
if (dataLoad.getContextMenu() != null && !dataLoad.getContextMenu().isEmpty()){
|
||||
contextMenu.getItems().addAll(dataLoad.getContextMenu());
|
||||
}
|
||||
contextMenu.getItems().addAll(editItem, deleteItem);
|
||||
tableView.setContextMenu(contextMenu);
|
||||
tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);//允许一次选择一个或多个连续的索引范围。
|
||||
|
||||
|
|
@ -143,7 +169,7 @@ public class DynamicTableComponent extends VBox {
|
|||
Button button = new Button("新增");
|
||||
button.setOnAction(event -> {
|
||||
Stage window = (Stage) this.getScene().getWindow();
|
||||
callBack.Add(callBack.getAddForm(), window,this);
|
||||
callBack.Add(callBack.getAddForm(), window, this);
|
||||
});
|
||||
this.getChildren().add(button);
|
||||
this.setSpacing(10);
|
||||
|
|
@ -162,7 +188,7 @@ public class DynamicTableComponent extends VBox {
|
|||
public void handle(ActionEvent event) {
|
||||
// System.out.println("当前选择页码:"+paginationPicker.getCurrentPage());
|
||||
data.clear();
|
||||
data.addAll(callBack.flushData(paginationPicker.getCurrentPage(),paginationPicker.getPageSize()).getDataList());
|
||||
data.addAll(callBack.flushData(paginationPicker.getCurrentPage(), paginationPicker.getPageSize()).getDataList());
|
||||
}
|
||||
});
|
||||
this.getChildren().add(paginationPicker);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
package com.zhangmeng.online.exam.ui.components;
|
||||
|
||||
/**
|
||||
*
|
||||
* 试题展示组件
|
||||
* @author zm
|
||||
* @date 2025/3/19 12:22
|
||||
* @version: 1.0
|
||||
*/
|
||||
public class PaperViewComponent {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -132,7 +132,7 @@ public class SideMenu extends VBox {
|
|||
case "/subject/list" -> init_table(hbox, ApiUtils.getSubjectList());
|
||||
case "/option/list" -> init_table(hbox, ApiUtils.getQuestionOptionList());
|
||||
case "/answer/list" -> init_table(hbox, ApiUtils.getAnswerList());
|
||||
case "/exam/paper" -> init_table(hbox, ApiUtils.getExamPaper());
|
||||
case "/paper/list" -> init_table(hbox, ApiUtils.getExamPaper());
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue