Back to Nocobase

ctx.collectionField

docs/docs/en/runjs/context/collection-field.md

2.0.605.0 KB
Original Source

ctx.collectionField

The collection field (CollectionField) instance for the current RunJS context; used to access field metadata, type, validation rules, and association info. Only present when the field is bound to a collection definition; custom/virtual fields may have null.

Use Cases

ScenarioDescription
JSFieldUse interface, enum, targetCollection, etc. for linkage or validation
JSItemAccess metadata of the column’s field in sub-table items
JSColumnChoose render by collectionField.interface, or use targetCollection

Note: ctx.collectionField is only available when the field is bound to a collection; in standalone JSBlock or actions with no field binding it is usually undefined—check before use.

Type

ts
collectionField: CollectionField | null | undefined;

Common Properties

PropertyTypeDescription
namestringField name (e.g. status, userId)
titlestringField title (i18n)
typestringData type (string, integer, belongsTo, etc.)
interfacestringUI type (input, select, m2o, o2m, m2m, etc.)
collectionCollectionField’s collection
targetCollectionCollectionTarget collection for association fields only
targetstringTarget collection name (association)
enumarrayEnum options (select, radio, etc.)
defaultValueanyDefault value
collectionNamestringCollection name
foreignKeystringForeign key (e.g. belongsTo)
sourceKeystringSource key (e.g. hasMany)
targetKeystringTarget key
fullpathstringFull path (e.g. main.users.status) for API/variables
resourceNamestringResource name (e.g. users.status)
readonlybooleanRead-only
titleablebooleanCan be used as title
validationobjectValidation config
uiSchemaobjectUI config
targetCollectionTitleFieldCollectionFieldTitle field of target collection (association)

Common Methods

MethodDescription
isAssociationField(): booleanWhether it is an association (belongsTo, hasMany, hasOne, belongsToMany, etc.)
isRelationshipField(): booleanWhether it is a relationship (o2o, m2o, o2m, m2m, etc.)
getComponentProps(): objectDefault props for the field component
getFields(): CollectionField[]Fields of target collection (association only)
getFilterOperators(): object[]Filter operators (e.g. $eq, $ne)

Examples

Branch by field type

ts
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 || [];
  // ...
}

Check association and use target collection

ts
if (ctx.collectionField?.isAssociationField()) {
  const targetCol = ctx.collectionField.targetCollection;
  const titleField = targetCol?.titleCollectionField?.name;
  // ...
}

Get enum options

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

Conditional render by readonly

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

Title field of target collection

ts
const titleField = ctx.collectionField?.targetCollectionTitleField;
const titleKey = titleField?.name ?? 'title';
const assocValue = ctx.getValue?.() ?? ctx.record?.[ctx.collectionField?.name];
const label = assocValue?.[titleKey];

Relation to ctx.collection

NeedRecommended
Collection of current fieldctx.collectionField?.collection or ctx.collection
Field metadata (name, type, interface, enum, etc.)ctx.collectionField
Target collectionctx.collectionField?.targetCollection

ctx.collection is usually the block’s collection; ctx.collectionField is the field definition; they can differ in sub-tables and associations.

Notes

  • In JSBlock, JSAction (no field binding), ctx.collectionField is usually undefined; use optional chaining.
  • Custom JS fields not bound to a collection field may have ctx.collectionField as null.
  • targetCollection exists only for association fields (m2o, o2m, m2m); enum only for select, radioGroup, etc.