Hi, i have followed the following url to implement Pjax on a page.
http://www.yiiframework.com/wiki/772/pjax-on-activeform-and-gridview-yii2/
Now i want to update the records by same approach, what i want to do is,
when i click on the edit/update icon, form should be filled with the selected record, and when i will click on "update" button, the form should be updated and gridview should be reladed with Pjax.
i have tried to do this with Jquery Ajax, but pjax stop working after i manually refreshes/reloads the GridView.
For better understanding, you can also see the attached screenshot. Please guide me how i can do the update functionality using Pjax.
Here is my code.
_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\Food;
use yii\widgets\Pjax;
?>
<div class="shift-quotas-form">
<?php Pjax::begin(['id' => 'new_shift_quota'])?>
<?php $form = ActiveForm::begin(['options' => ['data-pjax' => true, 'class' => 'form-inline']]); ?>
<?= $form->field($model, 'food_id')->dropDownList(ArrayHelper::map(Food::find()->all(), 'food_id', 'short_desp'))->label('Food') ?>
<?= $form->field($model, 'qty')->textInput(['class' => 'small-field form-control']) ?>
<?= $form->field($model, 'vault_from')->textInput(['class' => 'small-field form-control', 'value' => '21', 'maxlength' => '3']) ?>
<?= $form->field($model, 'vault_to')->textInput(['class' => 'small-field form-control', 'value' => '25', 'maxlength' => '3']) ?>
<?php echo $form->field($model, 'shift_id', ['options' => ['class' => 'hidden']])->hiddenInput(['value' => $shift_id])->label('')?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success submit-btn' : 'btn btn-primary submit-btn']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end();?>
</div>
<?php
$this->registerJs(
'$("document").ready(function(){
$("#new_shift_quota").on("pjax:end", function(data, status, xhr, options) {
$.pjax.reload({container:"#shift_quotas"}); //Reload GridView
});
$("#new_shift_quota").on("pjax:error", function(xhr, textStatus, error, options) {
console.info(xhr, textStatus, error, options);
$.pjax.reload({container:"#shift_quotas"}); //Reload GridView
});
});
'
);
?>
ShiftQuotasController.php
public function actionIndex()
{
$model = new ShiftQuotas();
if (Yii::$app->request->post()) {
$model->scenario = $model::SCENARIO_ADD_SHIFT_QUOTA;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model = new ShiftQuotas(); // reset model
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
$searchModel = new ShiftQuotasSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
index.pp
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use app\models\Shifts;
use app\models\Food;
use yii\helpers\ArrayHelper;
use yii\widgets\Pjax;
$this->title = 'Shift Quotas';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="shift-quotas-index">
<h3><?= Html::encode($this->title) ?></h3>
<p>
<?php //Html::a('Create Shift Quotas', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<!-- Render create form -->
<?= $this->render('_form', [
'model' => $model,
'shift_id' => $shift_id
//'shiftRecords' => $shiftRecords
]) ?>
<?php
$pjax_grid_id = 'shift_quotas';
Pjax::begin(['id' => $pjax_grid_id, 'enablePushState' => false])?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'food_id',
'label' => 'Food',
'value' => function($modal){
$FoodData = Food::findOne(['food_id' => $modal->food_id]);
//echo '<pre>'; print_r($FoodData); echo '</pre>';
return $FoodData->short_desp;
},
'filter' => ArrayHelper::map(Food::find()->asArray()->all(), 'food_id', 'short_desp'),
],
'qty',
'vault_from',
'vault_to',
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
'buttons' => [
'update' => function($url, $model, $pjax_grid_id){
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', false, [
'class' => 'ajaxUpdate',
'update-url' => yii::$app->homeUrl . '/shift-quotas/update?id='. $model->quota_id,
'pjax-container' => $pjax_grid_id,
'title' => Yii::t('app', 'Update'),
'aria-label' => Yii::t('app', 'Update')
]);
},
'delete' => function ($url, $model, $pjax_grid_id) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>', false, [
'class' => 'ajaxDelete',
'delete-url' => yii::$app->homeUrl . '/shift-quotas/delete-shift-quota?id='. $model->quota_id,
'pjax-container' => 'shift_quotas',
'title' => Yii::t('app', 'Delete'),
//'data-method' => 'post',
//'data-pjax' => '0'
]
);
}
]
],
],
]); ?>
<?php Pjax::end();?>
</div>