php
  • yii2
  • 2016-09-09 60 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"到數組。

    相關問題