简体   繁体   English

如何在 yii2 中的 radioList() 中添加不同和常见的类

[英]How to add different and common classes in radioList() in yii2

This is the chtml of radio-button in one of my update form.这是我的更新表单之一中单选按钮的 chtml。

 <?= $form->field($model, 'customer_sel')->radioList(array('customer' => 'Customer', 'supplier' => 'Supplier', 'excludecustomer' => 'Exclude Customer', 'all' => 'All'))->label('Select Choice'); ?>

I need to add class names of common(same) and different classes to each radio input.我需要为每个无线电输入添加通用(相同)和不同类的类名。 That is it look like in html...那是它在html中的样子......

 <div class="form-group field-cashbankentry-customer_sel">
<label class="control-label" for="cashbankentry-customer_sel">Select Choice</label>
<input type="hidden" name="CashBankentry[customer_sel]" value=""><div id="cashbankentry-customer_sel"><label><input type="radio" class="inputbox inputindex_1" name="CashBankentry[customer_sel]" value="customer" checked> Custom</label>
<label><input type="radio" class="inputbox inputindex_2 name="CashBankentry[customer_sel]" value="supplier"> Supplier</label>
<label><input type="radio" class="inputbox inputindex_3 name="CashBankentry[customer_sel]" value="excludecustomer"> Exclude Customer</label>
<label><input type="radio" class="inputbox inputindex_4 name="CashBankentry[customer_sel]" value="all"> All</label></div>

i have classes to add [inputbox(common to all)and inputindex_1-4 to each radios] i'm syntactically confused to add array of classes to this.我有类要添加 [inputbox(common to all) 和 inputindex_1-4 到每个收音机] 我在语法上很困惑向这个添加类数组。 How to add these classes to this如何将这些类添加到此

<?= $form->field($model, 'customer_sel')->radioList(array('customer' => 'Customer', 'supplier' => 'Supplier', 'excludecustomer' => 'Exclude Customer', 'all' => 'All'))->label('Select Choice'); ?>

You need to configure the item option for the radioList options, you can see the Html::activeRadioList() for details.您需要为radioList选项配置item选项,详细信息可以查看Html::activeRadioList()

The below code should work for you.下面的代码应该适合你。

<?php echo $form->field($model, 'customer_sel')->radioList(
        [
            'customer' => 'Customer',
            'supplier' => 'Supplier',
            'excludecustomer' => 'Exclude Customer',
            'all' => 'All',
        ],
        [
            'item' => function ($index, $label, $name, $checked, $value) {

                $return = '<label>';
                $return .= '<input class="inputbox inputindex_' . (++$index) . '" type="radio" name="' . $name . '" value="' . $value . '" ' . ($checked ? "checked" : "") . '>';
                $return .= ucwords($label);
                $return .= '</label>';
                return $return;
            },

        ]
    )->label('Select Choice');
?>

Note: to make the first option selected by default, you should set the attribute with the value inside the action.注意:要默认选中第一个选项,您应该使用操作内的值设置属性。 like喜欢

$model = new CashBankentry();
$model->customer_sel='customer';

I think it will be easier if you generate the markup without using the $form->field() helper.我认为如果不使用$form->field()助手生成标记会更容易。 Check its implementation and you will see the different parts you need to code by separate.检查其实现,您将看到需要单独编码的不同部分。

A call to $form->field() basically calls to Html::activeLabel() Html::activeInput() and Html::activeError()调用$form->field()基本上调用Html::activeLabel() Html::activeInput()Html::activeError()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM