AI翻訳通知

このドキュメントはAIによって翻訳されました。正確な情報については英語版をご参照ください。

ctx.collectionField

現在の RunJS 実行コンテキストに関連付けられたデータ表フィールド(CollectionField)のインスタンスです。フィールドのメタデータ、タイプ、バリデーションルール、および関連情報にアクセスするために使用されます。フィールドがデータ表の定義にバインドされている場合にのみ存在し、カスタムフィールドや仮想フィールドの場合は null になる可能性があります。

適用シーン

シーン説明
JSFieldフォームフィールドにおいて interfaceenumtargetCollection などに基づいた連動やバリデーションを行います。
JSItemサブテーブルの項目内で、現在の列に対応するフィールドのメタデータにアクセスします。
JSColumnテーブルの列において collectionField.interface に応じたレンダリング方式の選択や、targetCollection へのアクセスを行います。

注意:ctx.collectionField は、フィールドがデータ表(Collection)の定義にバインドされている場合にのみ利用可能です。JSBlock(独立したブロック)やフィールドバインドのないアクションイベントなどのシーンでは、通常 undefined となるため、使用前に空値チェックを行うことを推奨します。

型定義

collectionField: CollectionField | null | undefined;

主要なプロパティ

プロパティ説明
namestringフィールド名(例: statususerId
titlestringフィールドのタイトル(国際化対応を含む)
typestringフィールドのデータ型(stringintegerbelongsTo など)
interfacestringフィールドのインターフェース型(inputselectm2oo2mm2m など)
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 は、データ表内での現在のフィールドの定義を表します。サブテーブルや関連フィールドなどのシーンでは、両者が異なる場合があります。

注意事項

  • JSBlockJSAction(フィールドバインドなし) などのシーンでは、ctx.collectionField は通常 undefined です。アクセス前にオプショナルチェイニングを使用することを推奨します。
  • カスタム JS フィールドがデータ表フィールドにバインドされていない場合、ctx.collectionFieldnull になる可能性があります。
  • targetCollection は関連タイプのフィールド(m2o、o2m、m2m など)にのみ存在します。enum は select や radioGroup などの選択肢を持つフィールドにのみ存在します。

関連情報