2016-09-09 162 views
0

我試圖沒有成功地從數據庫的表中獲取元素,並根據一個條件在GridView中顯示它們。(Yii)根據CGridView中的條件替換數據庫值

這是我在GridView的代碼:

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'invoice-grid', 
'dataProvider' => $model->search(), 
'filter' => $model, 
'columns' => array(  

    'number', 
    'recipient_user_id', 
    'recipientName', 
    'recipientCompany', 
    'type' => function($data, $row) { 
        if ($data->type == 1) { return "Hello"; } 
        if ($data->type == 2) { return "Bye"; } 
        if ($data->type == 3) { return "Good"; } 
        if ($data->type == 4) { return "Milk"; } 
       }, 
    'periodFrom', 

    array(
     'class' => 'CButtonColumn', 
     'template'=>'{view}', 
    ), 
), 

)); ?>

它仍然無法正常工作。你有另一個想法嗎?

+0

對不起。我終於找到了我的錯誤。 – stfsngue

回答

0

對不起。我終於找到了我的錯誤。通常我應該這樣做:

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'invoice-grid', 
'dataProvider' => $model->search(), 
'filter' => $model, 
'columns' => array(  

    'number', 
    'recipient_user_id', 
    'recipientName', 
    'recipientCompany', 

     array(
       'name'=>'type', 
       'value'=>function($data, $row) { 
        if ($data->type == 1) { return "Hello"; } 
        if ($data->type == 2) { return "Bye"; } 
        if ($data->type == 3) { return "Good"; } 
        if ($data->type == 4) { return "Milk"; } 
       }, 

     ), 

    'periodFrom', 

    array(
     'class' => 'CButtonColumn', 
     'template'=>'{view}', 
    ), 
), 

)); ?>

相關問題