Yii Framework Forum: Confusion About Custom Function In Gridview - Yii Framework Forum

Jump to content

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

Confusion About Custom Function In Gridview

#1 User is offline   stuartrussell 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 23-September 13

Posted 29 March 2014 - 09:05 PM

Sorry if this is a basic question, I am new to Yii and advanced PHP.

I am unsure how to call a custom function in GridView:

In my Controller
public function stars($num){
            return str_repeat("*", $num);
        }


And in my View
['class' => '\kartik\grid\DataColumn',
        'attribute' => 'domain_rating',
         'format' => 'raw',
          'value' => ???,]


I don't know how to send the data of domain_rating into the stars() function.

any help would be appreciated.
0

#2 User is offline   Kartik V 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 598
  • Joined: 29-August 12

Posted 30 March 2014 - 12:12 AM

View Poststuartrussell, on 29 March 2014 - 09:05 PM, said:

I am unsure how to call a custom function in GridView:
I don't know how to send the data of domain_rating into the stars() function.any help would be appreciated.

You do not need the function in your controller... you can simply use a Closure function like below in your case
[
   'class' => '\kartik\grid\DataColumn',
   'attribute' => 'domain_rating',
   'format' => 'raw',
   'value' => function ($model, $index, $widget) {
       return str_repeat("*", $model->domain_rating); 
   }
]

0

#3 User is offline   stuartrussell 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 23-September 13

Posted 30 March 2014 - 12:42 AM

thank you for your reply.

but my confusion is where does $model, $index, $widget come from?

i keep getting an error 'Trying to get property of non-object'

meaning I am not clear what the model name is.
0

#4 User is offline   Kartik V 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 598
  • Joined: 29-August 12

Posted 30 March 2014 - 01:10 AM

View Poststuartrussell, on 30 March 2014 - 12:42 AM, said:

thank you for your reply.
but my confusion is where does $model, $index, $widget come from?

From the DataColumn specs... each row in the gridview is a model instance which will be automatically passed (remember the GridView dataProvider contains a list of models).
0

#5 User is offline   stuartrussell 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 23-September 13

Posted 30 March 2014 - 01:55 AM

thanks, that helped clear it up.
also my main problem was that
$model->domain_rating
didn't work, i had to use
$model['domain_rating']


What might the reason be that i couldn't access it via the
$model->domain_rating
way?
0

#6 User is offline   Kartik V 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 598
  • Joined: 29-August 12

Posted 30 March 2014 - 03:11 AM

View Poststuartrussell, on 30 March 2014 - 01:55 AM, said:

thanks, that helped clear it up.
also my main problem was that
$model->domain_rating
didn't work, i had to use
$model['domain_rating']


What might the reason be that i couldn't access it via the
$model->domain_rating
way?

Not sure why really? Do you have similar names, relations, or getters as `domain_rating`. You may try also including `domain_rating` in the safe attributes list and retry.
0

#7 User is offline   zelenin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 24-May 13

Posted 30 March 2014 - 04:38 AM

maybe asArray() or ArrayDataProvider are using
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