{"id":206,"date":"2020-05-31T07:03:29","date_gmt":"2020-05-30T23:03:29","guid":{"rendered":"http:\/\/blog.yuekegu.com\/?p=206"},"modified":"2020-05-31T07:03:29","modified_gmt":"2020-05-30T23:03:29","slug":"gridview%e7%9b%b4%e6%8e%a5%e6%9b%b4%e6%96%b0%e6%95%b0%e6%8d%ae%ef%bc%8ckartikgrideditablecolumn%e7%94%a8%e6%b3%95-2-0-%e7%89%88%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/book.yuekegu.com\/index.php\/2020\/05\/31\/gridview%e7%9b%b4%e6%8e%a5%e6%9b%b4%e6%96%b0%e6%95%b0%e6%8d%ae%ef%bc%8ckartikgrideditablecolumn%e7%94%a8%e6%b3%95-2-0-%e7%89%88%e6%9c%ac\/","title":{"rendered":"GridView\u76f4\u63a5\u66f4\u65b0\u6570\u636e\uff0ckartik\\grid\\EditableColumn\u7528\u6cd5 [ 2.0 \u7248\u672c ]"},"content":{"rendered":"\n<p>\u672c\u6587\u8f6c\u81ea\uff1ahttps:\/\/www.yiichina.com\/tutorial\/1110<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nuse yii\\helpers\\Url;\nuse common\\models\\ProductCategory;\n\nreturn [\n    [\n        'class' => 'kartik\\grid\\SerialColumn',\n        'width' => '30px',\n    ],\n\n    [\n        \/\/'class' => '\\kartik\\grid\\DataColumn',\n        'attribute' => 'name',\n        'class' => '\\kartik\\grid\\EditableColumn',\n    ],\n    [\n        'class' => '\\kartik\\grid\\DataColumn',\n        'attribute' => 'category.name',\n    ],\n\n    [\n        'class' => '\\kartik\\grid\\EditableColumn',\n        'attribute' => 'price',\n    ],\n\n    'created_at:datetime',\n    \n    [\n        'class' => 'kartik\\grid\\ActionColumn',\n        'dropdown' => false,\n        'vAlign' => 'middle',\n        'urlCreator' => function ($action, $model, $key, $index) {\n            return Url::to([$action, 'id' => $key]);\n        },\n        'header' => Yii::t('app', '\u64cd\u4f5c'),\n        'viewOptions' => ['role' => 'modal-remote', 'title' => 'View', 'data-toggle' => 'tooltip'],\n        'updateOptions' => ['role' => 'modal-remote', 'title' => 'Update', 'data-toggle' => 'tooltip'],\n        'deleteOptions' => ['role' => 'modal-remote', 'title' => 'Delete',\n            'data-confirm' => false, 'data-method' => false,\/\/ for overide yii data api\n            'data-request-method' => 'post',\n            'data-toggle' => 'tooltip',\n            'data-confirm-title' => Yii::t('app', '\u64cd\u4f5c\u786e\u8ba4'),\n            'data-confirm-message' => Yii::t('app', '\u4f60\u786e\u5b9a\u8981\u5220\u9664\u8fd9\u4e2a\u9009\u9879\u5417\uff1f')],\n    ],\n\n];<\/code><\/pre>\n\n\n\n<p>\n\u5728\u63a7\u5236\u5668index\u65b9\u6cd5\u91cc\u52a0\u4e0a\u4e00\u6bb5\n\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Lists all Product models.\n * @return mixed\n *\/\npublic function actionIndex()\n{    \n    $searchModel = new ProductSearch();\n    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);\n\n\n    if ($this->isAjax &amp;&amp; Yii::$app->request->post('hasEditable')) {\n        Yii::$app->response->format = Response::FORMAT_JSON;\n        \n        $model = Product::findOne($this->post['editableKey']);\n\n        $out = ['output' => '', 'message' => ''];\n\n        $this->post[$model->formName()] = $this->post[$model->formName()][$this->post['editableIndex']];\n\n        if ($model->load($this->post)) {\n            \/\/ can save model or do something before saving model\n            if($model->save()) {\n\n                $output = '';\n\n                \/*if (isset($this->post[$model->formName()]['price_cny'])) {\n                    $output = Yii::$app->formatter->asDecimal($model->price, 2);\n                }*\/\n\n                $out = ['output' => $output, 'message' => ''];\n            }else{\n                $out['message'] = $model->getErrors();\n            }\n        }else{\n            $out['message'] = $model->getErrors();\n        }\n        return $out;\n    }\n    \n\n    return $this->render('index', [\n        'searchModel' => $searchModel,\n        'dataProvider' => $dataProvider,\n    ]);\n}<\/code><\/pre>\n\n\n\n<p>\n\u5982\u679c\u9700\u8981\u505a\u6743\u9650\u63a7\u5236\u4e4b\u7c7b\u7684\uff0c\u4fee\u6539\u63d0\u4ea4\u5730\u5740\u4e0d\u60f3\u76f4\u63a5\u653e\u5728index\u65b9\u6cd5\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u63d0\u4ea4\u5730\u5740\uff0c\u52a0\u4e0aeditableOptions\u53c2\u6570\u5373\u53ef\n\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[\n    \/\/'class' => '\\kartik\\grid\\DataColumn',\n    'class' => '\\kartik\\grid\\EditableColumn',\n    'attribute' => 'price',\n    'editableOptions'=> function ($model, $key, $index) {\n        return [\n            \/\/'size'=>'sm',\n            'formOptions' => [\n                'method'=>'post',\n                'action' => ['editable']\n            ]\n        ];\n    }\n],<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u8f6c\u81ea\uff1ahttps:\/\/www.yiichina.com\/tutorial\/1110 \u5728\u63a7\u5236\u5668index\u65b9\u6cd5\u91cc\u52a0\u4e0a\u4e00\u6bb5 \u5982\u679c\u9700\u8981\u505a\u6743\u9650\u63a7\u5236\u4e4b\u7c7b\u7684\uff0c\u4fee\u6539\u63d0\u4ea4\u5730\u5740\u4e0d\u60f3\u76f4\u63a5\u653e\u5728index\u65b9\u6cd5\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u63d0\u4ea4\u5730\u5740\uff0c\u52a0\u4e0aeditableOptions\u53c2\u6570\u5373\u53ef<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[38,40],"class_list":["post-206","post","type-post","status-publish","format-standard","hentry","category-yii2","tag-gridview","tag-yii2"],"_links":{"self":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/comments?post=206"}],"version-history":[{"count":0,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"wp:attachment":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/tags?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}