天天看點

yii2架構中使用自動搜尋yii-widget-select2

1.利用composer安裝來安裝select2插件

在自己電腦的cmd下,到自己要安裝的目錄下

鍵入一下指令

安裝成功如下圖

yii2架構中使用自動搜尋yii-widget-select2

2.使用Select2

##引入Select2
use kartik\select2\Select2;


 <?= $form->field($model, 'sponsor')->widget(Select2::classname(), [
                'options' => ['placeholder' => '請輸入用戶名稱 ...'],
                'pluginOptions' => [
                    'placeholder' => 'search ...',
                    'allowClear' => true,
                    'language' => [
                      'errorLoading' => new JsExpression("function () { return '請輸入搜尋的會員..'; }"),
                    ],
                    'ajax' => [
                        'url' => 'user-one',
                        'dataType' => 'json',
                        'data' => new JsExpression('function(params) { return {q:params.term,a:"add"}; }')
                    ],
                    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),

                    'templateResult' => new JsExpression('function(res) {return res.text; }'),
                    'templateSelection' => new JsExpression('function (res) {
                    return res.text; }'),###當你點選選中的那個資料資訊,可以這個函數做處理
                ],
            ]);
           

官方文檔詳細說明