douyou1857 2013-09-12 05:35
浏览 36

如何在CGridview下拉列表中使用文本而不是值

I'm new to Yii. I have a gridview with search boxes. One of these search boxes has to be replaced by dropdown list. Based on the dropdownlist value the value in the gridview cells should be replaced.

Suppose, I use (0-> No Activity, 1->Pending, 2->Approved, 3-> Rejected). I need to replace the values 0 with No Activity, 1 with Pending, 2 with Approved, 3 with Rejected respectively both in Gridview cell and dropdownlist.

What should I mention in the value of the array

array(
            'name' => 'Test',
            'value' =>                  
            'filter' => array('0' => 'No Activity', '1' => 'Pending','2' => 'Approved', '3' => 'Rejected',''=>'All'),                        
            'htmlOptions' => array('style' => 'width: 500px;'),
            ),

I'm using PHP 5.2 and Yii version 1.13

I edited the below code , and this works fine for me

EDIT :

function getTestType($test)
{
    $testTypesList = array('0'=> 'No Activity', '1'=>'Pending', '2'=>'Approved', '3'=> 'Rejected',''=>'All');
    if(is_null($test)){
        return 'All';
    }else{
        return $test=$testTypesList[$test];
    }

}

and in view

array(
            'name' => 'Test',
            'value' =>'getTestType($data->textfield)'                  
            'filter' => array('0' => 'No Activity', '1' => 'Pending','2' => 'Approved', '3' => 'Rejected',''=>'All'),                        
            'htmlOptions' => array('style' => 'width: 500px;'),
            ),
  • 写回答

2条回答 默认 最新

  • duanfu7840 2013-09-12 05:45
    关注

    Controller:

     $arrayList = array(0-> No Activity, 1->Pending, 2->Approved, 3-> Rejected,''=>'All');
    

    Send this to view file:

    $this->render('index',array(
            'model'=>$model,
            'arrayList'=>$arrayList, // Send array to view file
        ));
    

    , and in view file:

    array(           
                    'name'=>'Test',
                    'value'=>function ($data, $row) use ($arrayList){ return $data->name_field? $arrayList[$data->name_field] : 'All' ; },
            ),
    

    Edited: PHP 5.2

    View:

        array(
             'name'=>'Test',
             'header'=>'Test',
             'type'=>'raw',
             'value'=>'Test::getTestType($data->test)'
       ),
    

    TEST Model:

    public static $testTypesList = array('0'=> 'No Activity', '1'=>'Pending', '2'=>'Approved', '3'=> 'Rejected',''=>'All');
    public function getTestType($test)
    {
        if(is_null($test)){
            return 'All';
        }else{
            return self::$testTypesList[$test];
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能