T
T
Tat622018-05-01 13:56:59
Frameworks
Tat62, 2018-05-01 13:56:59

How to bind modal window call to ActionColumn buttons in GridView Yii2?

It is not possible to bind the call of the modal window to the ActionColumn buttons in the GridView, for example, update.
If you do not call the modal window, everything works fine. But as soon as I make a call through a modal window, an empty window appears. When you call the modal window using the "create" button, everything works fine too.
Here are excerpts from my code:
-------------------------------------- --------------------------------------
index.php
--------- -------------------------------------------------- -----------------------
<?= Html::encode($this->title) ?>
<?= Html::encode($_page) ?>
<?= Html::button('Create', ['value' => Url::to('index.php?r=manual1/create'), 'class' => '
<?php
Modal::begin([
'id' => 'modal',
]);
echo "";
modal::end();
?>
<?php Pjax::begin([
'linkSelector' => false
]); ?>
<?=
GridView::widget( [
'dataProvider' => $dataProvider,
// 'pager' => ['maxButtonCount' => 5],
'columns' => [
[
'class' => 'yii\ grid\SerialColumn'
],
[
'label' => "Code",
'attribute' => 'id',
},
],
[
'label' => "Name",
'attribute' => 'name',
'value' => function($data) {
return $data["name"];
},
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
'buttons'=>[
'delete' => function ($url, $model , $key) {
return Html::a('', $url, [
'title' => Yii::t('yii', 'Delete'),
'
'data-pjax' => '0',
]);
},
'update' => function($url,$model,$key){
$btn = Html::button("",[
'value' => Url::to('index.php?r=manual1/ update?id='.$key),
'class'=>'update-modal-click',
'data-pjax'=>0,
]);
return $btn;
}
]
],
]
] ); ?>
<?php Pjax::end(); ?>
------------------------------------------------ ----------------------------------
update.php
------------- -------------------------------------------------- -------------------
$this->
$this->params['breadcrumbs'][] = $this->title;
?>
<?= Html::encode($this->title) ?>
<?= $this->render('_form', [
'model' => $model,
] ); ?>
------------------------------------------------ ----------------------------------
_form.php
------------- -------------------------------------------------- -------------------
<?php $form = ActiveForm::begin(['id' => 'edit-form'
]); ?>
<?= $form->field($model, 'name')->textInput(['autofocus' => true])->label('Name') ?>
<?= $form->field( $model, 'cod')->textInput()->label('Code') ?>
<?= Html::submitButton('
-------------------------------------------------- --------------------------------
actionIndex
----------------- -------------------------------------------------- ---------------
public function actionIndex()
{
$this->FormArrayFromTable_1();
$this->FormDataProvider();
return $this->render('index', [
'model' => $model, 'dataProvider' => $this->dataProvider,
]);
}
------------------------------------------------- ---------------------------------
FormArrayFromTable_1
---------------- -------------------------------------------------- ----------------
public function FormArrayFromTable_1()
{
$this->data[] = ['id' => '100', 'name' => 'admin'];
$this->data[] = ['id' => '200', 'name' => 'admin'];
$this->data[] = ['id' => '300', 'name' => 'demo'];
$this->data[] = ['id' => '400', 'name' => 'demo'];
$this->data[] = ['id' => '500', 'name' => 'admin'];
}
------------------------------------------------- ---------------------------------
FormDataProvider
---------------- -------------------------------------------------- ----------------
public function FormDataProvider()
{
$this->
'sort' => [
'attributes' => ['id', 'name'],
],
]);
$this->row = $this->dataProvider->getModels();
$this->cnt = $this->dataProvider->getTotalCount();
}
------------------------------------------------- ---------------------------------
actionUpdate
---------------- -------------------------------------------------- ----------------
public function actionUpdate($id)
{
// get model by id
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()))
{
// update post in database
return $this->redirect(['index']);
return $this->renderAjax('update', [
'model' => $model,
]);
}
}
------------------------------------------------ ----------------------------------
jscript :
--------------- -------------------------------------------------- ----------------
$(document).ready(function () {
$('#btnModal').on('click', function (e) {
$('# modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
} );
$('.update-modal-click').on ('click', function (e) {
e.preventDefault();
$('#modal').modal('show')
.find('#modalContent')
.load($(this). attr('value'));
});
});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question