Back to Nocobase

ctx.collection

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

2.0.514.0 KB
Original Source

ctx.collection

The collection (data table) instance associated with the current RunJS execution context; used to access collection metadata, field definitions, primary key, etc. Usually from ctx.blockModel.collection or ctx.collectionField?.collection.

Use Cases

ScenarioDescription
JSBlockBlock-bound collection; access name, getFields, filterTargetKey, etc.
JSField / JSItem / JSColumnCollection of current field or parent block; get field list, primary key, etc.
Table column / detail blockRender by collection structure, pass filterByTk when opening popup, etc.

Note: ctx.collection is available when the context is bound to a data block, form block, or table block; a standalone JSBlock with no bound collection may have null—check before use.

Type

ts
collection: Collection | null | undefined;

Common Properties

PropertyTypeDescription
namestringCollection name (e.g. users, orders)
titlestringCollection title (i18n)
filterTargetKeystring | string[]Primary key field name(s); used for filterByTk, getFilterByTK
dataSourceKeystringData source key (e.g. main)
dataSourceDataSourceData source instance
templatestringCollection template (e.g. general, file, tree)
titleableFieldsCollectionField[]Fields that can be used as title
titleCollectionFieldCollectionFieldTitle field instance

Common Methods

MethodDescription
getFields(): CollectionField[]All fields (including inherited)
getField(name: string): CollectionField | undefinedSingle field by name
getFieldByPath(path: string): CollectionField | undefinedField by path (supports association, e.g. user.name)
getAssociationFields(types?): CollectionField[]Association fields; types e.g. ['one'], ['many']
getFilterByTK(record): anyPrimary key value from record for API filterByTk

Relation to ctx.collectionField, ctx.blockModel

NeedRecommended
Collection for current contextctx.collection (same as ctx.blockModel?.collection or ctx.collectionField?.collection)
Collection of current fieldctx.collectionField?.collection
Target collection of associationctx.collectionField?.targetCollection

In sub-tables etc., ctx.collection may be the association target; in normal form/table it is usually the block’s collection.

Examples

Get primary key and open popup

ts
const primaryKey = ctx.collection?.filterTargetKey ?? 'id';
await ctx.openView(popupUid, {
  mode: 'dialog',
  params: {
    filterByTk: ctx.record?.[primaryKey],
    record: ctx.record,
  },
});

Iterate fields for validation or linkage

ts
const fields = ctx.collection?.getFields() ?? [];
const requiredFields = fields.filter((f) => f.options?.required);
for (const f of requiredFields) {
  const v = ctx.form?.getFieldValue(f.name);
  if (v == null || v === '') {
    ctx.message.warning(`${f.title} is required`);
    return;
  }
}

Get association fields

ts
const oneToMany = ctx.collection?.getAssociationFields(['many']) ?? [];
// For sub-tables, association resources, etc.

Notes

  • filterTargetKey is the collection’s primary key field name; some collections use string[] composite keys; fallback is often 'id'.
  • In sub-tables, association fields, ctx.collection may point to the association target, different from ctx.blockModel.collection.
  • getFields() merges inherited collection fields; local fields override inherited ones with the same name.