logologo
Get Started
Tutorials
Guide
Development
Plugins
API
Home
English
简体中文
日本語
한국어
Español
Português
Deutsch
Français
Русский
Get Started
Tutorials
Guide
Development
Plugins
API
Home
logologo
RunJS Overview
Importing Modules
Rendering in Container

Global Variables

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

The CollectionField instance associated with the current RunJS execution context, used to access field metadata, types, validation rules, and association information. It only exists when the field is bound to a collection definition; custom/virtual fields may be null.

#Use Cases

ScenarioDescription
JSFieldPerform linkage or validation in form fields based on interface, enum, targetCollection, etc.
JSItemAccess metadata of the field corresponding to the current column in sub-table items.
JSColumnSelect rendering methods based on collectionField.interface or access targetCollection in table columns.

Note: ctx.collectionField is only available when the field is bound to a Collection definition; it is usually undefined in scenarios like JSBlock independent blocks or action events without field binding. It is recommended to check for null values before use.

#Type Definition

collectionField: CollectionField | null | undefined;

#Common Properties

PropertyTypeDescription
namestringField name (e.g., status, userId)
titlestringField title (including internationalization)
typestringField data type (string, integer, belongsTo, etc.)
interfacestringField interface type (input, select, m2o, o2m, m2m, etc.)
collectionCollectionThe collection the field belongs to
targetCollectionCollectionThe target collection of the association field (only for association types)
targetstringTarget collection name (for association fields)
enumarrayEnumeration options (select, radio, etc.)
defaultValueanyDefault value
collectionNamestringName of the collection it belongs to
foreignKeystringForeign key field name (belongsTo, etc.)
sourceKeystringAssociation source key (hasMany, etc.)
targetKeystringAssociation target key
fullpathstringFull path (e.g., main.users.status), used for API or variable references
resourceNamestringResource name (e.g., users.status)
readonlybooleanWhether it is read-only
titleablebooleanWhether it can be displayed as a title
validationobjectValidation rule configuration
uiSchemaobjectUI configuration
targetCollectionTitleFieldCollectionFieldThe title field of the target collection (for association fields)

#Common Methods

MethodDescription
isAssociationField(): booleanWhether it is an association field (belongsTo, hasMany, hasOne, belongsToMany, etc.)
isRelationshipField(): booleanWhether it is a relationship field (including o2o, m2o, o2m, m2m, etc.)
getComponentProps(): objectGet the default props of the field component
getFields(): CollectionField[]Get the field list of the target collection (association fields only)
getFilterOperators(): object[]Get the filter operators supported by this field (e.g., $eq, $ne, etc.)

#Examples

#Branch rendering based on field type

if (!ctx.collectionField) return null;
const { interface: iface } = ctx.collectionField;
if (['m2o', 'o2m', 'm2m'].includes(iface)) {
  // Association field: display associated records
  const target = ctx.collectionField.targetCollection;
  // ...
} else if (iface === 'select' || iface === 'radioGroup') {
  const options = ctx.collectionField.enum || [];
  // ...
}

#Determine if it is an association field and access the target collection

if (ctx.collectionField?.isAssociationField()) {
  const targetCol = ctx.collectionField.targetCollection;
  const titleField = targetCol?.titleCollectionField?.name;
  // Process according to the target collection structure
}

#Get enumeration options

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

#Conditional rendering based on read-only/view mode

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)} />);
}

#Get the title field of the target collection

// When displaying an association field, use targetCollectionTitleField to get the title field name
const titleField = ctx.collectionField?.targetCollectionTitleField;
const titleKey = titleField?.name ?? 'title';
const assocValue = ctx.getValue?.() ?? ctx.record?.[ctx.collectionField?.name];
const label = assocValue?.[titleKey];

#Relationship with ctx.collection

RequirementRecommended Usage
Current field's collectionctx.collectionField?.collection or ctx.collection
Field metadata (name, type, interface, enum, etc.)ctx.collectionField
Target collectionctx.collectionField?.targetCollection

ctx.collection usually represents the collection bound to the current block; ctx.collectionField represents the definition of the current field in the collection. In scenarios like sub-tables or association fields, the two may differ.

#Notes

  • In scenarios such as JSBlock or JSAction (without field binding), ctx.collectionField is usually undefined. It is recommended to use optional chaining before access.
  • If a custom JS field is not bound to a collection field, ctx.collectionField may be null.
  • targetCollection only exists for association type fields (e.g., m2o, o2m, m2m); enum only exists for fields with options like select or radioGroup.

#Related

  • ctx.collection: Collection associated with the current context
  • ctx.model: Model where the current execution context is located
  • ctx.blockModel: Parent block carrying the current JS
  • ctx.getValue(), ctx.setValue(): Read and write the current field value