Yii把CGridView文本框换成下拉框的方法
程序员文章站
2023-11-17 23:40:10
本文实例讲述了yii把cgridview文本框换成下拉框的方法。分享给大家供大家参考。具体实现方法如下:
使用yii的朋友都知道yii中的cgridview默认显示提文本...
本文实例讲述了yii把cgridview文本框换成下拉框的方法。分享给大家供大家参考。具体实现方法如下:
使用yii的朋友都知道yii中的cgridview默认显示提文本框了,那么我们要怎么把它转换成下拉框呢?本文就此分析一下解决方法。
默认情况下cgridview会生成一个文本框,但是这并不友好,用户往往希望知道有什么可选的,特别是数据库里存的是数字id的时候,我们希望将数字转换为可读的文本,这时候只需要修改一下表头的"filter"属性就行了,比如:
复制代码 代码如下:
<?php
$this->widget('zii.widgets.grid.cgridview', array(
'id'=>'business-grid',
'dataprovider'=>$dataprovider,
'filter'=> business::model(),
'columns'=>array(
'business_id',
'name',
'package_id'=> array(
'name' => 'package_id',
'value' => '$data->package->package_title',
'filter'=> chtml::listdata(packages::model()->findall(array('order'=>'package_title')), 'package_id', 'package_title')
),
'user_id'=>array(
'name' => 'user_id',
'value' => '$data->user->name',
'filter'=> chtml::listdata(users::model()->findall(array('order'=>'firstname')), 'id', 'name')
),
'categories' => array(
'name' => 'categories',
'value' => '$data->returnallcategories(", ",false);',
'filter'=> chtml::listdata(categories::model()->findall(array('order'=>'category')), 'cat_id', 'category')
),
'keywords' => array(
'header' => 'keywords',
'value' => '$data->returnallkeywords(", ",false);',
),
'links'=>array(
'header'=>'manage',
'type'=>'raw',
'value'=>'chtml::link(chtml::image(yii::app()->request->baseurl."/images/admin/approve.png","approve"), array("approve","id"=>$data->business_id,"item"=>"business"),array("class"=>"approve-link","title"=>"approve business"))." ".
chtml::link(chtml::image(yii::app()->request->baseurl."/images/admin/reject.png","reject"), array("reject","id"=>$data->business_id,"item"=>"business"),array("class"=>"reject-link","title"=>"reject business"))',
),
),
));
$this->widget('zii.widgets.grid.cgridview', array(
'id'=>'business-grid',
'dataprovider'=>$dataprovider,
'filter'=> business::model(),
'columns'=>array(
'business_id',
'name',
'package_id'=> array(
'name' => 'package_id',
'value' => '$data->package->package_title',
'filter'=> chtml::listdata(packages::model()->findall(array('order'=>'package_title')), 'package_id', 'package_title')
),
'user_id'=>array(
'name' => 'user_id',
'value' => '$data->user->name',
'filter'=> chtml::listdata(users::model()->findall(array('order'=>'firstname')), 'id', 'name')
),
'categories' => array(
'name' => 'categories',
'value' => '$data->returnallcategories(", ",false);',
'filter'=> chtml::listdata(categories::model()->findall(array('order'=>'category')), 'cat_id', 'category')
),
'keywords' => array(
'header' => 'keywords',
'value' => '$data->returnallkeywords(", ",false);',
),
'links'=>array(
'header'=>'manage',
'type'=>'raw',
'value'=>'chtml::link(chtml::image(yii::app()->request->baseurl."/images/admin/approve.png","approve"), array("approve","id"=>$data->business_id,"item"=>"business"),array("class"=>"approve-link","title"=>"approve business"))." ".
chtml::link(chtml::image(yii::app()->request->baseurl."/images/admin/reject.png","reject"), array("reject","id"=>$data->business_id,"item"=>"business"),array("class"=>"reject-link","title"=>"reject business"))',
),
),
));
生成type下拉框可以这么写:
复制代码 代码如下:
'type'=>array(
'name' => 'type',
'filter'=>$model->gettypeoptions(),
),
'name' => 'type',
'filter'=>$model->gettypeoptions(),
),
'type'=>array(
'name' => 'type',
'filter'=>$model->gettypeoptions(),
),
希望本文所述对大家基于yii框架的php程序设计有所帮助。