yii2 插件使用
GridView插件
行内文本编辑
后端
if (Yii::$app->request->post('hasEditable')) { $id = Yii::$app->request->post('editableKey'); $model = ModulePosition::findOne($id); $out = ['output' => '', 'message' => '']; $posted = current($_POST['ModulePosition']); $post = ['ModulePosition' => $posted]; if ($model->load($post) && $model->save(false)) { $out = ['output' => '', 'message' => '']; } else { $out['message'] = $model->getErrors(); } echo Json::encode($out); return; }
前端
引入 use kartik\grid\GridView; 'export' => false,//使用kar.必须进行此处配置 [ 'class' => 'kartik\grid\EditableColumn', 'attribute' => 'name', 'editableOptions' => [ 'placement' => 'right', 'header' => '广告名称', 'size' => 'md' ], 'vAlign' => 'middle', 'format' => 'raw', ],
状态切换
[ 'class' => 'kartik\grid\EditableColumn', 'attribute' => 'status', 'value' => function ($model) { return \jrhbadmin\models\Report::getReportStatus()[$model->status]; }, 'format' => 'raw', 'headerOptions' => ['width' => '80px'], 'editableOptions' => [ 'placement' => 'left', 'inputType' => Editable::INPUT_DROPDOWN_LIST, 'data' => \jrhbadmin\models\Report::getReportStatus(), ], 'filter' => \jrhbadmin\models\Report::getReportStatus(), ],
多选
[ 'label'=>'标签', 'attribute'=>'tag', 'class'=>'kartik\grid\EditableColumn', 'value'=>function($model){ if($model->tag){ return implode(',',$model->tag); }else{ return ''; } }, 'editableOptions'=>function($model){ $data=ArrayHelper::map(Tag::find()->all(),'tag','tag'); if($model->profile->tag){ $model->tag=explode(',',$model->profile->tag); $data=array_merge($data, array_combine($model->tag,$model->tag)); } return [ 'placement'=>'left', 'size'=>'md', 'inputType'=>\kartik\editable\Editable::INPUT_SELECT2, 'options'=>[ 'data' => $data, 'options' => ['multiple' => true], 'pluginOptions' => [ 'tags' => true, 'tokenSeparators' => [',', ' '], 'maximumInputLength' => 10 ], ], ]; }, ],