yii2:显示标签而不是布尔复选框的值 [英] yii2: Show label instead of value for boolean checkbox

查看:116
本文介绍了yii2:显示标签而不是布尔复选框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个复选框输入作为Boolean类型,用于将值存储为盘算的-选中或未选中.选中将存储1,未选中将存储0.

I have created a check-box input as type Boolean for storing values as dishcharged - checked or unchecked. Checked will store 1 and unchecked will store 0.

现在,我想在网格视图和视图中将值1和0显示为.如何做到这一点.

Now I want to show the label as Yes or No for value 1 and 0 in grid-view and view. How can achieve that.

我的_form.php代码就像

my _form.php code is like

$form->field($model, 'discharged')->checkBox(['label' => 'Discharged', 
'uncheck' => '0', 'checked' => '1'])

我尝试过

[
'attribute'=>'discharged',
'value'=> ['checked'=>'Yes','unchecked=>'no']
],

,但语法不正确.

谢谢.

推荐答案

正如arogachev所说,您应该使用布尔格式器:

As arogachev said, you should use boolean formatter :

'discharged:boolean',

http://www.yiiframework.com/doc-2.0/guide-output-formatter .html

http://www.yiiframework.com/doc-2.0/yii-i18n-formatter.html#asBoolean()-detail

或者您可以在模型中添加getDischargedLabel()函数:

Or you could add a getDischargedLabel() function in your model :

public function getDischargedLabel()
{
    return $this->discharged ? 'Yes' : 'No';
}

在您的gridview中:

And in your gridview :

[
    'attribute'=>'discharged',
    'value'=> 'dischargedLabel',
],

这篇关于yii2:显示标签而不是布尔复选框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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