GridView custom action button effectless

Hi,

I am using multilingual posts. I want the creator of a post to be able to grant translations authorisations to certain users for his own posts.

I created a posttranslator table (that represents translator assignments to posts) with these fields:

id, post_id, user_id (the translator id), lang

For a given post and a given language (lang) , I want to create a page where the creator of the post can allow user to translate the post in this language.

At the moment I have not yet covered all the aspects (specially ajax) but I met a problem with action button in the GridView.

Here is the ‘manage’ view




    <?php yii\widgets\Pjax::begin(['id' => 'new_translator']) ?>

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

    <?php

    //find users with author role only

    $users = User::find()->joinWith('assignments')->where(['item_name' => 'author'])->all(); 

 

    $items = array();

    foreach ($users as $user) {

       $items[$user->id] = $user->username;

    }

 ?>

    //display a dropDownList with all the users that have the role Author

    <?= $form->field($model, 'user_id')->label(Yii::t('app','Translator'))

             ->dropDownList($items) ?>


    //2 hidden fields to complete the post (values are url parameters) of this page

    <?= $form->field($model, 'post_id')->hiddenInput(['value'=>$post_id])->label(false); ?>


    <?= $form->field($model, 'lang')->hiddenInput(['value'=>$lang])->label(false); ?>


    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Add') :

         Yii::t('app', 'Update'), 

        ['class' => $model->isNewRecord ? 'btn btn-success' :

                                          'btn btn-primary']) ?>

    </div>


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

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





   <?php Pjax::begin(['id' => 'translators']) ?>   <?= GridView::widget([

        'dataProvider' => $dataProvider,

     //   'filterModel' => $searchModel,

        'columns' => [

           // ['class' => 'yii\grid\SerialColumn'],


            ['attribute' => 'id','label' => 'Posttranslator ID','headerOptions' => array('style' => 'width: 5%;'),],

            ['attribute' => 'user_id','headerOptions' => array('style' => 'width: 5%;'),],

            ['attribute' => 'user.username','label' => 'User name','headerOptions' => array('style' => 'width: 5%;'),],

    

            ['class' => 'yii\grid\ActionColumn', 

              'visibleButtons' => ['delete' => true, 'update' => false, 'view' => false],

             'urlCreator' => function ($action, $model, $key, $index) {

                                   if ($action === 'delete') {

                                      $url = $action.'?id='.$model->id.'&post_id='.$model->post_id.'&lang='.$model->lang;

                                       return $url;

                                    }

                              },

              'headerOptions' => array('style' => 'width: 10%;'),],

        ],

    ]); ?>

    <?php Pjax::end(); ?></div>


</div>

<?php $this->registerJs(

   '$("document").ready(function(){ 

        $("#new_translator").on("pjax:end", function() {

            $.pjax.reload({container:"#posttranslators"});  //Reload GridView


    });'

  

);?>

The important part is in the ActionColumn

The url created for the delete button seems to be correct (it appears as such when the mouse hover the button).

for example.

In the admin/posttranslator controller, the delete action is a follows:


    public function actionDelete($id,$post_id,$lang)

    {

        $this->findModel($id)->delete();

        return $this->redirect(['manage','post_id' => $post_id, 'lang'=>$lang]);   

    }

I pass the parameters post_id and lang in addition to id, to be able to come back to the initial page.

When clicked the confirm dialog opens but the action is not entered at all.

Using update button with several parameters is oK despite the fact the update action only needs id.

When disabling javascript on client it works perfectly.

I don’t understand what is wrong

For information but not absolutely necessary the manage action


     public function actionManage($post_id,$lang)

     {

        

        $model=new Posttranslator();


        if ($model->load(Yii::$app->request->post()) && $model->save())

        {

            $model = new Posttranslator(); //reset model

        }

       

      $query = Posttranslator::find()->joinWith('user')

               ->from('posttranslator','user')

             ->select(['posttranslator.id','post_id','user_id','lang','user.username'])

             ->where(['post_id' => $post_id, 'lang' => $lang])

               ;


      $provider = new ActiveDataProvider([

          'query' => $query,

          'pagination' => [

           'pageSize' => 10,

           ],

      ] );   




        return $this->render('manage', [       

            'model' => $model,

            'dataProvider' =>$provider,

            'post_id' => $post_id,

            'lang' => $lang,

        ]);

    }

I eventually found the answer. The url should be written like this:


        'urlCreator' => function ($action, $model, $key, $index) {

                        if ($action === 'delete') {

                            return Url::toRoute([$action, 'id' => $model->id, 'post_id' => $model->post_id, 'lang' => $model->lang]);

                        } else {

                            return Url::toRoute([$action, 'id' => $model->id]);

                        }

                    },