Yii gridview使用外部变量的值 [英] Yii gridview use outside variable in value

查看:89
本文介绍了Yii gridview使用外部变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  getCaterogies(){
返回数组('1'=>'脾气好','2'=>'有趣','3'=>'有远见者',...);





$ b我将索引存储在数据库中,并使用数组的值显示值对应于..

  $ categories = $ teacher->类别; 
$ category = $ categories [$ teacher-> category];

我这样做是因为一旦有人建议我不要将数据库中的字符串存储为状态,而是存储整数值,并将转换存储在数据库中或将其定义在ht模型中。字符串的问题在于它们在比较中更容易出现人为错误。也许是因为区分大小写。



现在我面对的问题是,在gridview中显示值时,我需要在值字段中写入2行,但它是一个表达式和外部变量也不需要。



我怎样才能使这个工作gridview?

$ row $ data params其中 $ row 包含行号(从零开始)和 $ data 包含数据模型for







$ p $ (
'dataProvider'=> $ dataProvider,$ b $'columns'=>数组(
数组(
'name'=>'..',
'value'=>函数($ data,$ row){
$ categories = $ teacher-> categories;
返回$ categories [$ data->类别];
},
),
),
));

如果你想从外部使用它,你可以依靠PHP的使用

  $ categories = $ teacher->类别; 
$ this-> widget('zii.widgets.grid.CGridView',array(
'dataProvider'=> $ dataProvider,
'columns'=> array(
array(
'name'=>'..',
'value'=>函数($ data,$ row)use($ categories){
return $ categories [$ data->类别];
},
),
),
));

我个人会推荐第二个,因为这样计算数组只会计算一次,并且会在所有情况下都可以使用。

I have a function in my Teacher model which returns categories array.

getCaterogies() {
   return array('1' => 'short tempered', '2' => 'funny', '3' => 'visionary', ...);
}

I am storing indexes in database and displaying values everywhere using the value of the array corresponding to that..

$categories = $teacher->categories;
$category = $categories[$teacher->category];

I am doing this because once somebody suggested to me not to store strings in a database which are statuses, instead store integer values, and either store the conversion in the database or define it in ht model. The problem with strings is that they are more prone to human errors in comparisons. Maybe because of case sensitiveness.

Now the problem I am facing is that while displaying values in gridview, I need to write the 2 lines in a value field, but it is an expression, and outside variables also it doesn't take.

How can I get this working for gridview?

解决方案

You can use anonymous function as value which can take $row, $data params where $row holds the row number (zero-based) and $data contains the data model for the row.

That way you can have it defined inside only.

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        array(
            'name'=>'..',
            'value'=>function($data,$row){
                $categories = $teacher->categories;
                return $categories[$data->category];
            },
        ),
    ),
));

And if you want to use it from outside, you can rely on PHP's use:

$categories = $teacher->categories;
$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        array(
            'name'=>'..',
            'value'=>function($data,$row) use ($categories){
                return $categories[$data->category];
            },
        ),
    ),
));

I would personally recommend second one, because that way the calculation of the array will be only once and will be used in all cases.

这篇关于Yii gridview使用外部变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆