fix framework treegrid search and formeditgrid editors
This commit is contained in:
@@ -55,6 +55,7 @@ public class TemplateToAmisConverter {
|
||||
*/
|
||||
private String currentEngineclass;
|
||||
private static final String ENTITY_FORM_PREFIX = "ENTITY_FORM:";
|
||||
private static final Pattern IDENT = Pattern.compile("[A-Za-z_][A-Za-z0-9_]*");
|
||||
private static final Set<String> SYSTEM_FORM_COLUMNS = Set.of(
|
||||
"CREATETIME", "CREATEBY", "UPDATETIME", "UPDATEBY", "VERSION", "ISACTIVE");
|
||||
|
||||
@@ -506,7 +507,7 @@ public class TemplateToAmisConverter {
|
||||
}
|
||||
Map<String, Object> searchItem = UiTypeMapper.mapField(f, result, "search", colMeta);
|
||||
enrichFieldRefs(searchItem, f, result);
|
||||
inlineEnumerateOptions(searchItem, "search", result);
|
||||
inlineEnumerateOptions(searchItem, "search", result, colMeta);
|
||||
items.add(searchItem);
|
||||
}
|
||||
if (items.isEmpty()) {
|
||||
@@ -519,7 +520,58 @@ public class TemplateToAmisConverter {
|
||||
return filter;
|
||||
}
|
||||
|
||||
private void inlineEnumerateOptions(Map<String, Object> item, String origin, ConversionResult result) {
|
||||
private static String appendFilterParamsToApi(String api, Map<String, Object> filter) {
|
||||
if (api == null || api.isBlank() || filter == null) {
|
||||
return api;
|
||||
}
|
||||
LinkedHashSet<String> names = new LinkedHashSet<>();
|
||||
collectFilterNames(filter.get("body"), names);
|
||||
String out = api;
|
||||
for (String name : names) {
|
||||
if (name == null || !IDENT.matcher(name).matches() || apiHasQueryParam(out, name)) {
|
||||
continue;
|
||||
}
|
||||
out += (out.contains("?") ? "&" : "?") + name + "=${" + name + "}";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void collectFilterNames(Object node, Set<String> out) {
|
||||
if (node instanceof List<?> list) {
|
||||
for (Object item : list) {
|
||||
collectFilterNames(item, out);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!(node instanceof Map<?, ?> raw)) {
|
||||
return;
|
||||
}
|
||||
Map<String, Object> map = (Map<String, Object>) raw;
|
||||
Object name = map.get("name");
|
||||
if (name instanceof String s && !s.isBlank()) {
|
||||
out.add(s.trim());
|
||||
}
|
||||
collectFilterNames(map.get("body"), out);
|
||||
}
|
||||
|
||||
private static boolean apiHasQueryParam(String api, String name) {
|
||||
int idx = api.indexOf('?');
|
||||
if (idx < 0 || idx == api.length() - 1) {
|
||||
return false;
|
||||
}
|
||||
for (String part : api.substring(idx + 1).split("&")) {
|
||||
int eq = part.indexOf('=');
|
||||
String key = eq >= 0 ? part.substring(0, eq) : part;
|
||||
if (name.equals(key)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void inlineEnumerateOptions(Map<String, Object> item, String origin, ConversionResult result,
|
||||
Map<String, String[]> colMeta) {
|
||||
if (!"select".equals(item.get("type")) || !(item.get("source") instanceof String source)) {
|
||||
return;
|
||||
}
|
||||
@@ -535,12 +587,69 @@ public class TemplateToAmisConverter {
|
||||
if (options.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
String name = String.valueOf(item.getOrDefault("name", "")).toUpperCase();
|
||||
String[] meta = colMeta == null ? null : colMeta.get(name);
|
||||
if (meta != null && meta.length > 0 && "BOOLEAN".equalsIgnoreCase(String.valueOf(meta[0]))) {
|
||||
options = booleanOptions(options);
|
||||
if (item.containsKey("value")) {
|
||||
Boolean bool = toBooleanOptionValue(item.get("value"));
|
||||
if (bool != null) {
|
||||
item.put("value", bool);
|
||||
}
|
||||
}
|
||||
}
|
||||
item.remove("source");
|
||||
item.put("options", options);
|
||||
result.note(origin + "@field " + item.getOrDefault("name", "")
|
||||
+ " 枚举字典 " + dict + " → 内联 options(" + options.size() + "项),避免远程下拉空载");
|
||||
}
|
||||
|
||||
private static List<Map<String, Object>> booleanOptions(List<Map<String, Object>> options) {
|
||||
List<Map<String, Object>> out = new ArrayList<>();
|
||||
for (Map<String, Object> option : options) {
|
||||
Object raw = option.get("value");
|
||||
Boolean bool = toBooleanOptionValue(raw);
|
||||
if (bool == null) {
|
||||
out.add(option);
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> next = new LinkedHashMap<>(option);
|
||||
next.put("value", bool);
|
||||
out.add(next);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private static List<Map<String, Object>> defaultBooleanOptions() {
|
||||
List<Map<String, Object>> options = new ArrayList<>();
|
||||
Map<String, Object> no = new LinkedHashMap<>();
|
||||
no.put("label", "否");
|
||||
no.put("value", Boolean.FALSE);
|
||||
options.add(no);
|
||||
Map<String, Object> yes = new LinkedHashMap<>();
|
||||
yes.put("label", "是");
|
||||
yes.put("value", Boolean.TRUE);
|
||||
options.add(yes);
|
||||
return options;
|
||||
}
|
||||
|
||||
private static Boolean toBooleanOptionValue(Object raw) {
|
||||
if (raw instanceof Boolean b) {
|
||||
return b;
|
||||
}
|
||||
if (raw == null) {
|
||||
return null;
|
||||
}
|
||||
String v = String.valueOf(raw).trim();
|
||||
if ("1".equals(v) || "true".equalsIgnoreCase(v) || "Y".equalsIgnoreCase(v)) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
if ("0".equals(v) || "false".equalsIgnoreCase(v) || "N".equalsIgnoreCase(v)) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* F3-1:判断一个 search 段字段是否为主键/外键 UUID 列,应从查询区排除。
|
||||
*/
|
||||
@@ -1202,7 +1311,14 @@ public class TemplateToAmisConverter {
|
||||
item.put("disabled", true);
|
||||
}
|
||||
if (attr.defaultValue() != null && !attr.defaultValue().isBlank()) {
|
||||
item.put("value", scalarValue(attr.defaultValue().trim()));
|
||||
Object defaultValue = scalarValue(attr.defaultValue().trim());
|
||||
if ("BOOLEAN".equalsIgnoreCase(ref)) {
|
||||
Boolean bool = toBooleanOptionValue(defaultValue);
|
||||
if (bool != null) {
|
||||
defaultValue = bool;
|
||||
}
|
||||
}
|
||||
item.put("value", defaultValue);
|
||||
}
|
||||
|
||||
String codeLanguage = fallbackCodeLanguage(name, attr.label(), ref, attr.fieldLength());
|
||||
@@ -1219,6 +1335,9 @@ public class TemplateToAmisConverter {
|
||||
item.put("clearable", true);
|
||||
String dict = enumType.isBlank() ? "YESNO" : enumType;
|
||||
List<Map<String, Object>> options = enumerateOptions(dict, result);
|
||||
if ("BOOLEAN".equalsIgnoreCase(ref)) {
|
||||
options = options.isEmpty() ? defaultBooleanOptions() : booleanOptions(options);
|
||||
}
|
||||
if (options.isEmpty()) {
|
||||
item.put("source", "/api/poc/dict/" + dict);
|
||||
} else {
|
||||
@@ -1894,10 +2013,11 @@ public class TemplateToAmisConverter {
|
||||
// 本页脚本(key→脚本体):供 filterscript 级联(组织→部门→岗位)解析,与 convertViewPage 同口径
|
||||
Map<String, String> pageScripts = pageScriptsByKey(pageRoot);
|
||||
for (Element f : fields.elements("field")) {
|
||||
Map<String, Object> formItem = UiTypeMapper.mapField(f, result, "editform", colMeta);
|
||||
Map<String, Object> formItem = UiTypeMapper.mapField(f, result, "editform", colMeta, pageScripts);
|
||||
enrichFieldRefs(formItem, f, result);
|
||||
applyFilterscriptCascade(formItem, f, pageScripts, result);
|
||||
inlineEnumerateOptions(formItem, "editform", result);
|
||||
inlineEnumerateOptions(formItem, "editform", result, colMeta);
|
||||
enrichEditGridColumnsFromChildPage(formItem, f, result);
|
||||
formItems.add(formItem);
|
||||
}
|
||||
result.note("form@pageid=" + pageId + " → 解析 view_page 取得 " + formItems.size() + " 字段");
|
||||
@@ -2011,6 +2131,148 @@ public class TemplateToAmisConverter {
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* FormEditGrid 常见结构是:外层 {@code <display>} 只列出显示列,真正控件定义在
|
||||
* {@code <formeditgrid pageid="...">} 指向的子 view_page。先由 UiTypeMapper 按 display 建列,
|
||||
* 再在这里用子页面字段定义覆盖 quickEdit,避免全部退化成 input-text。
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void enrichEditGridColumnsFromChildPage(Map<String, Object> formItem, Element sourceField,
|
||||
ConversionResult result) {
|
||||
if (formItem == null || sourceField == null || !"input-table".equals(formItem.get("type"))) {
|
||||
return;
|
||||
}
|
||||
Object colsObj = formItem.get("columns");
|
||||
if (!(colsObj instanceof List<?> rawColumns) || rawColumns.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
String pageId = editGridChildPageId(sourceField);
|
||||
if (pageId == null || pageId.isBlank()) {
|
||||
return;
|
||||
}
|
||||
String pageXml = loadPageXml(pageId);
|
||||
if (pageXml == null || pageXml.isBlank()) {
|
||||
result.note("editgrid " + sourceField.attributeValue("name", "")
|
||||
+ " pageid=" + pageId + " 无法解析子 view_page → 保留 display 默认列控件");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Element childPage = DocumentHelper.parseText(pageXml).getRootElement();
|
||||
Element childFields = childPage.element("fields");
|
||||
if (childFields == null) {
|
||||
return;
|
||||
}
|
||||
Map<String, Element> byName = new LinkedHashMap<>();
|
||||
for (Element f : childFields.elements("field")) {
|
||||
String name = f.attributeValue("name", "").trim();
|
||||
if (!name.isBlank()) {
|
||||
byName.put(name.toUpperCase(), f);
|
||||
}
|
||||
}
|
||||
if (byName.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
String childEntityId = childPage.attributeValue("entityid", "");
|
||||
Map<String, String[]> childColMeta = loadColumnMeta(childEntityId, result);
|
||||
Map<String, String> childScripts = pageScriptsByKey(childPage);
|
||||
String gridName = sourceField.attributeValue("name", String.valueOf(formItem.getOrDefault("name", "")));
|
||||
int enriched = 0;
|
||||
for (Object rawCol : rawColumns) {
|
||||
if (!(rawCol instanceof Map<?, ?> m)) {
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> col = (Map<String, Object>) m;
|
||||
String colName = String.valueOf(col.getOrDefault("name", "")).trim();
|
||||
if (colName.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
Element childField = byName.get(colName.toUpperCase());
|
||||
if (childField == null) {
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> control = UiTypeMapper.mapField(
|
||||
childField, result, "editgrid:" + gridName, childColMeta, childScripts);
|
||||
enrichFieldRefs(control, childField, result);
|
||||
applyFilterscriptCascade(control, childField, childScripts, result);
|
||||
inlineEnumerateOptions(control, "editgrid:" + gridName, result, childColMeta);
|
||||
applyEditGridChildControl(col, control, childField, gridName, result);
|
||||
if (String.valueOf(col.getOrDefault("label", "")).isBlank()) {
|
||||
col.put("label", control.get("label"));
|
||||
}
|
||||
enriched++;
|
||||
}
|
||||
if (enriched > 0) {
|
||||
result.note("editgrid " + gridName + " pageid=" + pageId
|
||||
+ " → 按子 view_page 字段定义增强 " + enriched + " 列 quickEdit 控件");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
result.note("editgrid " + sourceField.attributeValue("name", "")
|
||||
+ " pageid=" + pageId + " 子 view_page 解析失败: " + e.getClass().getSimpleName()
|
||||
+ " → 保留 display 默认列控件");
|
||||
}
|
||||
}
|
||||
|
||||
private void applyEditGridChildControl(Map<String, Object> col, Map<String, Object> control,
|
||||
Element childField, String gridName, ConversionResult result) {
|
||||
String childName = childField.attributeValue("name", "");
|
||||
String childUitype = childField.attributeValue("uitype", "");
|
||||
boolean isRef = "SelectDialog".equals(childUitype) || "MultiSelectDialog".equals(childUitype);
|
||||
if (isRef && "static".equals(control.get("type"))) {
|
||||
col.put("type", "tpl");
|
||||
col.put("tpl", control.get("tpl"));
|
||||
col.put("quickEdit", false);
|
||||
result.note("editgrid " + gridName + " 子列 " + childName
|
||||
+ " 只读引用 → 显示列(tpl 显名 + quickEdit:false 只读,非 picker)");
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, Object> quickEdit = new LinkedHashMap<>(control);
|
||||
quickEdit.remove("label");
|
||||
String script = childField.attributeValue("script", "").trim();
|
||||
if (!script.isBlank()) {
|
||||
quickEdit.put("onEvent", UiTypeMapper.editGridChangeOnEvent(gridName, script, childName));
|
||||
result.note("editgrid " + gridName + " 子列 " + childName
|
||||
+ " script=" + script + " → onEvent.change 行内联动端点");
|
||||
}
|
||||
col.remove("type");
|
||||
col.remove("tpl");
|
||||
col.put("quickEdit", quickEdit);
|
||||
}
|
||||
|
||||
private static String editGridChildPageId(Element f) {
|
||||
Element tpl = f.element("editgridtemplate");
|
||||
if (tpl == null) {
|
||||
tpl = f.element("formeditgridtemplate");
|
||||
}
|
||||
String tplXml = tpl != null ? tpl.getText() : null;
|
||||
if (tplXml == null || tplXml.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Element grid = embeddedEditGridElement(DocumentHelper.parseText(tplXml.trim()).getRootElement());
|
||||
return grid != null ? grid.attributeValue("pageid") : null;
|
||||
} catch (Exception ignore) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Element embeddedEditGridElement(Element root) {
|
||||
if (root == null) {
|
||||
return null;
|
||||
}
|
||||
String name = root.getName();
|
||||
if ("editgrid".equals(name) || "formeditgrid".equals(name)) {
|
||||
return root;
|
||||
}
|
||||
for (Element child : root.elements()) {
|
||||
Element found = embeddedEditGridElement(child);
|
||||
if (found != null) {
|
||||
return found;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Map<String, Object> convertViewPage(Element pageRoot, ConversionResult result, String templateName) {
|
||||
String entityId = pageRoot.attributeValue("entityid", "");
|
||||
Map<String, String[]> colMeta = (entityId != null && !entityId.isBlank())
|
||||
@@ -2023,7 +2285,8 @@ public class TemplateToAmisConverter {
|
||||
Map<String, Object> formItem = UiTypeMapper.mapField(f, result, "editform", colMeta, pageScripts);
|
||||
enrichFieldRefs(formItem, f, result);
|
||||
applyFilterscriptCascade(formItem, f, pageScripts, result);
|
||||
inlineEnumerateOptions(formItem, "editform", result);
|
||||
inlineEnumerateOptions(formItem, "editform", result, colMeta);
|
||||
enrichEditGridColumnsFromChildPage(formItem, f, result);
|
||||
formItems.add(formItem);
|
||||
}
|
||||
}
|
||||
@@ -2881,7 +3144,10 @@ public class TemplateToAmisConverter {
|
||||
Map<String, Object> filter = buildSearchFilter(gridSearch, result, false, gridEntityId);
|
||||
if (filter != null) {
|
||||
gridSchema.put("filter", filter);
|
||||
result.note("tree+grid: gridtemplate <search> → 右表查询过滤表单");
|
||||
if (gridSchema.get("api") instanceof String api) {
|
||||
gridSchema.put("api", appendFilterParamsToApi(api, filter));
|
||||
}
|
||||
result.note("tree+grid: gridtemplate <search> → 右表查询过滤表单,api 显式携带查询字段");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -680,7 +680,7 @@ final class UiTypeMapper {
|
||||
* ③ 自定义动作 {@code applyEditGridParent} 把 {@code parentSetValues} 写主表(如申请总金额)。
|
||||
* templateId 此处填占位符,convertViewPage 后置注入。
|
||||
*/
|
||||
private static Map<String, Object> editGridChangeOnEvent(String editgridField, String scriptKey, String changedField) {
|
||||
static Map<String, Object> editGridChangeOnEvent(String editgridField, String scriptKey, String changedField) {
|
||||
Map<String, Object> data = new LinkedHashMap<>();
|
||||
data.put("templateId", EDITGRID_TPL_PLACEHOLDER);
|
||||
data.put("editgridField", editgridField);
|
||||
|
||||
Reference in New Issue
Block a user