Yii - 从模型查询数据以更改布局中的变量
Yii - Query data from model to change variable in layout
我是 Yii 的新手,我需要创建一个 table 来显示我公司的所有节点(服务器)。
我使用 Gii 生成了大部分数据并根据自己的喜好自定义了 CGridView。我很难尝试从每个节点获取布尔值以确定 "status_ON" 或 "status_OFF" 图像是否应显示在各自的行中。
我如何对其进行编码,以便根据 "isOnline" 来自数据库的结果 returns 0(离线)或 1(在线)而不需要 [=34] 来更改图像=] / ajax 或类似的?
请注意我被迫使用 Yii v1.1.8.r3324
希望我问的是对的!
模型:节点
<?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,
));
}
// ...
}
控制器: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,));
}
//...
}
查看: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 -->
举个小例子。希望对你有帮助。
$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时你可以使用Closure。示例:
//...
'value' => function ($data) use ($statusON_image, $statusOFF_image) {
//...
我是 Yii 的新手,我需要创建一个 table 来显示我公司的所有节点(服务器)。
我使用 Gii 生成了大部分数据并根据自己的喜好自定义了 CGridView。我很难尝试从每个节点获取布尔值以确定 "status_ON" 或 "status_OFF" 图像是否应显示在各自的行中。
我如何对其进行编码,以便根据 "isOnline" 来自数据库的结果 returns 0(离线)或 1(在线)而不需要 [=34] 来更改图像=] / ajax 或类似的?
请注意我被迫使用 Yii v1.1.8.r3324
希望我问的是对的!
模型:节点
<?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,
));
}
// ...
}
控制器: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,));
}
//...
}
查看: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 -->
举个小例子。希望对你有帮助。
$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时你可以使用Closure。示例:
//...
'value' => function ($data) use ($statusON_image, $statusOFF_image) {
//...