0

我尝试使用可编辑列设置 Kartik Gridview。一列是类型INPUT_SELECT2

在视图中,我有以下定义:

$gridColumns = [
[
        'class'=>'kartik\grid\EditableColumn',
        'attribute'=>'occupation_ids',
         'value' => 'occuList',
        'editableOptions'=>[
            'header'=>'Occupations',
            'inputType'=>Editable::INPUT_SELECT2,
            'options' => [
                'attribute'=>'occupations',
                'data' => ArrayHelper::map(Occupation::find()->all(), 'id', 'description'), 
                'options' => [
                    'multiple' => true,
                ],                    
                'pluginOptions' => [
                    'tags' => true,
                ],
            ],
            'asPopover' => false,
        ],
    ],
...];

echo GridView::widget([
        'dataProvider'=>$dataProvider,
        'columns'=>$gridColumns,
        'filterModel' => $searchModel,
    ]);

保存值非常有效。但是,如果涉及到加载和显示保存的数据,我不得不四处寻找让事情运行起来。GridView 包含一个MonthlyEmployment. MonthlyEmployment有以下方法

 * @property Occupation[] $occupations
 */
class MonthlyEmployment extends \yii\db\ActiveRecord
{
    public $occupation_ids;

    public function getOccuList() {
        $arr = [];
        foreach ($this->occupations as $ocu) {
         $arr[] = $ocu->description;
        }
        if ($arr != null && !empty($arr)) {
            return implode(', ', $arr);
        } else {
             return '';
        }
    }

所以保存的职业显示在网格视图中。但是,如果我打开模块来编辑字段,则不会选择值。下图,看看我的意思。

GridView 可编辑列(多)Select2

那么有没有人使用 Kartik GridView、Editable 和 Select2 的工作示例?

4

1 回答 1

0

have one without tags; this is the controller:

public function actionIndex()
    {
      $model = new Model;
      $searchModel = new OrderSearch();
      $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

              if (isset($_POST['hasEditable'])) {
                      \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

                      if ($model->load($_POST)) {
                          $value = $model->status;
                          return ['output'=>$value, 'message'=>''];
                      }
                      else {
                          return ['output'=>'', 'message'=>''];
                      }
                  }

              return $this->render('indextoday', [
                  'searchModel' => $searchModel,
                  'dataProvider' => $dataProvider,
              ]);
    }

and this is the column:

<?php $data_from_desiredModel=ArrayHelper::map(desiredModel::find()
->orderBy('id')->asArray()->all(), 'id', 'name');?>

[
      'class'=>'kartik\grid\EditableColumn',
      'attribute'=>'name',
      'editableOptions'=>[
          'header'=>'Name',
          'inputType'=>\kartik\editable\Editable::INPUT_SELECT2,
          'data' => $data_from_desiredModel,
          ],
  ],
于 2017-07-05T09:09:20.157 回答