Yii Framework Forum: Add value to action button in Yii2 - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Add value to action button in Yii2

#1 User is offline   Jaegyo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 05-September 17

Posted 13 October 2017 - 03:38 AM

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.
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,413
  • Joined: 16-February 11
  • Location:Japan

Posted 13 October 2017 - 05:52 AM

May be this?
['value' => $model['ID']]

0

#3 User is offline   Jaegyo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 05-September 17

Posted 14 October 2017 - 09:03 AM

View Postsoftark, on 13 October 2017 - 05:52 AM, said:

May be this?
['value' => $model['ID']]



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'  )];

0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,413
  • Joined: 16-February 11
  • Location:Japan

Posted 14 October 2017 - 05:58 PM

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.yiiframew...$buttons-detail
1

#5 User is offline   jkofsky 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 688
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 18 October 2017 - 03:17 PM

View PostJaegyo, on 14 October 2017 - 09:03 AM, said:

 $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'  )];


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.
Do, or do not. There is no 'try.' Jedi Master Yoda
0

#6 User is offline   Jaegyo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 05-September 17

Posted 20 October 2017 - 01:27 AM

View Postjkofsky, on 18 October 2017 - 03:17 PM, said:

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.



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

#7 User is offline   Jaegyo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 05-September 17

Posted 20 October 2017 - 01:29 AM

View Postsoftark, on 14 October 2017 - 05:58 PM, said:

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.yiiframew...$buttons-detail


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 :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users