logologo
开始
教程
手册
开发
插件
API
首页
English
简体中文
日本語
한국어
Español
Português
Deutsch
Français
Русский
开始
教程
手册
开发
插件
API
首页
logologo
RunJS 概述
导入模块
容器内渲染

全局变量

window
document
navigator

ctx

ctx.blockModel
ctx.collection
ctx.collectionField
ctx.dataSource
ctx.dataSourceManager
ctx.element
ctx.exit()
ctx.exitAll()
ctx.filterManager
ctx.form
ctx.getModel()
ctx.getValue()
ctx.getVar()
ctx.i18n
ctx.importAsync()
ctx.initResource()
ctx.libs
ctx.location
ctx.logger
ctx.makeResource()
ctx.message
ctx.modal
ctx.model
ctx.notification
ctx.off()
ctx.on()
ctx.openView()
ctx.render()
ctx.request()
ctx.requireAsync()
ctx.resource
ctx.route
ctx.router
ctx.setValue()
ctx.sql
ctx.t()
ctx.view
Previous Pagectx.collection
Next Pagectx.dataSource

#ctx.collectionField

当前 RunJS 执行上下文关联的数据表字段(CollectionField)实例,用于访问字段的元数据、类型、校验规则及关联信息。仅在字段绑定到数据表定义时存在;自定义/虚拟字段可能为 null。

#适用场景

场景说明
JSField表单字段中根据 interface、enum、targetCollection 等做联动或校验
JSItem子表格项中访问当前列对应字段的元数据
JSColumn表格列中按 collectionField.interface 选择渲染方式,或访问 targetCollection

注意:ctx.collectionField 仅在字段绑定到数据表(Collection)定义时可用;JSBlock 独立区块、无字段绑定的操作事件等场景中通常为 undefined,使用前建议做空值判断。

#类型定义

collectionField: CollectionField | null | undefined;

#常用属性

属性类型说明
namestring字段名(如 status、userId)
titlestring字段标题(含国际化)
typestring字段数据类型(string、integer、belongsTo 等)
interfacestring字段界面类型(input、select、m2o、o2m、m2m 等)
collectionCollection字段所属数据表
targetCollectionCollection关联字段的目标数据表(仅关联类型有值)
targetstring目标数据表名称(关联字段)
enumarray枚举选项(select、radio 等)
defaultValueany默认值
collectionNamestring所属数据表名称
foreignKeystring外键字段名(belongsTo 等)
sourceKeystring关联源键(hasMany 等)
targetKeystring关联目标键
fullpathstring完整路径(如 main.users.status),用于 API 或变量引用
resourceNamestring资源名(如 users.status)
readonlyboolean是否只读
titleableboolean是否可作为标题展示
validationobject校验规则配置
uiSchemaobjectUI 配置
targetCollectionTitleFieldCollectionField关联目标数据表的标题字段(关联字段)

#常用方法

方法说明
isAssociationField(): boolean是否为关联字段(belongsTo、hasMany、hasOne、belongsToMany 等)
isRelationshipField(): boolean是否为关系型字段(含 o2o、m2o、o2m、m2m 等)
getComponentProps(): object获取字段组件的默认 props
getFields(): CollectionField[]获取关联目标数据表的字段列表(仅关联字段)
getFilterOperators(): object[]获取该字段支持的筛选操作符(如 $eq、$ne 等)

#示例

#根据字段类型做分支渲染

if (!ctx.collectionField) return null;
const { interface: iface } = ctx.collectionField;
if (['m2o', 'o2m', 'm2m'].includes(iface)) {
  // 关联字段:显示关联记录
  const target = ctx.collectionField.targetCollection;
  // ...
} else if (iface === 'select' || iface === 'radioGroup') {
  const options = ctx.collectionField.enum || [];
  // ...
}

#判断是否为关联字段并访问目标数据表

if (ctx.collectionField?.isAssociationField()) {
  const targetCol = ctx.collectionField.targetCollection;
  const titleField = targetCol?.titleCollectionField?.name;
  // 按目标数据表结构处理
}

#获取枚举选项

const options = ctx.collectionField?.enum ?? [];
const labels = options.map((o) => (typeof o === 'object' ? o.label : o));

#根据只读/只展示模式做条件渲染

const { Input } = ctx.libs.antd;
if (ctx.collectionField?.readonly) {
  ctx.render(<span>{ctx.getValue?.() ?? '-'}</span>);
} else {
  ctx.render(<Input onChange={(e) => ctx.setValue?.(e.target.value)} />);
}

#获取关联目标数据表的标题字段

// 关联字段显示时,可用目标数据表的 titleCollectionField 获取标题字段名
const titleField = ctx.collectionField?.targetCollectionTitleField;
const titleKey = titleField?.name ?? 'title';
const assocValue = ctx.getValue?.() ?? ctx.record?.[ctx.collectionField?.name];
const label = assocValue?.[titleKey];

#与 ctx.collection 的关系

需求推荐用法
当前字段所属数据表ctx.collectionField?.collection 或 ctx.collection
字段元数据(名、类型、接口、枚举等)ctx.collectionField
关联目标数据表ctx.collectionField?.targetCollection

ctx.collection 通常表示当前区块绑定的数据表;ctx.collectionField 表示当前字段在数据表中的定义。在子表格、关联字段等场景下,两者可能不同。

#注意事项

  • 在 JSBlock、JSAction(无字段绑定) 等场景中,ctx.collectionField 通常为 undefined,访问前建议使用可选链。
  • 自定义 JS 字段若未绑定到数据表字段,ctx.collectionField 可能为 null。
  • targetCollection 仅在关联类型字段(如 m2o、o2m、m2m)下存在;enum 仅在 select、radioGroup 等有选项的字段下存在。

#相关

  • ctx.collection:当前上下文关联的数据表
  • ctx.model:当前执行上下文所在模型
  • ctx.blockModel:承载当前 JS 的父区块
  • ctx.getValue()、ctx.setValue():读写当前字段值