ctx.collectionField

Instance field collection (CollectionField) liên kết với ngữ cảnh thực thi RunJS hiện tại, dùng để truy cập metadata, kiểu, quy tắc validate và thông tin quan hệ của field. Chỉ tồn tại khi field được liên kết với định nghĩa collection; field tùy chỉnh/ảo có thể là null.

Kịch bản áp dụng

Kịch bảnMô tả
JSFieldTrong field của form, thực hiện liên kết hoặc validate dựa trên interface, enum, targetCollection, v.v.
JSItemTrong item của sub-table, truy cập metadata của field tương ứng với column hiện tại
JSColumnTrong column của table, chọn cách render theo collectionField.interface, hoặc truy cập targetCollection

Lưu ý: ctx.collectionField chỉ khả dụng khi field được liên kết với định nghĩa collection (Collection); trong JSBlock độc lập, sự kiện action không có liên kết field, v.v., thường là undefined, khuyến nghị kiểm tra null trước khi sử dụng.

Định nghĩa kiểu

collectionField: CollectionField | null | undefined;

Thuộc tính thường dùng

Thuộc tínhKiểuMô tả
namestringTên field (như status, userId)
titlestringTiêu đề field (có hỗ trợ i18n)
typestringKiểu dữ liệu của field (string, integer, belongsTo, v.v.)
interfacestringKiểu giao diện của field (input, select, m2o, o2m, m2m, v.v.)
collectionCollectionCollection mà field thuộc về
targetCollectionCollectionCollection đích của field quan hệ (chỉ có giá trị với kiểu quan hệ)
targetstringTên collection đích (field quan hệ)
enumarrayTùy chọn enum (select, radio, v.v.)
defaultValueanyGiá trị mặc định
collectionNamestringTên collection mà field thuộc về
foreignKeystringTên field khóa ngoại (belongsTo, v.v.)
sourceKeystringKhóa nguồn của quan hệ (hasMany, v.v.)
targetKeystringKhóa đích của quan hệ
fullpathstringĐường dẫn đầy đủ (như main.users.status), dùng cho API hoặc tham chiếu biến
resourceNamestringTên resource (như users.status)
readonlybooleanCó chỉ đọc hay không
titleablebooleanCó thể dùng làm tiêu đề hiển thị hay không
validationobjectCấu hình quy tắc validate
uiSchemaobjectCấu hình UI
targetCollectionTitleFieldCollectionFieldField tiêu đề của collection đích (field quan hệ)

Phương thức thường dùng

Phương thứcMô tả
isAssociationField(): booleanCó phải field quan hệ không (belongsTo, hasMany, hasOne, belongsToMany, v.v.)
isRelationshipField(): booleanCó phải field kiểu quan hệ không (bao gồm o2o, m2o, o2m, m2m, v.v.)
getComponentProps(): objectLấy props mặc định của component field
getFields(): CollectionField[]Lấy danh sách field của collection đích quan hệ (chỉ với field quan hệ)
getFilterOperators(): object[]Lấy các toán tử filter mà field hỗ trợ (như $eq, $ne, v.v.)

Ví dụ

Render phân nhánh theo kiểu field

if (!ctx.collectionField) return null;
const { interface: iface } = ctx.collectionField;
if (['m2o', 'o2m', 'm2m'].includes(iface)) {
  // Field quan hệ: hiển thị bản ghi liên quan
  const target = ctx.collectionField.targetCollection;
  // ...
} else if (iface === 'select' || iface === 'radioGroup') {
  const options = ctx.collectionField.enum || [];
  // ...
}

Kiểm tra có phải field quan hệ và truy cập collection đích

if (ctx.collectionField?.isAssociationField()) {
  const targetCol = ctx.collectionField.targetCollection;
  const titleField = targetCol?.titleCollectionField?.name;
  // Xử lý theo cấu trúc collection đích
}

Lấy tùy chọn enum

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

Render có điều kiện theo chế độ chỉ đọc/chỉ hiển thị

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

Lấy field tiêu đề của collection đích quan hệ

// Khi hiển thị field quan hệ, có thể dùng titleCollectionField của collection đích để lấy tên field tiêu đề
const titleField = ctx.collectionField?.targetCollectionTitleField;
const titleKey = titleField?.name ?? 'title';
const assocValue = ctx.getValue?.() ?? ctx.record?.[ctx.collectionField?.name];
const label = assocValue?.[titleKey];

Quan hệ với ctx.collection

Nhu cầuCách dùng khuyến nghị
Collection mà field hiện tại thuộc vềctx.collectionField?.collection hoặc ctx.collection
Metadata field (tên, kiểu, interface, enum, v.v.)ctx.collectionField
Collection đích quan hệctx.collectionField?.targetCollection

ctx.collection thường biểu thị collection được liên kết với block hiện tại; ctx.collectionField biểu thị định nghĩa field hiện tại trong collection. Trong các kịch bản như sub-table, field quan hệ, hai cái này có thể khác nhau.

Lưu ý

  • Trong các kịch bản như JSBlock, JSAction (không có liên kết field), ctx.collectionField thường là undefined, khuyến nghị sử dụng optional chaining khi truy cập.
  • JS field tùy chỉnh nếu chưa được liên kết với field collection, ctx.collectionField có thể là null.
  • targetCollection chỉ tồn tại với field kiểu quan hệ (như m2o, o2m, m2m); enum chỉ tồn tại với field có tùy chọn như select, radioGroup.

Liên quan