Yii gridview 在值中使用外部变量

Yii gridview use outside variable in value(Yii gridview 在值中使用外部变量)
本文介绍了Yii gridview 在值中使用外部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的教师模型中有一个函数返回类别数组.

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];

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

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.

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

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.

我怎样才能让它为 gridview 工作?

How can I get this working for gridview?

推荐答案

你可以使用匿名函数作为值,它可以接受 $row, $data params where $row 保存行号(从零开始),$data 包含行的数据模型.

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];
            },
        ),
    ),
));

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

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 在值中使用外部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)