参考 Yii2 Modal Dialog on Gridview view and update button shows same content for both buttons 和 How to implement Yii2 Modal Dialog on Gridview view and update button?
我通过点击gridview上的更新按钮获得模态对话框,其中所选行的ID参数正确。但是当我在gridview上使用搜索和分页时,问题就出现了。模态对话框似乎不再起作用,单击更新按钮将显示模态对话框,但ID参数与所选行不匹配。说实话,似乎gridview不再知道registerJS了。有人可以建议如何解决这个问题吗?
<?php
$gridColumns = [
[
//'class' => 'kartik\grid\EditableColumn',
'attribute' => 'branch_id',
'pageSummary' => true,
],
[
'class' => 'kartik\grid\ActionColumn',
'template' => '{update} {delete}',
'headerOptions' => ['width' => '20%', 'class' => 'activity-view-link',],
'contentOptions' => ['class' => 'padding-left-5px'],
'buttons' => [
'update' => function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-star-empty"></span>','/branches/update?id='.$key.'', [
'class' => 'activity-view-link',
'title' => Yii::t('yii', 'Update'),
'data-toggle' => 'modal',
'data-target' => '#activity-modal',
'data-id' => $key,
'data-pjax' => '0',
]);
},
],
],
];?>
<?php Pjax::begin(); ?>
<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
// 'containerOptions' => ['style'=>'overflow: auto'], // only set when $responsive = false
'headerRowOptions'=>['class'=>'kartik-sheet-style'],
'filterRowOptions'=>['class'=>'kartik-sheet-style'],
'pjax' => true, // pjax is set to always true for this demo
'pjaxSettings'=>[
'neverTimeout'=>true,
'beforeGrid'=>'Branches Data',
'afterGrid'=>'My fancy content after.',
'enablePushState' => false,
'options' => ['id' => 'BranchesGrid'],
],
'bordered' => true,
'striped' => true,
'condensed' => true,
'responsive' => true,
'hover' => true,
'floatHeader' => true,
'panel' => [
'type' => GridView::TYPE_PRIMARY
],
]);
?>
<?php yii\widgets\Pjax::end() ?>
<?php $this->registerJs(
'
$(".activity-view-link").click(function(e) {
var fID = $(this).closest("tr").data("key");
$.get(
"update",
{
id: fID
},
function (data)
{
$("#activity-modal").find(".modal-body").html(data);
$(".modal-body").html(data);
$("#activity-modal").modal("show");
}
);
});
'
); ?&GT;
<?php Modal::begin([
'id' => 'activity-modal',
'header' => '<h4 class="modal-title">Branches Updatez</h4>',
'size'=>'modal-lg',
'footer' => '<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>',
]); ?&GT;
答案 0 :(得分:1)
发生这种情况是因为您将JS中的click事件处理程序绑定到已呈现的元素。因此,此处理程序仅影响第一页上的元素。在任何ajax更新之后,这些元素会消失。每次pjax更新后,您需要重新激活点击处理程序。
<?php Pjax::begin(['id'=>'some_pjax_id']); ?>
<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
// 'containerOptions' => ['style'=>'overflow: auto'], // only set when $responsive = false
'headerRowOptions'=>['class'=>'kartik-sheet-style'],
'filterRowOptions'=>['class'=>'kartik-sheet-style'],
'pjax' => true, // pjax is set to always true for this demo
'pjaxSettings'=>[
'neverTimeout'=>true,
'beforeGrid'=>'Branches Data',
'afterGrid'=>'My fancy content after.',
'enablePushState' => false,
'options' => ['id' => 'BranchesGrid'],
],
'bordered' => true,
'striped' => true,
'condensed' => true,
'responsive' => true,
'hover' => true,
'floatHeader' => true,
'panel' => [
'type' => GridView::TYPE_PRIMARY
],
]);
?>
<?php yii\widgets\Pjax::end() ?>
<?php $this->registerJs(
'
function init_click_handlers(){
$(".activity-view-link").click(function(e) {
var fID = $(this).closest("tr").data("key");
$.get(
"update",
{
id: fID
},
function (data)
{
$("#activity-modal").find(".modal-body").html(data);
$(".modal-body").html(data);
$("#activity-modal").modal("show");
}
);
});
}
init_click_handlers(); //first run
$("#some_pjax_id").on("pjax:success", function() {
init_click_handlers(); //reactivate links in grid after pjax update
});
');