Vue three columns of checkboxes in Bootstrap 4

六眼飞鱼酱① 提交于 2019-12-11 11:13:27

问题


Can anyone help me here with a 3 column layout via vue-js in bootstrap-4. I want to get my checkboxes displaying as 3 columns. The users are in order and I want the order going down the first column, then down the second and finally the third.

<div v-for="(user, index) in users">
  <div class="{'controls' : (index % (users.length/3)===0)}">
    <input type="checkbox" :id="'user_'+user.id" :value="user.id" class="form-check-input" v-model="form.checkedUsers">
    <label class="form-check-label" for="'user_'+userr.id">
      <img :src="user.photo_url" class="small-photo mx-2"> @{{ user.first_name }} @{{ user.last_name }}
    </label>
  </div>
</div>

Thanks


回答1:


Use a Vue method to group the items into 3 groups using an array "chunk" method. use nested v-for to repeat the groups, and then the items in each group. This will put them in 3 columns ordered top-to-bottom...

Vue2 controller:

  methods: {
    chunk: function(arr, size) {
      var newArr = [];
      for (var i=0; i<arr.length; i+=size) {
        newArr.push(arr.slice(i, i+size));
      }
      this.groupedItems  = newArr;
    }
  },

Markup:

<div class="container" id="app">
    <div class="row">
        <div class="col-sm-4 py-2" v-for='(g, gIndex) in groupedItems'>
            <form class="form-inline" v-for='(item, index) in g'>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox">
                    <label class="form-check-label">
                     {{ item.name }}
                    </label>
                </div>
            </form>
        </div>
    </div>
</div>

Demo: https://www.codeply.com/go/ZaiUsUupsr


An alternate option is to put them in 3 columns without re-iterating the .row every 3 items in the loop. All of the checkboxes can go in a single row, and they will be in 3 columns ordered left-to-right.

Demo: https://www.codeply.com/go/3gOvXFzaOw

<div class="container">
    <div class="row">
        <div v-for="item in items" class="col-sm-4 py-2">
            <form class="form-inline">
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" >
                    <label class="form-check-label">
                        {{ item.name }}
                    </label>
                </div>
            </form>
        </div>
    </div>
</div>


来源:https://stackoverflow.com/questions/49509467/vue-three-columns-of-checkboxes-in-bootstrap-4

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