Yii Framework Forum: Problem With $Data In Gridview - Yii Framework Forum

Jump to content

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

Problem With $Data In Gridview problem with assuming value Rate Topic: -----

#1 User is offline   desatir7316 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 238
  • Joined: 19-March 14

Posted 09 June 2014 - 11:46 AM

hi there
i dont have a problem with this :

'value'=>'$data->item_id'

and no problem with this too:

'value'=>'$data->item->category_id'

and this is my custom class:

class Helper {

public static function getString($inputList, $spaceCount = 3){

return implode(str_pad(' ', $spaceCount), $inputList);

}

}

when i use 'value'=>'$data->item_id' like this again no problem:

'value'=> "Helper::getString(array(
\"<a href='".Yii::app()->createUrl('/item/view').'&id=$data->item_id'."'>\".
\"<img src='".Yii::app()->baseUrl."/image/categories/".'$data->item_id'. "'>\".
\"</a>\" ));",

but when use 'value'=>'$data->item->category_id' a problem occur :
'value'=> "Helper::getString(array(
\"<a href='".Yii::app()->createUrl('/item/view').'&id=$data->item_id'."'>\".
\"<img src='".Yii::app()->baseUrl."/image/categories/".'$data->item->category_id'. "'>\".
\"</a>\" ));",


and this is error msg:
Object of class Item could not be converted to string

thanks for thought on this
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,631
  • Joined: 04-March 10
  • Location:UK

Posted 09 June 2014 - 03:55 PM

I highly recommend that you use anonymous functions rather than trying to manage so much quote escaping. Also, the CHtml::link() method can help you with your links.

    'columns'=>array(
        array(
            'name'=>'blah',
            'value'=>function($data){
                $imageTag = CHtml::image(Yii::app()->baseUrl . '/images/categories/'
                        . $data->item->category_id);
                $linkTag = CHtml::link($imageTag, array('/item/view', 'id'=>$data->item_id));
                return Helper::getString(array($linkTag));
            },
        ),
    ),

0

#3 User is offline   desatir7316 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 238
  • Joined: 19-March 14

Posted 09 June 2014 - 11:12 PM

Thanks
But Because of my php version i cant use anonymous functions
0

#4 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,631
  • Joined: 04-March 10
  • Location:UK

Posted 10 June 2014 - 03:56 AM

Well, just create a function in your view file to serve the same purpose.

Somewhere near the top of the view file:
function getBlahColumn($data)
{
    $imageTag = CHtml::image(Yii::app()->baseUrl . '/images/categories/' . $data->item->category_id);
    $linkTag = CHtml::link($imageTag, array('/item/view', 'id'=>$data->item_id));
    return Helper::getString(array($linkTag));
}


Then your column config becomes:
    'value'=>'getBlahColumn($data)',


Feel free to wrap the function definition in a separate class or model as appropriate.

With this approach, you wrap far less of your code in a string, making it much easier to understand.
0

#5 User is offline   desatir7316 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 238
  • Joined: 19-March 14

Posted 10 June 2014 - 01:20 PM

thank you
but actually i dont want to have a func definition in every view file, if i have these definition so i only process input array in each view file nd dont need that helper class
how can i use my helper class with out anything between ...

thanks for you help
0

#6 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,631
  • Joined: 04-March 10
  • Location:UK

Posted 11 June 2014 - 02:48 AM

I don't understand. How is a function definition worse than a string expression which is evaluated? Especially one which has become so complicated that you're unable to debug it.
0

#7 User is offline   desatir7316 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 238
  • Joined: 19-March 14

Posted 11 June 2014 - 11:23 AM

thanks for ur replay
my only problem is that i want to concat $data->item->category_id with a string
0

#8 User is offline   Joblo 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 685
  • Joined: 12-September 10
  • Location:Austria

Posted 11 June 2014 - 02:51 PM

Instead of using anonymus functions to avoid much quote escaping you can use callable methods of your model, the current controller or a static method of a helper class.

See this wiki
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