如何在yii2的radioList中添加不同的通用类

问题描述

这是我的一种更新形式中的单选按钮的chtml。

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

我需要为每个无线电输入添加通用(相同)类和不同类的类名。 就像在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>

我有添加[inputBox(对所有公共)和inputindex_1-4到每个无线电的类] 我在语法上困惑于为此添加类数组。 如何将这些类添加到此

<?= $form->field($model,'all' => 'All'))->label('Select Choice'); ?>

解决方法

您需要为item选项配置radioList选项,有关详细信息,请参见Html::activeRadioList()

下面的代码应该对您有用。

<?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');
?>

注:要默认选择第一个选项,应使用操作内的值设置属性。喜欢

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

我认为,如果不使用$form->field()帮助程序来生成标记,将会更容易。检查其实现,您将看到需要单独编码的不同部分。

Html::activeLabel()的呼叫基本上是对Html::activeInput() Html::activeError()class License(models.Model): PACKAGE_CHOISES = ( ('Production','Production'),('Evaluation','Evaluation'),) LICENSE_CHOISES = ( ('js','Javascript_sdk'),('ios','Ios_sdk'),('android','Android_sdk'),) client = models.ForeignKey('Client',on_delete=models.CASCADE) package = models.CharField(max_length=15,choices=PACKAGE_CHOISES,blank=True,null=True) license_type = models.CharField(max_length=15,choices=LICENSE_CHOISES,null=True) created_datetime = models.DateTimeField(auto_now=True) expiration_datetime = models.DateTimeField(default=get_default_license_expiration) 的呼叫