我希望能够在不刷新整个页面的情况下刷新pjax listview。这只是pjax列表本身的视图。
<?= Html::Button('refresh-countries', ['class' => 'btn btn-primary', 'name' => 'login-button', 'id'=>'refresh']) ?>
<?php Pjax::begin(['id' => 'countries']) ?>
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'comment-item'],
'itemView' => 'commentadapter',
]); ?>
<?php Pjax::end() ?>
请我刷新该按钮,只刷新列表视图。我知道怎么做,但它刷新了整个页面。
答案 0 :(得分:6)
你必须这样:
use yii\widgets\Pjax;
<?php Pjax::begin(['id' => 'some_pjax_id']) ?>
//your code
<?php Pjax::end() ?>
上面包含在tab中的表单,这是我重新加载pjax的脚本:
$("#my_tab_id").click(function() {
$.pjax.reload({container: '#some_pjax_id', async: false});
});
答案 1 :(得分:4)
PJAX有timeout
选项。如果PJAX在此超时期间未获得AJAX响应,则它将执行整页重新加载。
使用以下JS代码段:
$.pjax.defaults.timeout = false; // For JS use case yor should manual override default timeout.
$.pjax.reload({container: '#pjaxId'});
或更短的摘录:
$.pjax.reload('#pjaxId', {timeout : false});
此外,在我的项目中,我使用了Pjax的覆盖版本:
/**
* Custom Pjax with incremented timeout.
* JS for Pjax updating:
* <code>
* $.pjax.defaults.timeout = false; // For JS use case yor should manual override default timeout.
* $.pjax.reload({container: '#pjaxId'});
*
* // OR
* $.pjax.reload('#pjaxId', {timeout : false});
*
* // OR for gridview with search filters
* $('.grid-view').yiiGridView('applyFilter'); // Thats true only if you have search Filters
* </code>
*
* Note: In more cases ID of widget should be static, because widgetId is autoincremented and browser version of page may be not up-to-date.
*/
class Pjax extends \yii\widgets\Pjax
{
/**
* @var int Timeout {@link \yii\widgets\Pjax::$timeout}.
* For JS use case yor should manual override defaults ( $.pjax.defaults.timeout = false; ).
*/
public $timeout = 30000;
}
答案 2 :(得分:1)
只需使用电流过滤器重新加载网格:
$(".grid-view").yiiGridView("applyFilter");
重置高级搜索过滤器:
$("#search-form").find("input, select").val(""); // clear search form fields
$(".grid-view .filters input").val(""); // clear grid filters
window.history.pushState('object', document.title, window.location.href.split('?')[0]);
$.pjax.reload({container: '#wrapper-pjax', async: false});
答案 3 :(得分:0)
尝试按下Pjax下方的Refesh按钮:begin()。并设置url相同的当前网址。
实施例
<?php Pjax::begin(['id' => 'countries']) ?>
<?= Html::a("Refresh Countries", ['Your Current Url to view Country'], ['class' => 'btn btn-lg btn-primary']);?>
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'comment-item'],
'itemView' => 'commentadapter',
]);
?>
<?php Pjax::end() ?>
答案 4 :(得分:0)
Yii2 GridView列表通过Pjax刷新/更新
<?php
use yii\widgets\Pjax;
?>
<?php Pjax::begin(['id' => 'list-data-list', 'timeout' => false, 'enablePushState' => false]); ?>
<?=
GridView::widget([
dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => "<div class='tab-bg'>{summary}</div>\n\n<div class='table table-responsive list-table'>{items}\n{pager}</div>",
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'data_id',
[
'attribute' => 'data_type',
'label' => 'Data Type',
'format' => 'raw',
'value' => function ($model) {
return ($model->data_subtype_id) ? $model->dataName->parentDataName->name : '-';
},
],
]
]);?>
<?php Pjax::end(); ?>
这里是将更新的数据加载到gridview列表中的脚本。
$.pjax.reload({container: "#list-data-list", async: false});