Yii - Query data from model to change variable in

2019-07-21 07:04发布

问题:

I am rather new to Yii and am in need of creating a table that displays all the nodes (servers) from my company.

I have used Gii to generate most of the data and customized the CGridView to my liking. I am rather stumped at trying to grab a boolean value from each node to determine if the "status_ON" or "status_OFF" image should be displayed in their respective row.

How can I code it so the image is changed based on if the "isOnline" result from the database returns a 0(offline) or 1(online) without the need of javascript / ajax or similar?

Please be aware I am forced to use Yii v1.1.8.r3324

I hope I asked this correctly!

Model: Nodes

<?php
class Nodes extends CActiveRecord
{
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $criteria=new CDbCriteria;
        $criteria->compare('id',$this->id);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('url',$this->url,true);
        $criteria->compare('description',$this->description,true);
        $criteria->compare('node_type',$this->node_type,true);
        $criteria->compare('last_bounced',$this->last_bounced,true);
        $criteria->compare('isonline',$this->isonline);
        return new CActiveDataProvider($this, array(
                    'pagination'=>array(
                        'pageSize'=>Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
                    ),
            'criteria'=>$criteria,
        ));
    }
    // ...
}

Controller: NodeBouncer

<?php
class NodeBouncerController extends Controller
{
    /**
     * Lists all models.
     */
    public function actionIndex()
    {
        $model= new Nodes('search');
        $model->unsetAttributes();  // clear any default values

        if(isset($_GET['pageSize'])){
            Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);
            unset($_GET['pageSize']);
        }

        if(isset($_GET['Nodes']))
            $model->attributes=$_GET['Nodes'];

        return $this->render('index',array('model'=>$model,));
    }
    //...
}

View: index.php

<!-- 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 -->

回答1:

Here a small example. Hope this help you.

$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
                }
            }
           )
        )
);

At the same you can use Closure when php >= 5.3. Example:

//...
'value' => function ($data) use ($statusON_image, $statusOFF_image) {
//...


标签: php gridview yii