简体   繁体   中英

Changing column name in yii cgridview based on database value

I am stuck in a situation where I have to change the column header based on database value. eg If database value is 1, column name should be 'Kick Off', If its 2 then column name should be 'Tip Off'. I used following code but its not working.

array(
    'name' => 'date', 
    'header' => '($data->league_id==1) ? "Kick Off" : "Tip Off"',
    'type' => 'raw',
    'value' => 'strtoupper(date("D M d", strtotime($data->date)))',
),

If you see CDataColumn (default if you use an array to specify the attributes), the header value is a string and will not be evaluated as a PHP expression.

If you take a look at CDataColumn's renderHeaderCellContent() method, you will notice that it uses the attribute name from the model.

$this->grid->dataProvider->model->getAttributeLabel($this->name)

So I don't see any problem with placing this in your model, which is where it should have belonged in the first place:

public function attributeLabels() {
    return array(
        'date' => '($this->league_id == 1) ? "Kick Off" : "Tip Off"',
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM