2012-05-28 16 views
1

我下面的文件在這裏http://www.yiiframework.com/wiki/278/cgridview-render-customized-complex-datacolumns/CGridView編碼

所以我考慮以下

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'item-table-grid', 
'dataProvider'=>$model->search(), 
'itemsCssClass'=>'item-table-grid', 
'columns'=>array(
    'customer_name', 
    array(
     'name'=>'Edit', 
     'value'=>array($model, 'editLink'), 
    ), 
), 
)); 

,這裏是在模型

public function editLink($data, $row) { 
    $link = ''; 
    if ($data->is_draft) { 
     $link = '<a href="customer/update/'.$data->id.'">Edit</a>'; 
    } 
    return $link; 
} 

的問題EDITLINK函數值我得到的是返回值編碼,所以我得到< a href = ...>

有沒有辦法告訴CGridView不要編碼值?

感謝

+0

基本上,您可以在視圖級別對其進行編碼/解碼,但似乎並不是最佳選擇。 <?php echo CHtml :: encode($ link); ?> – Dharmavir

+0

它作爲編碼出來,我不喜歡使用解碼,我更喜歡讓它不在第一個地方編碼。設置類型爲raw似乎有竅門 –

回答

5

解決方案A:

array(
    'name'=>'Edit', 
    'type' => 'raw', 
    'value'=>array($model, 'editLink'), 
), 

B:(不夠好)

array(
    'name' => 'Edit', 
    'class' => 'CLinkColumn', 
    'urlExpression' => '$data->is_draft ? "customer/update/{$data->id}" : "#disabled"', 
    'label' => 'edit', 
), 
+0

我更喜歡解決方案A,因爲我需要處理模型中的特殊情況。把這個類型設置爲raw就行。謝謝! –

2

試試這個..

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'item-table-grid', 
'dataProvider'=>$model->search(), 
'itemsCssClass'=>'item-table-grid', 
'columns'=>array(
    'customer_name', 
    array(
'name'=>'Edit', 
'type' => 'raw', 
'value'=>array($model, 'editLink'), 

), ), ));

+0

工作正常!謝謝。對不起,我不能選擇兩個答案。這兩個答案都很好。 –

0
my code in CGridView 

array(
     'name'=>'Post Content', 
     'value'=>array($model,'postContent'), 
     'type'=>'html', 
     'htmlOptions'=>array('width'=>'380'), 
    ), 

in Model I have the following method 

public function postContent($data){ 
    echo $data->content; 
} 

it works fine but when i click on another page of my pagination 
then it doesn't works means it work only on Index page first time opened... 

plz any solution....???