From 3b416e8f7077a6630a1de8392f7ba25a43e9377a Mon Sep 17 00:00:00 2001 From: zhengwr Date: Mon, 15 Jun 2026 12:17:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(editgrid):=20A3=20=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E3=80=8C=E6=B7=BB=E5=8A=A0=E7=94=A8=E5=93=81=E3=80=8D=E2=80=94?= =?UTF-8?q?=E2=80=94MultiSelectDialog=20onlybutton=20=E2=86=92=20=E5=A4=9A?= =?UTF-8?q?=E9=80=89=E5=BC=B9=E7=AA=97=E8=BF=BD=E5=8A=A0=E5=AD=90=E8=A1=A8?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 老模板里 onlybutton 的 MultiSelectDialog(如采购申请「添加用品」)是「批量选用品 → 追加明细行」的 按钮,原映射成普通多选 picker 表单项不可用。改建为:按钮 → 选择弹窗(crud 多选,走 /api/picker/ 含规格/单位/现存量列) → 确认把选中行 addItem 追加到本页子表 input-table。 机制(浏览器实证): - UiTypeMapper.buildEditGridAddButton:onlybutton MultiSelectDialog → 按钮+选择弹窗(crud selectable multiple);crud onEvent.selectedChange → setValue 把 ${event.data.selectedItems} 存进弹窗 scope 变量 __egpicked(amis 不经 componentId 暴露 crud.selectedItems,故经事件落 scope);确认按钮 onEvent.click = [addItem ${ARRAYMAP(__egpicked, item => ({...item}))}, closeDialog](closeDialog 仅关本弹窗、不连父表单 dialog;括号包对象字面量——箭头函数仅认单表达式)。 - TemplateToAmisConverter.wireEditGridAddButtons:后置把 addItem 的 componentId 占位符替换为本页 input-table 真实 id(eg-)。 - 关键坑:amis 表达式 ${id.x} 里连字符被当减法 → crud/弹窗 id 一律无连字符。 实证:采购「添加用品」弹出 123 用品多选→确认→子表追加 2 行(GOODS_ID 引用列 + 计量单位 + 现存量), 父表单 dialog 保持打开。converter 108 测试绿。 注:引用列映射(GOODS_ID←OA_OFFICE_GOODS_ID 等 picker<实体>_ID 列)是 per-entity 的,本批用 raw-spread ({...item}) 兜底(同名列规格/单位直接带);采购实例的精确映射已在 live schema 验证(recipe 记于项目记忆)。 Co-Authored-By: Claude Opus 4.8 (1M context) --- .../converter/TemplateToAmisConverter.java | 56 ++++++++++++ .../eip4/platform/converter/UiTypeMapper.java | 86 +++++++++++++++++++ 2 files changed, 142 insertions(+) diff --git a/platform-converter/src/main/java/com/eip4/platform/converter/TemplateToAmisConverter.java b/platform-converter/src/main/java/com/eip4/platform/converter/TemplateToAmisConverter.java index e29a948..bc581e0 100644 --- a/platform-converter/src/main/java/com/eip4/platform/converter/TemplateToAmisConverter.java +++ b/platform-converter/src/main/java/com/eip4/platform/converter/TemplateToAmisConverter.java @@ -1096,9 +1096,65 @@ public class TemplateToAmisConverter { Map pageSchema = page(formTitle, formSchema); // A1:把 editgrid 行内脚本 onEvent 里的 templateId 占位符替换为真实 view_page_id(currentTemplateId)。 injectEditGridTemplateId(pageSchema, currentTemplateId); + // A3:把「添加用品」按钮 addItem 的目标占位符替换为本页子表 input-table 的真实 id。 + wireEditGridAddButtons(pageSchema); return pageSchema; } + /** A3:找页面里第一个 input-table 的 id,替换所有「添加按钮」addItem 的目标占位符,并清掉 marker。 */ + @SuppressWarnings("unchecked") + private static void wireEditGridAddButtons(Object root) { + String inputTableId = findFirstInputTableId(root); + if (inputTableId == null) { + return; // 无子表 → 添加按钮无目标(仍保留按钮,addItem 占位但不致命) + } + replaceAndStripAddTarget(root, inputTableId); + } + + @SuppressWarnings("unchecked") + private static String findFirstInputTableId(Object node) { + if (node instanceof Map m) { + Map map = (Map) m; + if ("input-table".equals(map.get("type")) && map.get("id") instanceof String s) { + return s; + } + for (Object v : map.values()) { + String r = findFirstInputTableId(v); + if (r != null) { + return r; + } + } + } else if (node instanceof List l) { + for (Object it : l) { + String r = findFirstInputTableId(it); + if (r != null) { + return r; + } + } + } + return null; + } + + @SuppressWarnings("unchecked") + private static void replaceAndStripAddTarget(Object node, String inputTableId) { + if (node instanceof Map m) { + Map map = (Map) m; + map.remove(UiTypeMapper.EDITGRID_ADD_TARGET_PLACEHOLDER); // 清 marker + for (Map.Entry e : map.entrySet()) { + Object v = e.getValue(); + if (v instanceof String s && UiTypeMapper.EDITGRID_ADD_TARGET_PLACEHOLDER.equals(s)) { + e.setValue(inputTableId); + } else { + replaceAndStripAddTarget(v, inputTableId); + } + } + } else if (node instanceof List l) { + for (Object it : l) { + replaceAndStripAddTarget(it, inputTableId); + } + } + } + /** A1:递归把 schema 里的 {@link UiTypeMapper#EDITGRID_TPL_PLACEHOLDER} 替换成真实 templateId。 */ @SuppressWarnings("unchecked") private static void injectEditGridTemplateId(Object node, String templateId) { diff --git a/platform-converter/src/main/java/com/eip4/platform/converter/UiTypeMapper.java b/platform-converter/src/main/java/com/eip4/platform/converter/UiTypeMapper.java index 1c1b769..2796443 100644 --- a/platform-converter/src/main/java/com/eip4/platform/converter/UiTypeMapper.java +++ b/platform-converter/src/main/java/com/eip4/platform/converter/UiTypeMapper.java @@ -230,6 +230,16 @@ final class UiTypeMapper { break; } String dialogId = f.attributeValue("dialogid", ""); + // A3:onlybutton=true 的 MultiSelectDialog(如「添加用品」)是「批量选择 → 追加子表行」的按钮, + // 非普通多选 picker 表单项。映射为按钮,点开选择弹窗(crud 多选),确认后 addItem 到本页子表(input-table)。 + // 目标 input-table id 由 convertViewPage 后置注入(此处占位)。 + if ("true".equals(f.attributeValue("onlybutton")) && !dialogId.isBlank()) { + buildEditGridAddButton(item, f, dialogId); + result.note(origin + "@field " + name + " uitype=MultiSelectDialog(onlybutton,dialogid=" + + dialogId + ") → 「" + f.attributeValue("buttoncaption", "添加") + + "」按钮 → 选择弹窗 → addItem 追加子表行"); + break; + } if (dialogId.isBlank()) { applySelectDialogFallback(item, "", true); result.degrade(origin + "@field " + name @@ -416,6 +426,82 @@ final class UiTypeMapper { return "eg-" + editgridField; } + /** A3:「添加用品」按钮 addItem 目标 input-table id 的占位符;由 convertViewPage 后置注入本页子表 id。 */ + public static final String EDITGRID_ADD_TARGET_PLACEHOLDER = "__EDITGRID_ADD_TARGET__"; + + /** + * A3:把 onlybutton 的 MultiSelectDialog(如「添加用品」)建成「按钮 → 选择弹窗(crud 多选) → 确认 addItem 追加子表行」。 + * 弹窗内 crud 走 {@code /api/picker//list}(与 picker 同数据源,含用品规格/单位/现存量等列); + * 确认时把选中行经 ARRAYMAP 映射成子表行 addItem 到目标 input-table(componentId 后置注入)。 + */ + @SuppressWarnings("unchecked") + private static void buildEditGridAddButton(Map item, Element f, String dialogId) { + // 清掉默认表单项壳,重建为按钮 + String name = String.valueOf(item.get("name")); + String label = (String) item.get("label"); + item.keySet().removeIf(k -> !"name".equals(k) && !"label".equals(k)); + item.put("type", "button"); + item.put("label", f.attributeValue("buttoncaption", label != null ? label : "添加")); + item.put("level", "primary"); + item.put("actionType", "dialog"); + + // id 必须无连字符——amis 表达式 ${id.x} 里的连字符会被当成减法解析。 + String sanitized = name.replaceAll("[^A-Za-z0-9]", ""); + String dialogIdAttr = "egadddlg" + sanitized; // 选择弹窗 id(selectedChange 把选中行存进它的数据域) + + Map searchBox = new LinkedHashMap<>(Map.of( + "type", "search-box", "name", "q", "placeholder", "输入关键字搜索")); + Map crud = new LinkedHashMap<>(); + crud.put("type", "crud"); + crud.put("api", "/api/picker/" + dialogId + "/list"); + crud.put("mode", "table"); + crud.put("syncLocation", false); + crud.put("selectable", true); + crud.put("multiple", true); + crud.put("keepItemSelectionOnPageChange", true); + crud.put("primaryField", "_id"); + crud.put("headerToolbar", List.of(searchBox)); + crud.put("footerToolbar", List.of("pagination")); + // crud 选中变化 → 把选中行集存进弹窗数据域变量 __egpicked(amis 不经 componentId 暴露 crud.selectedItems, + // 故经 selectedChange 事件 setValue 落到弹窗 scope,供确认按钮读取)。 + Map stash = new LinkedHashMap<>(); + stash.put("actionType", "setValue"); + stash.put("componentId", dialogIdAttr); + stash.put("args", new LinkedHashMap<>(Map.of( + "value", new LinkedHashMap<>(Map.of("__egpicked", "${event.data.selectedItems}"))))); + crud.put("onEvent", new LinkedHashMap<>(Map.of( + "selectedChange", new LinkedHashMap<>(Map.of("actions", List.of(stash)))))); + + // 确认:addItem 把选中行 raw-spread 成子表行(同名列规格/单位等直接带;引用列如 GOODS_ID 取选中行的 + // <实体>_ID 需 per-entity 映射,本批 raw-spread 兜底),再 closeDialog 仅关本选择弹窗(不连父表单 dialog)。 + Map addItem = new LinkedHashMap<>(); + addItem.put("actionType", "addItem"); + addItem.put("componentId", EDITGRID_ADD_TARGET_PLACEHOLDER); + // 括号包对象字面量——amis 箭头函数仅认单表达式,{..} 不括会被当成块语句(返回 undefined)。 + addItem.put("args", new LinkedHashMap<>(Map.of( + "item", "${ARRAYMAP(__egpicked, item => ({...item}))}"))); + Map closeDlg = new LinkedHashMap<>(Map.of("actionType", "closeDialog")); + Map confirmBtn = new LinkedHashMap<>(); + confirmBtn.put("type", "button"); + confirmBtn.put("label", "确定"); + confirmBtn.put("level", "primary"); + confirmBtn.put("onEvent", new LinkedHashMap<>(Map.of( + "click", new LinkedHashMap<>(Map.of("actions", List.of(addItem, closeDlg)))))); + Map cancelBtn = new LinkedHashMap<>(Map.of( + "type", "button", "label", "取消", "actionType", "close")); + + Map dialog = new LinkedHashMap<>(); + dialog.put("type", "dialog"); + dialog.put("id", dialogIdAttr); + dialog.put("title", f.attributeValue("windowcaption", "选择")); + dialog.put("size", "lg"); + dialog.put("body", crud); + dialog.put("actions", List.of(cancelBtn, confirmBtn)); + item.put("dialog", dialog); + // marker:convertViewPage 据此把 addItem 的 componentId 占位符替换为本页子表 input-table id + item.put(EDITGRID_ADD_TARGET_PLACEHOLDER, true); + } + /** * A1:构造 editgrid 子列 {@code quickEdit.onEvent.change} 动作链—— * ① ajax 调 {@code /api/script/execute-editgrid-row}(传 templateId/editgridField/scriptKey/