Yii2 Kartik GridView直接修改記錄

Yii2的組件確實很豐富,直接使用Kartik CRUD Generator能夠直接生成能夠湊活的表單管理功能,今天是要在此基礎上添加一個可編輯的功能。須要安裝一個插件前端

composer require kartik-v/yii2-grid "@dev"

前端View修改後端

[
                'label'=>'編輯推薦',
                'format'=>'raw',
                'value' => function ($model) {
                    return \kartik\editable\Editable::widget([
                        'name' => 'promotion',
                        'value' => $model->promotion,
                        'attribute' => 'promotion',
                        'header' => '推薦',
                        'size' => 'md',
                        'formOptions' => [
                            'method' => 'post',
                            'action' => Yii::$app->urlManager->createAbsoluteUrl(['raw-taobaoke/promotion', 'id' => $model->id])
                        ],
                        'format' => 'button',
                        'inputType' => Editable::INPUT_DROPDOWN_LIST,
                        'data' => [
                            3 => '3',
                            1 => '1',
                            2 => '2',
                            4 => '4',
                            5 => '5',
                        ],
                        'options' => ['class' => 'form-control', 'placeholder' => 'Enter person name...'],

                    ]);
                },
            ],

後端Controller修改yii2

 public function actionPromotion($id)
    {
        $model = RawTaobaoke::findOne($id);
        if (isset($model)) {
            //設置是否客戶端推薦
            if (isset($_POST['promotion'])) {
                $latestPromotion = (int)$_POST['promotion'];
                $model->promotion = $latestPromotion;
                $model->save();
                echo \yii\helpers\Json::encode(['status' => "ok", 'promotion' => $latestPromotion]);
            }
        } else {
            echo \yii\helpers\Json::encode(['status' => "error", 'message' => "can not find item"]);
        }
    }

最終展現效果app

相關文章
相關標籤/搜索