Php Yii-从模型查询数据以更改布局中的变量

Php Yii-从模型查询数据以更改布局中的变量,php,gridview,yii,Php,Gridview,Yii,我是Yii的新手,需要创建一个表来显示我公司的所有节点(服务器) 我使用Gii生成了大部分数据,并根据自己的喜好定制了CGridView。试图从每个节点获取一个布尔值来确定“status\u ON”或“status\u OFF”图像是否应该显示在各自的行中,这让我感到相当困惑 如果来自数据库的“isOnline”结果返回0(脱机)或1(联机),而不需要javascript/ajax或类似工具,我如何对其进行编码,从而根据这些结果更改图像 请注意,我被迫使用Yii v1.1.8.r3324 我希望

我是Yii的新手,需要创建一个表来显示我公司的所有节点(服务器)

我使用Gii生成了大部分数据,并根据自己的喜好定制了CGridView。试图从每个节点获取一个布尔值来确定“status\u ON”或“status\u OFF”图像是否应该显示在各自的行中,这让我感到相当困惑

如果来自数据库的“isOnline”结果返回0(脱机)或1(联机),而不需要javascript/ajax或类似工具,我如何对其进行编码,从而根据这些结果更改图像

请注意,我被迫使用Yii v1.1.8.r3324

我希望我问得对

模型:节点
这里是一个小例子。希望这对你有帮助

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'nodes-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        /* your columns... */
        array(
            'name' => 'isonline',
            // you can switch off sortable and filter
            //'filter' => false,
            //'sortable' => false,
            'type' => 'raw', // use when you render HTML
            'value' => function ($data) {
                if ($data->isonline) {
                    return CHtml::image("/images/abs/ButtonON.png"); // or other HTML
                } else {
                    return CHtml::image("/images/abs/ButtonOFF.png"); // or other HTML
                }
            }
           )
        )
);
同时,当php>=5.3时,可以使用闭包。例如:

//...
'value' => function ($data) use ($statusON_image, $statusOFF_image) {
//...
澄清:“filter”和“sortable”属性用于删除列(filter)顶部的大型白色搜索框,并防止用户在试图更改页面大小时对列进行排序,这是我发现的一个bug。否则,这个建议完全奏效了,我对此表示感谢!
<!-- node table -->
<?php
/* statusON/OFF_image variable used to change which image is displayed */
$statusON_image = 'CHtml::image("/images/abs/ButtonON.png")';
$statusOFF_image = 'CHtml::image("/images/abs/ButtonOFF.png")';
$pageSize = Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']);
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'nodes-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        array('class' => 'CCheckBoxColumn'),
        /* 'id', */
        'name',
        'url',
        'description',
        'node_type',
        'last_bounced',
        array(
            'name' => 'isonline',
            'header' => CHtml::dropDownList('pageSize', $pageSize, array(10 => 10, 20 => 20, 50 => 50, 100 => 100), array(
                'onchange' => "$.fn.yiiGridView.update('nodes-grid',{ data:{pageSize: $(this).val() }})",)),
            'type' => 'raw',
            'sortable'=>false,
            'value' => $statusOFF_image,
            'htmlOptions' => array('id' => 'NB_status'),
            'filter' => '',
        ),
    )
        )
);
?>
<!-- node table END -->
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'nodes-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        /* your columns... */
        array(
            'name' => 'isonline',
            // you can switch off sortable and filter
            //'filter' => false,
            //'sortable' => false,
            'type' => 'raw', // use when you render HTML
            'value' => function ($data) {
                if ($data->isonline) {
                    return CHtml::image("/images/abs/ButtonON.png"); // or other HTML
                } else {
                    return CHtml::image("/images/abs/ButtonOFF.png"); // or other HTML
                }
            }
           )
        )
);
//...
'value' => function ($data) use ($statusON_image, $statusOFF_image) {
//...