Pjax在Yii2的gridview搜索上重新加载页面

时间:2019-03-19 10:18:57

标签: php ajax gridview yii2 pjax

我正在用ajax请求在页面内加载gridview。 然后,在页面加载后,我想让用户像往常一样使用gridview进行排序和搜索,显然不重新加载页面。

虽然排序有效,但搜索会重新加载页面(由于加载ajax内容的操作与当前操作不同,因此页面会完全更改)。我知道Pjax在超时值之后会重新加载整个页面,但这不是问题,因为我更改为非常高的值并且仍然可以重新加载。

而且,这是页面上唯一的pjax。

可能是什么问题?

这是带有gridview的视图的代码     

    <?php Pjax::begin([
        "id" => "associates-ajax-list",
        "enablePushState" => FALSE,
        "enableReplaceState" => FALSE,
        "timeout" => 5000,
    ]); ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'name',
            'surname',

        ],
    ]); ?>
    <?php Pjax::end(); ?>
</div>

这是ajax操作的代码

public function actionAssociatesList($id) {
    $searchModel = new \app\models\AssociateSearch();
    $searchModel->associates_for = $id;
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    if (Yii::$app->request->isAjax) {
        return $this->renderAjax('associates_list', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    } else {
        return $this->render('associates_list', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }
}

加载该内容的页面是使用Gii生成的标准视图页面

2 个答案:

答案 0 :(得分:0)

您可以尝试在视图中添加此脚本吗?

   {
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "AngularDemo": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
         "styleext": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/AngularDemo",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
              "src/styles.css"
            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.min.js",
              "node_modules/bootstrap/dist/js/bootstrap.min.js"
            ],
            "es5BrowserSupport": true
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "AngularDemo:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "AngularDemo:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "AngularDemo:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.css"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "AngularDemo-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "prefix": "",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "AngularDemo:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "AngularDemo:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "AngularDemo",
  "schematics": {
    "@schematics/angular:component": {
      "styleext": "scss"
    }
  }
}

您可能需要更改一些选择器,因为我不知道HTML在您的项目中的样子。

而且,如果搜索字段位于<?php $this->registerJs( '$("document").ready(function(){ $("#associates-ajax-list").on("pjax:end", function() { $.pjax.reload({container:"#associates-ajax-list"}); //Reload GridView }); });' ); ?> 中,则可能需要一些JS来阻止默认的提交操作。否则,表单将在PJAX触发之前提交。

答案 1 :(得分:0)

问题在于gridview。 您需要指定一个ID。 有趣的是,此gridview是为此页面生成的唯一视图,因此显然没有冲突。

<?= GridView::widget([
        'id' => "grid-view-name",
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,