2017-02-19 10 views
0

現在、私はKartikのSelect2に基づいてオプショングループを使って静的なドロップダウンをレンダリングするためのフォームフィールドを持っています。データは、次のように入力されます。Yii2:AjaxベースのKartikのSelect2でオプショングループを使用する方法

public function getBibliographyList() 
{ 
    return ArrayHelper::map($this->find()->select(['id','title','author']) 
       ->orderBy('author','title')->all(), 'id', 'title', 'author'); 
} 

タイトルはそれぞれの作成者オプショングループの下に表示されます。 ---------

------ VIEWを:

は、今私はAJAXの利点を活用するためのフォームを刷新したいので、私はKrajee Demo Site for Select2に例を取って、次のようにそれを作り直し - KartikのSelectセレクトdocs、ArrayHelperあたりとして

<?= $form->field($model, 'orig_id')->widget(Select2::classname(), [ 
     'options' => ['placeholder' => Yii::t('app', 'Select a title...)], 
     'pluginOptions' => [ 
      'allowClear'   => true, 
      'minimumInputLength' => 3, 
      'language'   => Yii::$app->language, 
      'theme'    => 'krajee', 
      'ajax' => [ 
       'url'  => \yii\helpers\Url::to(['search-doc']), 
       'dataType' => 'json', 
       'data'  => new JsExpression('function (params) { return {q:params.term}; }'), 
      ], 
      'errorLoading'  => new JsExpression("function() { return '".Yii::t('app', 'Waiting for data...')."'; }"), 
      'escapeMarkup'  => new JsExpression("function (markup) { return markup; }"), 
      'templateResult' => new JsExpression("function (bibliography) { return bibliography.title; }"), 
      'templateSelection' => new JsExpression("function (bibliography) { return bibliography.title; }"), 
     ], 
]) ?> 

-------- CONTROLLER -------------

public function actionSearchDoc($q = null) 
{ 
    Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; 
    $out = ['id' => '', 'title' => '', 'author' => '']; 
    if ($q) { 
     $query = new yii\db\Query; 
     $query->select('id, title, author') 
       ->from('bibliography') 
       ->where(['like', 'title', $q]) 
       ->orWhere(['like', 'author', $q]) 
       ->limit(50); 
     $command = $query->createCommand(); 
     $data = $command->queryAll(); 
     $out = array_values($data); 
    } 
    return \yii\helpers\ArrayHelper::map($out, 'id', 'title', 'author'); 
} 

::マップが道でありますoptgroupsがあるときに行くが、私はfiすることができないドロップアウトが常に空であるため、これを調べてください。 ArrayHelper :: mapのJSON文字列の例を次に示します。

{"results":{"Author1":{"4":"DocumentFoo1","121":"DocumentFoo2","219":"DocumentFoo3","197":"DocumentFoo4","198":"DocumentFoo5","2":"DocumentFoo6","273":"DocumentFoo7"},"Author2":{"68":"DocumentThee1"}}} 

アイデアはありますか?

+0

エラーメッセージを追加してください。 – marche

+0

mmmm、私はArrayHelper行の先頭にバックスラッシュがないため、このエラーが発生しました。とにかく、オプトグループのマッピングの問題は解決しません。私は適切な解決策を掲示できるまで試し続けます。 –

答えて

1

私はこれを自分で考え出しました。まず、このようにする必要がありAJAXベースのSelectセレクト養うために必要なJSON文字列を気にしなければなりません(予告特別なキーワードをresultsidtextとoptgroupsを構築するためにAJAXベースのSelectセレクトによって要求さchildren

Array 
(
[results] => Array 
    (
     [0] => Array 
      (
       [text] => "author1" 
       [children] => Array 
        (
         [0] => Array 
          (
           [id] => "id1" 
           [text] => "title1" 
          ) 
         [1] => Array 
          (
           [id] => "id2" 
           [text] => "title2" 
          )       
        ) 
      ) 
     [1] => Array 
      (
       [text] => "author2" 
       [children] => Array 
        (
         [0] => Array 
          (
           [id] => "id3" 
           [text] => "title3" 
          ) 
        ) 
      ) 
    ) 
) 

ただし、次のように4引数ArrayHelper::map出力は次のようになります。

Array 
(
    [author1] => Array 
     (
      [id1] => "title1" 
      [id2] => "title2" 
     ) 

    [author2] => Array 
     (
      [id3] => "title3" 
     ) 
) 

だから、ものを再配置し、それらの特別なキーワードを組み込む必要があります。したがって、適切なコントローラーアクションは、次のようになります。

public function actionSearchDoc($q = null) 
{ 
    // JSON format result. 
    Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; 
    $out['results'] = ''; 
    if ($q) { 
     $query = Bibliography::find()->select(['id', 'title', 'author']) 
            ->where(['like', 'title', $q]) 
            ->orWhere(['like', 'author', $q]) 
            ->all(); 
     // Group titles by author. 
     $authorArray = ArrayHelper::map($query, 'id', 'title', 'author'); 
     // Previous array lacks keywords needed to use optgroups 
     // in AJAX-based Select2: 'results', 'id', 'text', 'children'. 
     // Let's insert them. 
     $results = []; 
     foreach ($authorArray as $author => $docArray) { 
      $docs = []; 
      foreach ($docArray as $id => $title) { 
       $docs[] = ['id' => $id, 'text' => $title]; 
      } 
      $results[] = ['text' => $author, 'children' => $docs]; 
     } 
     $out['results'] = $results; 
    } 
    return $out; 
} 

これはそうです。それが役に立てば幸い。

関連する問題