2017-02-19 92 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'); 
} 

使标题显示在其各自的作者选项组下。

现在我要以改革的形式采取从AJAX的优势,所以我采取了例如在Krajee Demo Site for Select2和重新整理如下:

------查看--------- -

<?= $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; }"), 
     ], 
]) ?> 

--------控制器-------------

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'); 
} 

按卡尔蒂克的选择二docs,ArrayHelper ::地图是这样的当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行的开头遗漏了一个反斜杠,因此出现这个错误。无论如何,映射optgroups的问题依然存在。我会继续尝试,直到我可以发布适当的解决方案。 –

回答

1

我自己想通了。首先,我们必须关心需要JSON字符串养活基于AJAX的选择二,必须是这样的(注意特殊的关键字resultsidtextchildren基于AJAX的选择二要求建设optgroups)

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" 
          ) 
        ) 
      ) 
    ) 
) 

然而,四参数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; 
} 

所以,就是这样。希望能帮助到你。

相关问题