将属性添加到yii2 checkboxlist container div

时间:2016-09-09 05:32:52

标签: php yii2

我正在尝试使用yii2 checkboxlist的自定义模板我正在使用代码:

<?=
 $form->field($model, 'fruit_ids')->checkboxList($fruits, [
        'item' => function($index, $label, $name, $checked, $value) {
            return "<label class='col-md-4'><input type='checkbox' {$checked} name='{$name}' value='{$value}'>{$label}</label>";
        }
]);
?>

哪个输出:

<div id="testform-fruit_ids">
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="0">Apple</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="1">Banana</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="2">Orange</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="3">Pear</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="4">Pineapple</label>
</div>

但我想将属性 class="btn-checkbox options data-toggle="button" 添加到 <div id="testform-fruit_ids"> 包装器div元素,即我想输出如:

<div id="testform-fruit_ids" class="btn-checkbox options" data-toggle="button">
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="0">Apple</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="1">Banana</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="2">Orange</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="3">Pear</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="4">Pineapple</label>
</div>

请告诉我正确的方法。

3 个答案:

答案 0 :(得分:1)

这应该有效:

<?= $form->field($model, 'fruit_ids')->checkboxList($fruits, [
        'item' => function($index, $label, $name, $checked, $value) {
            return "<label class='col-md-4'><input type='checkbox' {$checked} name='{$name}' value='{$value}'>{$label}</label>";
        },
        'class' => 'btn-checkbox options',
        'data-toggle' => 'button'
   ]); ?>

答案 1 :(得分:1)

更新您的代码,如下所示

<?=
 $form->field($model, 'fruit_ids')->checkboxList($fruits, ['class'=>"btn-checkbox options",'data-toggle'=>"button",
        'item' => function($index, $label, $name, $checked, $value) {
            return "<label class='col-md-4'><input type='checkbox' {$checked} name='{$name}' value='{$value}'>{$label}</label>";
        }
]);
?>

将您的课程'class'=>"btn-checkbox options"'data-toggle'=>"button"添加到数组中。

答案 2 :(得分:-1)

您可以使用以下解决方案将属性<div class="form-group"> <label for="int">Clients Id <?php echo form_error('clients_id') ?></label> <select class="form-control select2 select2-hidden-accessible" style="width: 100%;"> <center><option selected="selected">--- Select ---</option></center> <?php foreach ($invoices as $row) { echo '<option class="form-control select2" value="'.$row['client_id'].'">'.$row['first_name'].'</option>'; } ?> </select> </div> <div class="form-group"> <label for="int">Suppliers Id <?php echo form_error('suppliers_id') ?></label> <select class="form-control select2 select2-hidden-accessible" se="width: 100%;"> <center><option selected="selected">--- Select ---</option></center> <?php foreach ($invoices as $row) { echo '<option class="form-control select2" value="'.$row['service_id'].'">'.$row['service_name'].'</option>' } ?> </select> </div> class="btn-checkbox options"添加到data-toggle="button"包装器<div id="testform-fruit_ids">元素:

div