Bootstrap to display dynamic checkboxes in 3 columns

房东的猫 提交于 2020-01-16 09:03:34

问题


I am using Bootstrap 3 in my Angular 4 app to create a form. In the form, I need to render checkboxes on the basis of data returned from a web service call. So, I need to iterate over the data and render a checkbox for every value received.

The display for these checkboxes is bit messed up as in the screenshot below -

Is there a way to display these checkboxes in 3 columns. Something like this (looks much cleaner)

My current code is as below

<div class="container-fluid">
  <div class="row">
    <div class="col-xs-12">
      <form [formGroup]="myForm">

        <div class="row">
          ...
        </div>
        <div class="row">
          ...
        </div>
        <div class="row">
          <div class="col-xs-8">
            <div class="form-group">
              <label for="options" class="col-xs-4">Options</label>
              <div class="checkbox"  formGroupName="options" *ngFor="let option of options">
               <div class="col-xs-4">
                <input id="{{option}}" formControlName="{{option}}" type="checkbox" 
                         [checked]=false> {{option}}
               </div>
              </div>
            </div>
          </div>
          <div class="col-xs-4">
            <div class="form-group">
              <label for="name" class="col-xs-4">Name</label>
              <div class="col-xs-8">
                <input type="text" id="name" formControlName="name" class="form-control input-sm">
              </div>
            </div>
          </div>
        </div>
        <div class="row">
        ...
        </div>
      </form>
    </div>
  </div>
</div>

I have come across some other threads on stack overflow that have examples with static checkboxes (Twitter Bootstrap - checkbox columns / columns within form). But I am not able to make out how to apply it to dynamically generated checkboxes.


回答1:


You can use linq-es2015 library this way:

import { asEnumerable } from 'linq-es2015';

//somewhere in component
var groups = asEnumerable(this.options)
    .Select((option, index) => { return { option, index }; })
    .GroupBy(
              x => Math.floor(x.index / 3),
              x => x.option,
              (key, options) => asEnumerable(options).ToArray()
            )
    .ToArray();

//template's fragment
<div class="col-xs-8">
  <div class="row">
    <div class="col-xs-4">
        <label for="options" class="col-xs-4">Options</label>
    </div>            
    <div class="col-xs-8">
      <div class="row" *ngFor="let group of groups">
        <div class="col-xs-4" *ngFor="let option of group">
          <input id="{{option}}" formControlName="{{option}}" type="checkbox" [checked]=false/> {{option}}
        </div>      
      </div>              
    </div>            
  </div>
</div>


来源:https://stackoverflow.com/questions/44559332/bootstrap-to-display-dynamic-checkboxes-in-3-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!