1 回答

TA貢獻1812條經驗 獲得超5個贊
我沒有指定一件事是重新加載 gridview,這不是那里的要求,但可以使用$.pjax
.
您需要注意的幾件事
updateStatus
根據您的要求,您不需要任何視圖來執行操作。您只需要將一些狀態代碼返回到您在 gridView 中用于切換輸入的 ajax 調用,并刷新您的 gridview 以加載新的更新狀態。
在 ajax 成功回調函數中檢查狀態代碼是否正常,那么您應該刷新 gridview 而不是使用 render partial。
你沒有更新任何內容
updateStatus
?你需要切換狀態
所以將您的操作更改為以下內容
public function actionUpdateStatus()
{
? ? try {
? ? ? ? $status = Yii::$app->request->post('status');
? ? ? ? $id = Yii::$app->request->post('id');
? ? ? ? $model = Customer::findOne($id);
? ? ? ? $model->status=$status;
? ? ? ? if(!$model->save()){
? ? ? ? ? ?throw new Exception(implode("\n",\yii\helpers\ArrayHelper::getColumn($model->errors,'0'));
? ? ? ? }
? ? ? ? return $this->asJson(['success' => true]);
? ? } catch (Exception $e) {
? ? ? ? return $this->asJson(['success' => false, 'message' => $e->getMessage()]);
? ? }
}
確保已將 GridView 包裝在 Pjax 開始和結束內,并將 an 添加id到 Pjax 小部件
<?php?
? ? Pjax::begin(['id'=>'pjax-container']);
? ? echo GridView::widget([
? ? 'dataProvider' => $customers,
? ? 'columns' => [
? ? .......
? ? ]);
? ? Pjax::end();
??>
您對以下內容的 ajax 調用
'switchChange.bootstrapSwitch' => 'function(e) {
? ? $.ajax({
? ? ? ? method: "POST",
? ? ? ? url: "'.Url::to(['update-status']).'",
? ? ? ? dataType:'json',
? ? ? ? data: { "status": e.currentTarget.checked, "id": e.currentTarget.name },
? ? ? ? success:function(data){
? ? ? ? ? ? ?if(data.success){
? ? ? ? ? ? ? ? ? $.pjax({container: "#pjax-container"});
? ? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? ? console.log(data.message);
? ? ? ? ? ? ?}
? ? ? ? },
? ? ? ? error:function(res){
? ? ? ? ? ? console.log(res.responseText);
? ? ? ? }
? ? });
}'
- 1 回答
- 0 關注
- 133 瀏覽
添加回答
舉報