logologo
スタート
マニュアル
開発
プラグイン
API
ホーム
English
简体中文
日本語
한국어
Español
Português
Deutsch
Français
Русский
スタート
マニュアル
開発
プラグイン
API
ホーム
logologo
RunJS 概要
モジュールのインポート
コンテナ内でのレンダリング

グローバル変数

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.blockModel
Next Pagectx.collectionField
AI翻訳通知

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

#ctx.collection

現在の RunJS 実行コンテキストに関連付けられたコレクション(Collection)インスタンスです。コレクションのメタデータ、フィールド定義、主キーなどの設定にアクセスするために使用されます。通常、ctx.blockModel.collection または ctx.collectionField?.collection から取得されます。

#适用シーン

シーン説明
JSBlockブロックにバインドされたコレクション。name、getFields、filterTargetKey などにアクセス可能です。
JSField / JSItem / JSColumn現在のフィールドが属するコレクション(または親ブロックのコレクション)。フィールドリストや主キーなどの取得に使用されます。
テーブル列 / 詳細ブロックコレクション構造に基づいたレンダリングや、ポップアップを開く際の filterByTk の受け渡しなどに使用されます。

注意:ctx.collection は、データブロック、フォームブロック、テーブルブロックなど、コレクションがバインドされているシーンで利用可能です。コレクションがバインドされていない独立した JSBlock では null になる可能性があるため、使用前に空値チェックを行うことを推奨します。

#型定義

collection: Collection | null | undefined;

#常用プロパティ

プロパティ型説明
namestringコレクション名(例:users、orders)
titlestringコレクションのタイトル(国際化対応を含む)
filterTargetKeystring | string[]主キーのフィールド名。filterByTk や getFilterByTK で使用されます。
dataSourceKeystringデータソースのキー(例:main)
dataSourceDataSource所属するデータソースのインスタンス
templatestringコレクションテンプレート(例:general、file、tree)
titleableFieldsCollectionField[]タイトルとして表示可能なフィールドのリスト
titleCollectionFieldCollectionFieldタイトルフィールドのインスタンス

#常用メソッド

メソッド説明
getFields(): CollectionField[]すべてのフィールドを取得します(継承されたものを含む)
getField(name: string): CollectionField | undefinedフィールド名で単一のフィールドを取得します
getFieldByPath(path: string): CollectionField | undefinedパスでフィールドを取得します(user.name のような関連フィールドもサポート)
getAssociationFields(types?): CollectionField[]関連フィールドを取得します。types には ['one'] や ['many'] などを指定できます。
getFilterByTK(record): anyレコードから主キーの値を抽出します。API の filterByTk で使用されます。

#ctx.collectionField、ctx.blockModel との関係

ニーズ推奨される使い方
現在のコンテキストに関連付けられたコレクションctx.collection(ctx.blockModel?.collection または ctx.collectionField?.collection と等価)
現在のフィールドのコレクション定義ctx.collectionField?.collection(フィールドが属するコレクション)
関連先のコレクションctx.collectionField?.targetCollection(関連フィールドのターゲットコレクション)

子テーブルなどのシーンでは、ctx.collection は関連先のコレクションを指す場合があります。通常のフォームやテーブルでは、一般的にブロックにバインドされたコレクションを指します。

#示例

#主キーを取得してポップアップを開く

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

#フィールドをループしてバリデーションや連動を行う

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} は必須です`);
    return;
  }
}

#関連フィールドを取得する

const oneToMany = ctx.collection?.getAssociationFields(['many']) ?? [];
// 子テーブルや関連リソースの構築などに使用

#注意事項

  • filterTargetKey はコレクションの主キーフィールド名です。一部のコレクションでは string[] 型の複合主キーになる場合があります。設定されていない場合は、一般的に 'id' がフォールバックとして使用されます。
  • 子テーブルや関連フィールドなどのシーンでは、ctx.collection は関連先のコレクションを指すことがあり、ctx.blockModel.collection とは異なる場合があります。
  • getFields() は継承されたコレクションのフィールドをマージします。自身のフィールドは、同名の継承フィールドを上書きします。

#関連情報

  • ctx.collectionField:現在のフィールドのコレクションフィールド定義
  • ctx.blockModel:現在の JS を保持する親ブロック(collection を含む)
  • ctx.model:現在のモデル(collection を含む場合がある)