Add value to action button in Yii2


(Assej Antivola) #1

I’m trying to add the ID on an ActionColumn’s button that’s coming from the database. So I can pass the ID to another page. But I’ve looked for answers but I dont seem to get one right. Here’s my code:


<?= 

    

    GridView::widget([

    'dataProvider' => $dataProvider,

    'columns' => [

        'ID',

        'Job Post',

        'Email',

        'Date Added',

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

           'header'=>'Action',

          'template' => '{search}',

            'buttons' => [

                'search' => function ($model) {

                    return Html::button('<i class="fa fa-search" aria-hidden="true"></i>', ['value' => $model->ID]);

 

                }

                ]

          ],

],

        'tableOptions' =>['class' => 'table table-bordered table-hover'],

        

]); 


?>



I want to add the “ID” as a value of my button search. But error shows: “TRYING TO GET PROPERTY FROM A NON OBJECT”. Please help. I’m new to Yii2.


(Softark) #2

May be this?




['value' => $model['ID']]




(Assej Antivola) #3

It shows an error: Illegal string offset ‘ID’. By the way, i’m using


SqlDataProvider

.




 $dataProvider = new SqlDataProvider ([

                  'sql' => 'SELECT a.user_jobpost Job Post,

                 b.email Email,

                 a.user_job_id ID,

                 a.user_job_date Date Added

                   FROM users_jobpost a,

                        user b 

                        WHERE b.id = a.user_id'  )];




(Softark) #4

You have a wrong signature of the callback, which should be:




    'search' => function ($url, $model, $key) {

        return Html::button('<i class="fa fa-search" aria-hidden="true"></i>', ['value' => $model['ID']]);

    },



http://www.yiiframework.com/doc-2.0/yii-grid-actioncolumn.html#$buttons-detail


(Jkofsky) #5

Does this actually return what you want? I’m thinking you need an AS in there (ie, b.email as Email)

I could be wrong, it’s been awhile since I’ve used raw SQL.


(Assej Antivola) #6

Yes it is returning what I want. :) I actually had this solved with $model[‘ID’];


(Assej Antivola) #7

Yes you are right. I think there was an error with my code thats why the $model[‘ID’] didn’t work. But now it worked. Thanks :)