A
A
Anar2016-07-27 15:07:52
Yii
Anar, 2016-07-27 15:07:52

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(); ?>

And in the controller something like this:
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]);
}

What's wrong. When you click on the "Add" button, the form sends the product ID to the controller and it adds it to the database, respectively, by design, if the entry has occurred, then $result = 'Added' is returned and the text "Added" is displayed instead of the button. The problem is that when you click on the add button, the button disappears for all rows in the grid and the inscription "Added" appears. Can anyone suggest a solution or point me to the error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anar, 2016-07-28
@Anarmus

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 = 'Ошибка';
  }

And in the view:
'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;
        }
    }

It's annoying to watch, but somehow it does its job.
I did not find how to make the payax be executed only from the called cell. If anyone knows how it should work and look, they will be eternally grateful.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question