gpt4 book ai didi

ActiveForm 和 GridView 上的 Yii2 Pjax [让它工作起来]

转载 作者:行者123 更新时间:2023-12-01 19:37:11 30 4
gpt4 key购买 nike

index page
Modal when clicked on remove member
when form submitted
我有一个 GridView

<?php Pjax::begin(['id' => 'members']) ?> 

<?= GridView::widget([
'dataProvider' => $dataProvider,
// 'filterModel' => $searchModel,
'columns' => [
['header'=>'Remove Member',
'value'=> function($data)
{
return Html::a(Yii::t('app', ' {modelClass}', [
'modelClass' =>'',
]), ['members/stl_remove','id'=>$data->id],
['class' => 'btn btn-link fa fa-times fa-2x pop']
);
},
'format' => 'raw'
],
['class' => 'yii\grid\ActionColumn','template'=>'{update}'],
],
]); ?>
<?php Pjax::end() ?>

当我点击“删除成员”链接时,会弹出一个模式窗口(delete.php)

<?php Pjax::begin(['id' => 'delete_members']) ?>

<?php $form = ActiveForm::begin(['options' => ['data-pjax' => true ]]); ?>

<div class="row">
<div class="col-md-5">

<?php echo $form->field($model, 'remarks')->dropDownList(['death' => 'Death',
'marriage' => 'Marriage', 'house_shift' => 'House Shift'],['prompt'=>'Select'])
->label("Reason For Removal"); ?>

</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Remove') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>
<?php yii\widgets\Pjax::end() ?>

现在这些是我的 Controller 代码[代码]

public function actionStl_remove($id,$ajax=0)
{
$model = $this->findModel($id);
$model_delete = new Members();
$model->scenario = "remove";

if ($model_delete->load(Yii::$app->request->post()))
{
$model->remarks = $model_delete->remarks;
$model->status = 1;
$model->save();
// $model = new Members();
// return $this->redirect(['index']);
}
else
{
return $this->renderAjax('delete', [
'model' => $model_delete,
'id' => $id,
]);
}
}

我无法将数据加载到 gridview 中。我点击链接模式弹出窗口,然后提交,然后进入索引页面,页面重新加载

最佳答案

这就是我让它工作的方式。在 delete.php 中,我做了以下更改。

<?php $form = ActiveForm::begin([
'options' => ['data-pjax' => true,
'id'=> 'dynamic-form111',
// 'validationUrl' => 'validation-rul'
]]); ?>

<div class="row">
<div class="col-md-5">

<?php echo $form->field($model, 'remarks')->dropDownList(['death' => 'Death',
'marriage' => 'Marriage', 'house_shift' => 'House Shift'],['prompt'=>'Select'])
->label("Reason For Removal"); ?>

</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Remove') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

基本上我给了一个来自dynamic-form111的id。然后在 index.php 以及 pjax 中的 gridview 中我添加了以下代码

<?php
$this->registerJs(
'jQuery(document).ready(function($){
$(document).ready(function () {
$("body").on("beforeSubmit", "form#dynamic-form111", function () {
var form = $(this);
// return false if form still have some validation errors
if (form.find(".has-error").length)
{
return false;
}
// submit form
$.ajax({
url : form.attr("action"),
type : "post",
data : form.serialize(),
success: function (response)
{

$("#ajaxModal").modal("toggle");
$.pjax.reload({container:"#countries"}); //for pjax update

},
error : function ()
{
console.log("internal server error");
}
});
return false;
});
});

});'
);
?>

通过ajax检查delete.php是否已成功提交,如果是,则使用pjax重新加载gridview并关闭模式。

关于ActiveForm 和 GridView 上的 Yii2 Pjax [让它工作起来],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34651018/

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