Answer the question
In order to leave comments, you need to log in
Yii2. Pajax in GridView updates all rows, what should I do?
Hello! I am familiar with Yii2 for about 5 days.
There is the following code in the view:
<?php Pjax::begin(); ?>
<?=
GridView::widget([
'dataProvider' => $products,
'tableOptions' => [
'class' => 'table table-striped table-bordered category-table'
],
'columns' => [
//....
[
'label' => 'Добавить',
'format' => 'raw',
'value' => function($data) use (&$result) {
$cat = $data->attributes['Category'];
$id = $data->attributes['ID'];
if (!$result) {
return Html::beginForm(['/site/category?name='.$cat],'post',['id'=>'add-prod-'.$id,'data-pjax'=>'0','class'=>'favorites-form']) . Html::hiddenInput('productID', $id) . Html::submitButton('Добавить', ['class' => 'mps button add-to-favorites']) . Html::endForm();
}
elseif ($result === 'Добавлено') {
return '<p class="added">'.$result.'</p>';
}
elseif($result === 'Ошибка') {
return '<p class="non-added"><a href="/site/category?name='.$cat.'">'.$result.'</a></p>';
}
}
],
],
]); ?>
<?php Pjax::end(); ?>
public function actionCategory($name)
{
$products = Category::getCatProv($name);
$result = '';
if(isset($_POST['productID'])) {
$getProduct = GetProduct::getProd(Yii::$app->request->post('productID'));
$result = 'Ошибка';
}
else {
$getProduct = '';
}
$userID = Yii::$app->user->id;
if($getProduct && $userID) {
$model = new AddFavorites();
//...
if( $model->save() ) {
$result = 'Добавлено';
}
}
return $this->render('category',['products'=>$products,'result'=>$result]);
}
Answer the question
In order to leave comments, you need to log in
In general, washed down while a patch. I added $result_id to the controller like this:
$result_id = '';
if(isset($_POST['productID'])) {
$getProduct = GetProduct::getProd(Yii::$app->request->post('productID'));
$result_id = Yii::$app->request->post('productID');
$result = 'Ошибка';
}
'value' => function($data) use (&$result, &$result_id) {
$cat = $data->attributes['Category'];
$id = $data->attributes['ID'];
$form = Html::beginForm(['/site/category?name='.$cat],'post',['data-pjax'=>'0','class'=>'favorites-form']) . Html::hiddenInput('productID', $id) . Html::submitButton('Добавить', ['class' => 'mps button add-to-favorites']) . Html::endForm();
if (!$result) {
return $form;
}
elseif (($result === 'Добавлено') && ($result_id == $id)) {
return '<p class="added"><a href="/site/category?name='.$cat.'">'.$result.'</a></p>';
}
elseif(($result === 'Ошибка') && ($result_id == $id)) {
return '<p class="non-added"><a href="/site/category?name='.$cat.'">'.$result. '</a></p>';
}
else {
return $form;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question