gpt4 book ai didi

gridview - 易2 : Pjax + Gridview delete does not send ajax request

转载 作者:行者123 更新时间:2023-12-02 19:16:40 24 4
gpt4 key购买 nike

我正在将 Pjax 与 Gridview 一起使用,并且我希望所有操作按钮都执行 ajax。默认情况下,他们不会,所以我在谷歌上搜索并找到了删除 data-pjax = 0 的方法。但是,仍然没有 ajax 请求,所有这些都是常规请求。

很多人都遇到这个问题,我也找不到解决方案。

我关注过:

我的代码:

<?php Pjax::begin(['id' => 'employee-timesheet-grid-id', 'timeout' => false, 'enablePushState' => false, 'clientOptions' => ['method' => 'POST']]) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'Employee',
'value' => function ($model) {
return $model->employeePayRate->employeeName;
},
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {delete}',
'buttons' => [
'delete' => function ($url , $model) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url,
['data-confirm' => 'Are you sure you want to delete this item?', 'data-method' =>'POST'] );
}
],
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'view') {
$url = Url::to(['employee-time-sheet/view', 'id' => $model->id]);
return $url;
} else if ($action === 'delete') {
$url = Url::to(['employee-time-sheet/delete', 'id' => $model->id]);
return $url;
}
}
],
],
]); ?>
<?php Pjax::end(); ?>

有人找到这个问题的解决方案了吗?

最佳答案

试试这个。 (我的工作代码)

首先在上面的 Gridview View 文件上注册以下 JavaScript我在这里使用 bootbox确认框。

$this->registerJs("

$(document).on('ready pjax:success', function () {
$('.ajaxDelete').on('click', function (e) {
e.preventDefault();
var deleteUrl = $(this).attr('delete-url');
var pjaxContainer = $(this).attr('pjax-container');
bootbox.confirm('Are you sure you want to change status of this item?',
function (result) {
if (result) {
$.ajax({
url: deleteUrl,
type: 'post',
error: function (xhr, status, error) {
alert('There was an error with your request.'
+ xhr.responseText);
}
}).done(function (data) {
$.pjax.reload({container: '#' + $.trim(pjaxContainer)});
});
}
}
);
});
});

");

下面是 Gridview 的代码

    <?php
\yii\widgets\Pjax::begin([
'id' => 'pjax-list',
]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'stu_category_name',
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {delete}',
'buttons' => [
'view' => function ($url, $model) {
return ((Yii::$app->user->can("/student/stu/view"))
? Html::a(
'<span class="glyphicon glyphicon-eye-open"></span>',
$url,
['title' => Yii::t('app', 'View'),]
)
: '');
},
'delete' => function ($url, $model) {
return ((Yii::$app->user->can("/student/stu/delete"))
? Html::a(
'<span class="glyphicon glyphicon-trash"></span>',
false,
[
'class' => 'ajaxDelete',
'delete-url' => $url,
'pjax-container' => 'pjax-list',
'title' => Yii::t('app', 'Delete')
]
)
: '');
}
],
],
],
]); ?>
<?php \yii\widgets\Pjax::end(); ?>

感谢@skworden支持此解决方案。此解决方案的 Yii 论坛链接。 click here

关于gridview - 易2 : Pjax + Gridview delete does not send ajax request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31038642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com