2

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

4

1 回答 1

3

使用 Vue 方法使用数组“块”方法将项目分为 3 组。使用嵌套v-for来重复组,然后是每个组中的项目。这会将它们放在从上到下排序的 3 列中......

Vue2控制器:

  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;
    }
  },

标记:

<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>

演示: https ://www.codeply.com/go/ZaiUsUupsr


一种选择是将它们放在 3 列中,而无需重新迭代循环中的.row每 3 个项目。所有的复选框都可以放在一个row中,它们将在 3 列中从左到右排序。

演示: 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>
于 2018-03-27T10:02:41.037 回答