在我的页面之一中,有一个由pjax处理的链接。基本上,用户单击一个项目,该项目将被“选中”(并保存在数据库中)。
我已为这些请求禁用pushState,因为这毫无意义,用户实际上停留在同一页面上,因此更改URL是违反直觉的。
但是,在某些情况下,此pjax请求导致重定向到登录页面(当用户未登录时)。这是我真的需要pushState起作用的时候,但这不是因为我首先禁用了它。
是否有可能以正常响应在没有pushState的情况下配置pjax的方式配置,但重定向响应(使用X-Pjax-Url
标头完成)do执行pushState?
无法使用当前功能来做到这一点。我为pjax添加了两个选项,我的PR已被pjax的yii2分支接受。因此,事不宜迟:
https://github.com/yiisoft/jquery-pjax
//pushRedirect - Whether to pushState the URL for redirects. Defaults to false.
//replaceRedirect - Whether to replaceState the URL for redirects. Defaults to true.
// ...
jQuery(document).pjax("#example_selector", {
"push": false,
"replace": false,
"pushRedirect": true,
"replaceRedirect": false
});
使用linkSelector指定哪些链接触发pjax调用
<?php Pjax::begin([
'enablePushState' => false, // don't change the Browser URL
'linkSelector' => 'pjax-btn', // pjax links that
]); ?>
<?= GridView::widget([
//...
[ // no pjax, normal linl
'label' => 'link with state replace',
'format' => 'raw',
'value' => function ($model, $key, $index, $column) {
return Html::a($model->title, Url::to(['/controllet/action', 'id' => $model->id]));
}
],
[
'class' => 'yii\grid\ActionColumn',
//'class' => 'common\widgets\ActionColumn',
'template' => '{toggle} {view} {update} {delete}',
'buttons' => [
'toggle' => function ($url, $model, $key) {
$options = [
'title' => 'Privacy',
'aria-label' => 'Privacy',
'class' => 'pjax-btn', // no state replacement, load with pjax
];
$icon = Html::tag('span', '', ['class' => 'glyphicon glyphicon-check']);
return Html::a($icon, $url, $options);
},
]
]]) ?>
<?php Pjax::end(); ?>