shuliangfu 2016-12-23 13:40:22 7809次浏览 2条评论 7 2 0

视图的_columns.php文件,这里用到的是crudajax生成的

<?php
use yii\helpers\Url;
use common\models\ProductCategory;

return [
    [
        'class' => 'kartik\grid\SerialColumn',
        'width' => '30px',
    ],

    [
        //'class' => '\kartik\grid\DataColumn',
        'attribute' => 'name',
        'class' => '\kartik\grid\EditableColumn',
    ],
    [
        'class' => '\kartik\grid\DataColumn',
        'attribute' => 'category.name',
    ],

    [
        'class' => '\kartik\grid\EditableColumn',
        'attribute' => 'price',
    ],

    'created_at:datetime',
    
    [
        'class' => 'kartik\grid\ActionColumn',
        'dropdown' => false,
        'vAlign' => 'middle',
        'urlCreator' => function ($action, $model, $key, $index) {
            return Url::to([$action, 'id' => $key]);
        },
        'header' => Yii::t('app', '操作'),
        'viewOptions' => ['role' => 'modal-remote', 'title' => 'View', 'data-toggle' => 'tooltip'],
        'updateOptions' => ['role' => 'modal-remote', 'title' => 'Update', 'data-toggle' => 'tooltip'],
        'deleteOptions' => ['role' => 'modal-remote', 'title' => 'Delete',
            'data-confirm' => false, 'data-method' => false,// for overide yii data api
            'data-request-method' => 'post',
            'data-toggle' => 'tooltip',
            'data-confirm-title' => Yii::t('app', '操作确认'),
            'data-confirm-message' => Yii::t('app', '你确定要删除这个选项吗?')],
    ],

];

在控制器index方法里加上一段

/**
 * Lists all Product models.
 * @return mixed
 */
public function actionIndex()
{    
    $searchModel = new ProductSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


    if ($this->isAjax && Yii::$app->request->post('hasEditable')) {
        Yii::$app->response->format = Response::FORMAT_JSON;
        
        $model = Product::findOne($this->post['editableKey']);

        $out = ['output' => '', 'message' => ''];

        $this->post[$model->formName()] = $this->post[$model->formName()][$this->post['editableIndex']];

        if ($model->load($this->post)) {
            // can save model or do something before saving model
            if($model->save()) {

                $output = '';

                /*if (isset($this->post[$model->formName()]['price_cny'])) {
                    $output = Yii::$app->formatter->asDecimal($model->price, 2);
                }*/

                $out = ['output' => $output, 'message' => ''];
            }else{
                $out['message'] = $model->getErrors();
            }
        }else{
            $out['message'] = $model->getErrors();
        }
        return $out;
    }
    

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

基中的$this->isAjax和$this->post,是我在基类定义好了的,同学们可以改为自己的

下面是效果图
---错误 (#8): 服务器内部错误。图片弄不了

如果需要做权限控制之类的,修改提交地址不想直接放在index方法,可以自定义提交地址,加上editableOptions参数即可

[
    //'class' => '\kartik\grid\DataColumn',
    'class' => '\kartik\grid\EditableColumn',
    'attribute' => 'price',
    'editableOptions'=> function ($model, $key, $index) {
        return [
            //'size'=>'sm',
            'formOptions' => [
                'method'=>'post',
                'action' => ['editable']
            ]
        ];
    }
],
觉得很赞
您需要登录后才可以评论。登录 | 立即注册