Php 如何在yii2中使用Pjax更改网格视图?
好的,我先解释一下我有什么 -一个名为cases的数据库表 这保存了我需要在gridview中显示的所有案例 -三个表称为Category、Subcategory和ChildCategory 表case中的案例将链接到childcategory 因此,我制作了三个DropDownLists,它们是从数据库中的各个类别表填充的。例如: _categories.phpPhp 如何在yii2中使用Pjax更改网格视图?,php,ajax,gridview,yii2,pjax,Php,Ajax,Gridview,Yii2,Pjax,好的,我先解释一下我有什么 -一个名为cases的数据库表 这保存了我需要在gridview中显示的所有案例 -三个表称为Category、Subcategory和ChildCategory 表case中的案例将链接到childcategory 因此,我制作了三个DropDownLists,它们是从数据库中的各个类别表填充的。例如: _categories.php <?= $this->render('_categories', [ 'model' => $model
<?= $this->render('_categories', [
'model' => $model,
'searchModel' => $searchModel,
'allCategory' => $allCategory
]) ?>
<?php Pjax::begin(['id' => 'cases']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'case_id',
'name',
'judgement_date',
'year',
'neutral_citation',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end() ?>
因此,当选择第一个类别时,它将运行onchange=>getSubcategory。这将基本上向我的控制器发送一个带有所选选项值的Ajax请求。然后它将拉回子类别,其中子类别_id=所选选项的值。然后用这些信息填充子类别下拉列表
此函数位于_categories.php上,上面有类别下拉列表
好的,这只是一点帮助你理解事物的分类方面。现在我有了一个gridview,在提交页面时从数据库填充它。然而,正如我使用ajax获取类别一样,当类别发生更改时,我需要使用pjax更改gridview
因此,在我的控制器中,actionIndex是通过gridview的searchModel和dataprovider发送的,如下所示:
CasesController.php
然后,在显示网格视图的索引页上:
注意::Index.php呈现上述类别下拉列表_categories.php
<?= $this->render('_categories', [
'model' => $model,
'searchModel' => $searchModel,
'allCategory' => $allCategory
]) ?>
<?php Pjax::begin(['id' => 'cases']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'case_id',
'name',
'judgement_date',
'year',
'neutral_citation',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end() ?>
选择最后一个childcategory时会调用此函数。我知道必须在此处执行某些操作,但我不知道是什么。
有人能帮忙吗?请在回答中添加一些解释,以便将来的读者能够轻松理解您的解决方案。
<?php use yii\widgets\Pjax; ?>
<?php Pjax::begin() ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'case_id',
'name',
'judgement_date',
'year',
'neutral_citation',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end() ?>
public function actionIndex()
{
$model = new Cases;
$searchModel = new CaseSearch();
$allCategory = Category::find()->all();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'allCategory' => $allCategory,
'model' => $model
]);
}
<?= $this->render('_categories', [
'model' => $model,
'searchModel' => $searchModel,
'allCategory' => $allCategory
]) ?>
<?php Pjax::begin(['id' => 'cases']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'case_id',
'name',
'judgement_date',
'year',
'neutral_citation',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end() ?>
function submitNow(){
$.pjax.reload({container:"#cases"}); //Reload GridView
}
<?php use yii\widgets\Pjax; ?>
<?php Pjax::begin() ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'case_id',
'name',
'judgement_date',
'year',
'neutral_citation',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end() ?>