1.Composer安装yii2-grid
cmd进入basic根目录,输入命令
composer require kartik-v/yii2-grid "@dev"
2.Composer安装yii2-editable
composer require kartik-v/yii2-editable "@dev"
3.配置config/web.php
$config = [ ... //模块配置start 'modules' => [ 'gridview' => [ 'class' => '\kartik\grid\Module', ], ], //模块配置end 'components' => [ ... ] ]
view文件里
<?php use yii\helpers\Html; //use yii\grid\GridView;//注释掉原来的GridView use kartik\grid\GridView; use kartik\editable\Editable; /* @var $this yii\web\View */ /* @var $searchModel app\models\AccountreadySearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = '现户'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="accountready-index"> <h1><!--<?= Html::encode($this->title) ?>--></h1> <?php // echo $this->render('_search', ['model' => $searchModel]); ?> <p> <?= Html::a('现户登记', ['create'], ['class' => 'btn btn-success']) ?> </p> <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'export' => false,//取消导出功能,因为有个PDF关联没安装 'hover' => true,//鼠标停留的那一行颜色变暗 'columns' => [ ['class' => 'yii\grid\SerialColumn'], 'id', 'media', //'user', [ 'attribute' => 'user', 'class'=>'kartik\grid\EditableColumn', 'editableOptions'=>[ 'asPopover' => false,//非弹窗模式修改 ], ], 'class', //'remark:ntext', [ 'attribute' => 'remark', 'class'=>'kartik\grid\EditableColumn', 'headerOptions' => ['width' => '400'], 'editableOptions'=>[ 'asPopover' => false, 'inputType'=>\kartik\editable\Editable::INPUT_TEXTAREA, 'options' => [ 'rows' => 14, ], ], ], ['class' => 'yii\grid\ActionColumn'], ], ]); ?> </div>
Controller文件
/** * Lists all Accountready models. * @return mixed */ public function actionIndex() { $searchModel = new AccountreadySearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); if (Yii::$app->request->post('hasEditable')) { $id = Yii::$app->request->post('editableKey');//就是mysql表里的id字段 $model = Accountready::findOne(['id' => $id]); $post = ['Accountready' => current($_POST['Accountready'])];//Accountready是表的名称 $out = ['output' => '', 'message' => ''];//output就是显示在GridView的内容 if ($model->load($post) && $model->save()) { $out = Json::encode(['output'=>'']); }else{ $out['message'] = $model->getErrors(); } return $out; } return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); }
修改成功


浙公网安备 33010602011771号