php
  • yii2
  • 2016-09-09 56 views 0 likes 
    0

    我试图使用自定义模板yii2的CheckBoxList我使用的代码:添加属性yii2 CheckBoxList的div容器

    <?= 
    $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 optionsdata-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> 
    

    请告诉我正确的方法来做到这一点。

    回答

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

    谢谢。我可以添加多少这样的属性以及Yii如何确定项目内容应放置在div和class内部应放置在div开始标记内。 – Alex

    +0

    只限制PHP和HTML的限制,所以它不应该是一个具有合理数量属性的问题。至于Yii看看从[checkboxList()方法](https://github.com/yiisoft/yii2/blob/2.0.9/framework/widgets/ActiveField.php#L622)开始的代码, – Bizley

    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"到数组。

    相关问题