treeGrid 加节点改弹窗表单:满足多必填列实体 + 顶级 parentid 留空

根因(dogfooding):InputTree 内联加节点只给 label,真实分类实体需 CODE/名称/排序 多必填列,
且顶级 parentid 留空(空串触发引用校验失败)→ 报「类别编码必填」「父引用被删除」。
改:左树「新增分类」弹窗表单(实体可编辑必填列,排序号默认0 + 上级 tree-select 可空),
提交整表单到 add 流、成功重载树;编辑/删除保留内联。实证:弹窗4字段渲染 + 顶级节点 PARENTID=NULL 落库。76测绿

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-14 12:05:02 +08:00
parent 3feefb3f44
commit 4e09dcdd96

View File

@@ -1208,6 +1208,9 @@ public class TemplateToAmisConverter {
// 把该 className 内的 .cxd-Tree max-height 抬到视口相对(calc(100vh-230px))+ 内部滚动。既非定高框 // 把该 className 内的 .cxd-Tree max-height 抬到视口相对(calc(100vh-230px))+ 内部滚动。既非定高框
// 只露一半,也非 heightAuto 随内容无限撑高把页面拉爆(大树数千 px)。 // 只露一半,也非 heightAuto 随内容无限撑高把页面拉爆(大树数千 px)。
treeSchema.put("className", "eip4-treegrid-tree"); treeSchema.put("className", "eip4-treegrid-tree");
final String treeCompId = "treegrid_tree";
treeSchema.put("id", treeCompId);
Map<String, Object> treeAddBtn = null; // 新增分类弹窗按钮(替代内联 creatable,满足多必填列实体)
if (treeTemplate != null && !treeRef.isBlank()) { if (treeTemplate != null && !treeRef.isBlank()) {
treeSchema.put("source", "/api/template/" + treeRef + "/tree"); treeSchema.put("source", "/api/template/" + treeRef + "/tree");
// 选中树节点 → 重载右表(${tree} 进右表 api 过滤);TemplateTreeController 运行时构树 // 选中树节点 → 重载右表(${tree} 进右表 api 过滤);TemplateTreeController 运行时构树
@@ -1216,8 +1219,9 @@ public class TemplateToAmisConverter {
new LinkedHashMap<>(Map.of("actionType", "reload", "componentId", gridCrudId))))))); new LinkedHashMap<>(Map.of("actionType", "reload", "componentId", gridCrudId)))))));
result.note("tree+grid: treetemplate ref=" + treeRef result.note("tree+grid: treetemplate ref=" + treeRef
+ " → input-tree source /api/template/" + treeRef + "/tree(运行时构树)+ 选中重载右表"); + " → input-tree source /api/template/" + treeRef + "/tree(运行时构树)+ 选中重载右表");
// 左树分类节点增删改:treetemplate ref 的 <node><buttons> → amis 原生 creatable/editable/removable // 左树分类节点 CRUD:编辑/删除保留 input-tree 原生内联;新增改弹窗表单(真实实体需 CODE/名称/排序
applyTreeNodeCrud(treeSchema, treeTemplate, treeRef, result); // 等多必填列 + 顶级 parentid 留空,InputTree 内联只给 label 满足不了)→ 返回新增按钮。
treeAddBtn = applyTreeNodeCrud(treeSchema, treeTemplate, treeRef, treeCompId, result);
} }
Map<String, Object> gridSchema = new LinkedHashMap<>(); Map<String, Object> gridSchema = new LinkedHashMap<>();
gridSchema.put("type", "crud"); gridSchema.put("type", "crud");
@@ -1274,8 +1278,10 @@ public class TemplateToAmisConverter {
// (非 flex.items——flex 的 items 必须是渲染器本身,误用会「找不到对应的渲染器」)。 // (非 flex.items——flex 的 items 必须是渲染器本身,误用会「找不到对应的渲染器」)。
Map<String, Object> hbox = new LinkedHashMap<>(); Map<String, Object> hbox = new LinkedHashMap<>();
hbox.put("type", "grid"); hbox.put("type", "grid");
// 左列 body:有新增按钮则 [新增分类按钮, 树],否则仅树
Object leftBody = treeAddBtn != null ? List.of(treeAddBtn, treeSchema) : treeSchema;
hbox.put("columns", List.of( hbox.put("columns", List.of(
new LinkedHashMap<>(Map.of("md", 3, "body", treeSchema)), new LinkedHashMap<>(Map.of("md", 3, "body", leftBody)),
new LinkedHashMap<>(Map.of("md", 9, "body", gridSchema)))); new LinkedHashMap<>(Map.of("md", 9, "body", gridSchema))));
// F2-13:title 优先取 view_template.name // F2-13:title 优先取 view_template.name
String treeGridTitle = templateName != null ? templateName : "树+列表"; String treeGridTitle = templateName != null ? templateName : "树+列表";
@@ -1291,13 +1297,13 @@ public class TemplateToAmisConverter {
* edit 送 {主键:${value}, 名称列:${label}},delete 送 {主键:${value}}。InputTree 配了 source, * edit 送 {主键:${value}, 名称列:${label}},delete 送 {主键:${value}}。InputTree 配了 source,
* 增删改后 amis 自动重拉 source 刷新树。无 <buttons> 或实体未解析 → 跳过(只读树)。 * 增删改后 amis 自动重拉 source 刷新树。无 <buttons> 或实体未解析 → 跳过(只读树)。
*/ */
private void applyTreeNodeCrud(Map<String, Object> treeSchema, Element treeTemplate, String treeRef, private Map<String, Object> applyTreeNodeCrud(Map<String, Object> treeSchema, Element treeTemplate,
ConversionResult result) { String treeRef, String treeCompId, ConversionResult result) {
Element refRoot = loadRefRoot(treeRef, result); Element refRoot = loadRefRoot(treeRef, result);
Element node = refRoot != null ? refRoot.element("node") : null; Element node = refRoot != null ? refRoot.element("node") : null;
Element buttonsEl = node != null ? node.element("buttons") : null; Element buttonsEl = node != null ? node.element("buttons") : null;
if (buttonsEl == null) { if (buttonsEl == null) {
return; return null;
} }
boolean hasInsert = false, hasUpdate = false, hasDelete = false; boolean hasInsert = false, hasUpdate = false, hasDelete = false;
String addSvc = "", deleteSvc = ""; String addSvc = "", deleteSvc = "";
@@ -1319,18 +1325,14 @@ public class TemplateToAmisConverter {
String treeTable = resolveTableName(treeTemplate.attributeValue("entityid", ""), result); String treeTable = resolveTableName(treeTemplate.attributeValue("entityid", ""), result);
if (treeTable == null || treeTable.isBlank()) { if (treeTable == null || treeTable.isBlank()) {
result.degrade("tree+grid: 左树实体未解析 → 跳过节点增删改(树仍可浏览)"); result.degrade("tree+grid: 左树实体未解析 → 跳过节点增删改(树仍可浏览)");
return; return null;
} }
treeTable = treeTable.toUpperCase(); treeTable = treeTable.toUpperCase();
String pk = treeTable + "_ID"; String pk = treeTable + "_ID";
String nameCol = resolveDisplayColumn(treeTable); String nameCol = resolveDisplayColumn(treeTable);
String parentCol = resolveTreeParentCol(refRoot); String parentCol = resolveTreeParentCol(refRoot);
if (hasInsert && !addSvc.isBlank()) { // 编辑/删除:input-tree 原生内联(编辑只改名列,原行其它必填列已存在;删除只送主键)。
treeSchema.put("creatable", true);
treeSchema.put("rootCreatable", true);
treeSchema.put("addApi", flowApi(addSvc, mapOf(nameCol, "${label}", parentCol, "${parent.value}")));
}
if (hasUpdate && !addSvc.isBlank()) { if (hasUpdate && !addSvc.isBlank()) {
treeSchema.put("editable", true); treeSchema.put("editable", true);
treeSchema.put("editApi", flowApi(addSvc, mapOf(pk, "${value}", nameCol, "${label}"))); treeSchema.put("editApi", flowApi(addSvc, mapOf(pk, "${value}", nameCol, "${label}")));
@@ -1339,10 +1341,96 @@ public class TemplateToAmisConverter {
treeSchema.put("removable", true); treeSchema.put("removable", true);
treeSchema.put("deleteApi", flowApi(deleteSvc, mapOf(pk, "${value}"))); treeSchema.put("deleteApi", flowApi(deleteSvc, mapOf(pk, "${value}")));
} }
result.note("tree+grid: 左树节点 CRUD creatable=" + (hasInsert && !addSvc.isBlank()) // 新增:改弹窗表单(真实实体新增需 CODE/名称/排序等多必填列 + 顶级 parentid 留空,
+ " editable=" + (hasUpdate && !addSvc.isBlank()) + " removable=" + (hasDelete && !deleteSvc.isBlank()) // InputTree 内联 creatable 只能给 label 满足不了 → 报「xx 必填」「父引用被删除」)。
Map<String, Object> addBtn = null;
if (hasInsert && !addSvc.isBlank()) {
addBtn = buildTreeNodeAddButton(treeTable, addSvc, parentCol, treeRef, treeCompId, result);
}
result.note("tree+grid: 左树节点 CRUD 新增=弹窗(" + (addBtn != null)
+ ") editable=" + (hasUpdate && !addSvc.isBlank()) + " removable=" + (hasDelete && !deleteSvc.isBlank())
+ "(表=" + treeTable + " 名列=" + nameCol + " 父列=" + parentCol + "(表=" + treeTable + " 名列=" + nameCol + " 父列=" + parentCol
+ " add=" + addSvc + " del=" + deleteSvc + ")"); + " add=" + addSvc + " del=" + deleteSvc + ")");
return addBtn;
}
/**
* 左树「新增分类」弹窗按钮:表单含实体可编辑必填列(排序号默认 0)+ 上级选择器(tree-select,
* 留空=顶级、省略 parentid 避免空引用校验失败)。提交整表单到 add 服务流,成功后重载左树。
*/
private Map<String, Object> buildTreeNodeAddButton(String treeTable, String addSvc, String parentCol,
String treeRef, String treeCompId, ConversionResult result) {
List<Map<String, Object>> fields = buildTreeAddFormFields(treeTable, parentCol, result);
// 上级选择器:tree-select 复用左树同源数据;clearable,留空即顶级(amis 不下发空值 → 引擎按 null 建顶级)。
Map<String, Object> parentField = new LinkedHashMap<>();
parentField.put("type", "tree-select");
parentField.put("name", parentCol);
parentField.put("label", "上级分类");
parentField.put("source", "/api/template/" + treeRef + "/tree");
parentField.put("clearable", true);
parentField.put("placeholder", "留空 = 顶级分类");
fields.add(parentField);
Map<String, Object> form = new LinkedHashMap<>();
form.put("type", "form");
form.put("title", "新增分类");
form.put("api", "post:/api/flow/" + addSvc); // 送整表单(含各必填列 + 选填 parentid)
form.put("reload", treeCompId); // 成功后重载左树
form.put("body", fields);
Map<String, Object> dialog = new LinkedHashMap<>();
dialog.put("title", "新增分类");
dialog.put("body", form);
Map<String, Object> btn = new LinkedHashMap<>();
btn.put("type", "button");
btn.put("label", "新增分类");
btn.put("level", "primary");
btn.put("icon", "fa fa-plus");
btn.put("className", "m-b-sm");
btn.put("actionType", "dialog");
btn.put("dialog", dialog);
return btn;
}
/** 树实体可编辑必填列 → amis 表单字段(排除主键/审计/version/isactive/父列;排序类用 input-number 默认 0)。 */
private List<Map<String, Object>> buildTreeAddFormFields(String treeTable, String parentCol,
ConversionResult result) {
List<Map<String, Object>> fields = new ArrayList<>();
List<Map<String, Object>> cols;
try {
cols = jdbcTemplate.queryForList(
"SELECT TRIM(a.columnname) AS col, TRIM(a.displayname) AS dn"
+ " FROM sys_entityattribute a JOIN sys_entity e ON TRIM(a.entityid)=TRIM(e.sys_entity_id)"
+ " WHERE UPPER(TRIM(e.tablename))=? AND a.ismandatory=1 AND a.iskey=0"
+ " AND COALESCE(a.isreadonly,0)=0 AND a.isactive=1"
+ " AND UPPER(TRIM(a.columnname)) NOT IN"
+ " ('CREATETIME','CREATEBY','UPDATETIME','UPDATEBY','VERSION','ISACTIVE')"
+ " AND UPPER(TRIM(a.columnname))<>? ORDER BY a.entitydisplayseq NULLS LAST",
treeTable.toUpperCase(), parentCol == null ? "" : parentCol.toUpperCase());
} catch (Exception e) {
result.degrade("tree+grid: 新增表单字段查询失败(" + treeTable + ") → 仅上级选择器");
return fields;
}
for (Map<String, Object> c : cols) {
String col = c.get("col") == null ? "" : c.get("col").toString();
String dn = c.get("dn") == null ? "" : c.get("dn").toString();
if (col.isBlank()) {
continue;
}
boolean isSort = col.toUpperCase().matches(".*(SORT|SEQ|ORDER).*")
|| dn.contains("排序") || dn.contains("顺序");
Map<String, Object> f = new LinkedHashMap<>();
f.put("type", isSort ? "input-number" : "input-text");
f.put("name", col);
f.put("label", dn.isBlank() ? col : dn);
f.put("required", true);
if (isSort) {
f.put("value", 0);
}
fields.add(f);
}
return fields;
} }
/** 取被引用模板(view_template)的根元素;失败/空返 null。treegrid 列定义、右表按钮、左树节点 CRUD 共用。 */ /** 取被引用模板(view_template)的根元素;失败/空返 null。treegrid 列定义、右表按钮、左树节点 CRUD 共用。 */