代码之家  ›  专栏  ›  技术社区  ›  law_81

Yii2-使用Ajax加载Select2插件的设置值

  •  1
  • law_81  · 技术社区  · 7 年前

    我对yii2的Select2 kartik插件有一些问题。 我用Ajax加载来设置插件,在我的create视图中运行良好,所以我可以选择多个值并将其保存在数据库中。

    这就是我尝试过的。

    echo $form->field($model, 'lista_art')->widget(Select2::classname(), [
        'initValueText' => $art_list,// array of text to show in the tag for the selected items 
        'showToggleAll' => false,
        'options' => ['placeholder' => 'Select...',
                      'multiple' =>true,
                      'value' => $value, // array of Id of the selected items
                     ],
        'pluginOptions' => [
            'tags' => true,
            'tokenSeparators' => [',', ' '],
            'allowClear' => true,
            'minimumInputLength' => 3,
            'language' => [
                'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),
            ],
            'ajax' => [
                'url' => \yii\helpers\Url::to(['lista-articoli']),
                'dataType' => 'json',
                'data' => new JsExpression('function(params) { return {q:params.term}; }')
            ],
            'escapeMarkup' => new JsExpression('function (markup) { console.log(markup);return markup; }'),
            'templateResult' => new JsExpression('function(lista_art) { return lista_art.art_modello; }'),
            'templateSelection' => new JsExpression('function (lista_art) { return lista_art.art_modello; }'),
        ],
    ]);
    

    这就是结果。

    enter image description here

    $价值

    $art_list = ['name1','name2'];
    $value= ['id_name1','id_name2'];
    

    <li class="select2-selection__choice" title="name1">
        <span class="select2-selection__choice__remove" role="presentation">×</span>
    </li>
    

    更新 错误就在这里

     'templateResult' => new JsExpression('function(lista_art) { return lista_art.art_modello; }'),
     'templateSelection' => new JsExpression('function (lista_art) { return lista_art.art_modello; }')
    

    没有lista_艺术模型因为这个元素的对象的格式是 id:id\姓名1 文本:名称1 所以像这样修改代码就行了

    'templateResult' => new JsExpression('function(lista_art) { return lista_art.text; }'),
    'templateSelection' => new JsExpression('function (lista_art) { return lista_art.text; }')
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   law_81    7 年前

    我为我的问题找到了一个解决方案,我会为任何遇到同样问题的人发布一个答案。

    这是我视图中的Select2字段:

    echo $form->field($model, 'lista_art')->widget(Select2::classname(), [
        'initValueText' => $art_list,// array of text to show in the tag for the selected items 
        'showToggleAll' => false,
        'options' => ['placeholder' => 'Seleziona un prodotto',
                      //'tags' => true,
                      'multiple' =>true,
                      'value' => $value, // array of Id of the selected items
                      'class' => 'validatej'
                     ],
        'pluginOptions' => [
            'tags' => true,
            'tokenSeparators' => [',' , ' '],
            'allowClear' => true,
            'minimumInputLength' => 3,
            'language' => [
                'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),
            ],
            'ajax' => [
                'url' => \yii\helpers\Url::to(['lista-articoli']),
                'dataType' => 'json',
                'data' => new JsExpression('function(params) {return {q:params.term}; }')
            ],
            'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
            'templateResult' => new JsExpression('function(data) {return data.text; }'),
            'templateSelection' => new JsExpression('function (data) {  return data.text; }'),
        ],
    ]);
    

    public function actionGetArt($q = null, $id = null) {
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;//restituisco json
            $out = ['results' => ['id' => '', 'text' => '']];
            if (!is_null($q)) {
                $query = new Query;
                $query->select('art_cod AS id, art_modello AS text') 
                      ->from('art')
                      ->where(['ilike', 'art_modello', $q]);
                $command = $query->createCommand();
                $data = $command->queryAll();
                $out['results'] = array_values($data);
            }
            elseif ($id > 0) {
                $out['results'] = ['id' => $id, 'text' => Catalogo::find($id)->art_modello];
            }
    
            return $out;
        }
    

    在我调用selectsql的行中

    $query->select('art_cod AS id, art_modello AS text') 
    

    您必须根据select2小部件将id表和文本表(在我的例子中是artmodello)设置为id和文本。这在文档中没有指定,所以我已经阅读了代码并找到了这个解决方案。

        2
  •  0
  •   Imtiaz    7 年前

    你不能设置 initValueText 作为一个数组。 见文件:

    'initValueText' => $art_text, // set the initial display text
    

    data 取而代之的是:

    'data' => $art_list,