Files
2026-05-18 09:28:15 +08:00

210 lines
7.3 KiB
Python

"""列表配置构建器
参考前端 ColumnDesign/src/helper/config.ts 中 defaultColumnData 的 columnData 部分。
生成完整的 columnData JSON 配置,包含搜索条件、列显示、按钮配置等。
"""
from typing import Any, Dict, List
from aibridge.builders.field_model import build_field_model
def build_column_data(
fields: List[Dict[str, Any]],
table_name: str = "base_table",
base_render_key: int = 0,
search_field_count: int = 3,
display_field_count: int = 3,
) -> Dict[str, Any]:
"""构建列表配置 columnData JSON
Args:
fields: 字段定义列表
table_name: 表名
base_render_key: 基础渲染键
search_field_count: 搜索条件字段数量(默认前 3 个字段)
display_field_count: 列表显示字段数量(默认前 3 个字段)
Returns:
columnData 字典(需 JSON 序列化后传入 API)
"""
all_field_models = []
for i, f in enumerate(fields):
all_field_models.append(build_field_model(
field_name=f.get("fieldName", ""),
field_vmodel=f.get("field", ""),
jnpf_key=f.get("jnpfKey", "input"),
required=f.get("required", False),
table_name=table_name,
render_key=base_render_key + i,
options=f.get("options"),
dictionary_type=f.get("dictionaryType", ""),
))
# searchList - 前 N 个字段作为搜索条件
search_list = []
for fm in all_field_models[:search_field_count]:
search_list.append({
"label": fm["fullName"],
"labelI18nCode": "",
"prop": fm["id"],
"jnpfKey": fm["__config__"]["jnpfKey"],
"sourceType": 2,
"searchType": 2,
"searchMultiple": False,
"isKeyword": False,
"id": fm["id"],
"fullName": fm["fullName"],
"fullNameI18nCode": [""],
**fm,
})
# columnList - 前 N 个字段显示在列表
column_list = []
for fm in all_field_models[:display_field_count]:
column_list.append({
"label": fm["fullName"],
"labelI18nCode": "",
"prop": fm["id"],
"fixed": "none",
"headerAlign": "left",
"align": "left",
"jnpfKey": fm["__config__"]["jnpfKey"],
"sortable": False,
"width": None,
"id": fm["id"],
"fullName": fm["fullName"],
"fullNameI18nCode": [""],
**fm,
})
# columnOptions - 所有可选字段
column_options = []
for fm in all_field_models:
column_options.append({
"id": fm["id"],
"fullName": fm["fullName"],
"fullNameI18nCode": [""],
**fm,
})
# defaultColumnList - 所有字段,前 N 个 checked=true
default_column_list = []
for idx, fm in enumerate(all_field_models):
default_column_list.append({
"label": fm["fullName"],
"labelI18nCode": "",
"prop": fm["id"],
"fixed": "none",
"headerAlign": "left",
"align": "left",
"jnpfKey": fm["__config__"]["jnpfKey"],
"sortable": False,
"width": None,
"id": fm["id"],
"fullName": fm["fullName"],
"fullNameI18nCode": [""],
"checked": idx < display_field_count,
**fm,
})
return {
"searchList": search_list,
"columnList": column_list,
"columnOptions": column_options,
"defaultColumnList": default_column_list,
# 过滤规则 - SuperJsonModel 类型,必须是对象
"ruleList": {"matchLogic": "and", "conditionList": []},
# 标签面板 - TabConfigModel 类型,不能为 null
"tabConfig": {"on": False, "relationField": "", "hasAllTab": True},
# 卡片布局配置
"cardListConfig": {
"cardLayout": "horizontal",
"titleField": "",
"subTitleField": "",
"displayFields": "",
"columnCount": 1,
"enableImage": False,
"imageSource": "",
"displayPosition": "left",
"fillMode": "contain",
},
"customBtnsList": [],
# 顶部按钮列表
"btnsList": [
{"show": True, "value": "add", "icon": "icon-ym icon-ym-btn-add",
"label": "新增", "labelI18nCode": "common.add2Text"},
{"show": False, "value": "download", "icon": "icon-ym icon-ym-btn-download",
"label": "导出", "labelI18nCode": "common.exportText"},
{"show": False, "value": "upload", "icon": "icon-ym icon-ym-btn-upload",
"label": "导入", "labelI18nCode": "common.importText"},
{"show": False, "value": "batchRemove", "icon": "icon-ym icon-ym-btn-clearn",
"label": "批量删除", "labelI18nCode": "common.batchDelText"},
{"show": False, "value": "batchPrint", "icon": "icon-ym icon-ym-report-icon-preview-printPreview",
"label": "批量打印", "labelI18nCode": "common.batchPrintText"},
],
# 行操作按钮
"columnBtnsList": [
{"show": True, "value": "edit", "icon": "icon-ym icon-ym-btn-edit",
"label": "编辑", "labelI18nCode": "common.editText",
"event": {"enableFunc": "({ row, rowIndex, onlineUtils }) => { return true }"}},
{"show": True, "value": "remove", "icon": "icon-ym icon-ym-btn-clearn",
"label": "删除", "labelI18nCode": "common.delText",
"event": {"enableFunc": "({ row, rowIndex, onlineUtils }) => { return true }"}},
{"show": True, "value": "detail", "icon": "icon-ym icon-ym-generator-menu",
"label": "详情", "labelI18nCode": "common.detailText",
"event": {"enableFunc": "({ row, rowIndex, onlineUtils }) => { return true }"}},
],
# 其他配置
"resizable": True,
"hasSuperQuery": True,
"showOverflow": True,
"childTableStyle": 1,
"showSummary": False,
"summaryField": [],
"type": 1,
"defaultSortConfig": [],
"viewKey": "",
"hasPage": True,
"pageSize": 20,
"hasTreeQuery": False,
"treeTitle": "左侧标题",
"treeTitleI18nCode": "",
"treeDataSource": "dictionary",
"treeDictionary": "",
"treeRelation": "",
"treeRelationFieldSelectType": "all",
"treeRelationFieldAbleIds": [],
"treeSyncType": 0,
"treeSyncInterfaceId": "",
"treeSyncInterfaceName": "",
"treeSyncTemplateJson": [],
"treePropsUrl": "",
"treePropsName": "",
"treeTemplateJson": [],
"treePropsValue": "id",
"treePropsChildren": "children",
"treePropsLabel": "fullName",
"groupField": "",
"parentField": "",
"printIds": [],
"useColumnPermission": False,
"useFormPermission": False,
"useBtnPermission": False,
"useDataPermission": False,
"funcs": {
"afterOnload": "({ data, tableRef, onlineUtils }) => {}",
"rowStyle": "({ row, rowIndex }) => {}",
"cellStyle": "({ row, column, rowIndex, columnIndex }) => {}",
},
"uploaderTemplateJson": {},
"complexHeaderList": [],
}