jaegyo23
(Assej Antivola)
October 13, 2017, 8:38am
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
(Softark)
October 13, 2017, 10:52am
2
May be this?
['value' => $model['ID']]
jaegyo23
(Assej Antivola)
October 14, 2017, 2:03pm
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
(Softark)
October 14, 2017, 10:58pm
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
(Jkofsky)
October 18, 2017, 8:17pm
5
$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.
jaegyo23
(Assej Antivola)
October 20, 2017, 6:27am
6
jkofsky:
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’];
jaegyo23
(Assej Antivola)
October 20, 2017, 6:29am
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
pawansgi92
(Pawan Nagar)
October 27, 2018, 9:55am
8
Can we send two button under single return as array???