matthew-p/yii2-models-select

选择模型小部件。

1.2 2018-07-10 10:50 UTC

This package is auto-updated.

Last update: 2023-08-19 22:55:44 UTC


README

在select2输入中查找并选择模型。

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require --prefer-dist matthew-p/yii2-models-select "*"

或者将以下内容添加到你的 composer.json 文件的 require 部分。

"matthew-p/yii2-models-select": "*"

使用

扩展安装完成后,只需在代码中使用即可

$form->field($model, 'attribute')->widget(MPModelSelect::class, [
    'searchModel'     => YouActiveRecordModel::class,
    'valueField'      => 'id',
    'titleField'      => 'title',
    'searchFields'    => [
        // convert to orWhere 'id' => query-string and etc.
        'id', 'title', 
        // add related input (will be added to data request and conver to ->andWhere 'category_id' => request value)
        'category_id' => new JsExpression('$("#category-id").val()'),
        // more examples see MPModelSelect::searchFields
    ],
    'dropdownOptions' => [
        'options'       => [
            'placeholder' => Yii::t('app', 'Select models ...'),
            'multiple'    => true,
        ],
        'pluginOptions' => [
            'minimumInputLength' => 1,
        ],
    ],
])

在控制器中添加动作

class SampleController extends Controller
{
...
    public function actions(): array
    {
        return array_merge(parent::actions(), [
            'model-search' => [
                'class' => MPModelSelectAction::class,
            ],
        ]);
    }
...
}

在params.php中定义加密密钥

'MPModelSelect'   => [
    'encryptionKey' => 'RandomKey',
],

就这些了。检查一下。