2

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 Answer 1

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) {
//...
Sign up to request clarification or add additional context in comments.

1 Comment

Clarification: The "filter" and "sortable" attributes are there to remove the large white search box at the top of the column (filter) and to prevent the user from sorting the column when they attempt to change the page size, it was a bug I discovered. Otherwise, this suggestion totally worked and I thank you for it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.