yii2: Show label instead of value for boolean checkbox

前端 未结 3 1706
灰色年华
灰色年华 2021-01-04 20:03

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.

Now I wan

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-04 20:35

    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

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

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

    And in your gridview :

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

提交回复
热议问题