javascript - Bootstrap Vue perPage 用于自定义列或行

标签 javascript vue.js vuejs2 vue-component bootstrap-vue

我有一个简单的 vue 项目 codesandboxbootstrap-vue .

有几列带有卡片和这些列的分页:

<template>

  <b-container>
    <b-row 
      :current-page="currentPage"
      :per-page="perPage">   

      <b-col cols="12" sm="4" class="my-1"
        v-for="item in items" 
          :key="item.id">

        <b-card           
          :bg-variant="item.variant"
          text-variant="white"
          header="item.title"
          class="text-center"
          >
          <p class="card-text">
            {{item.body}}
          </p>
        </b-card>  

      </b-col>

    </b-row>

    <b-row>
      <b-col md="6" class="my-1">
        <b-pagination :total-rows="totalRows" :per-page="perPage" v-model="currentPage" class="my-0" />
      </b-col>
    </b-row>
  </b-container>

</template>

所以我正在尝试为列实现一个工作分页,类似于 bootstrap table 的分页。 .

页面应显示 2 列,其中 card = perPage: 2

问题:如何为自定义列或行设置 bootstrap-vue perPage

最佳答案

如果您想每页获得 2 张卡片,我建议您使用我的工作 solution通过使用计算属性,我创建了一个名为 currentPageItems

的属性

computed: {
   ...
   
   currentPageItems() {
          let lengthAll =this.items.length;
      this.nbPages = 0;
        for (let i = 0; i < lengthAll; i = i + this.perPage) {
        this.paginated_items[this.nbPages] = this.items.slice(
          i,
          i + this.perPage
        );
        this.nbPages++;
      }
       
        return this.paginated_items[this.currentPage-1];
  
    }
    
    ...
  }

我在 data() 中添加了这些属性:

paginated_items: {},
currentPageIndex:0,
nbPages:0

在模板部分将 items 更改为 currentPageItems

...
 <b-col cols="12" sm="4" class="my-1" v-for="item in currentPageItems" :key="item.id">
...

完整代码片段:

<template>

  <b-container>
    <b-row>   
    
      <b-col cols="12" sm="4" class="my-1"
        :key="index"
        v-for="(item, index) in currentPageItems" 
          >
        <b-card         
          :bg-variant="item.variant"
          text-variant="white"
          :header="item.title"
          class="text-center"
          >
          <p class="card-text">
            {{item.body}}
          </p>
        </b-card>  

      </b-col>

    </b-row>

    <b-row>
      <b-col md="6" class="my-1">
        <b-pagination :total-rows="totalRows" :per-page="perPage" v-model="currentPage" class="my-0" />
      </b-col>
    </b-row>
  </b-container>

</template>

<script>
const items = [
  {
    title: "Primary",
    body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    variant: "primary"
  },
  {
    title: "Secondary",
    body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    variant: "secondary"
  },
  {
    title: "Success",
    body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    variant: "success"
  },
  {
    title: "Info",
    body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    variant: "info"
  },
  {
    title: "Warning",
    body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    variant: "warning"
  },
  {
    title: "Danger",
    body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    variant: "danger"
  }
];

export default {
  name: "MyBootstrapGrid",
  data() {
    return {
      items: items,
      currentPage: 1,
      perPage: 2,
      totalRows: items.length,
      paginated_items: {},
      currentPageIndex:0,
      nbPages:0
    };
  },
  computed: {
    pageCount() {
      let l = this.totalRows,
        s = this.perPage;
      return Math.floor(l / s);
    },
      currentPageItems() {
          let lengthAll =this.items.length;
      this.nbPages = 0;
        for (let i = 0; i < lengthAll; i = i + this.perPage) {
        this.paginated_items[this.nbPages] = this.items.slice(
          i,
          i + this.perPage
        );
        this.nbPages++;
      }
       
        return this.paginated_items[this.currentPage-1];
  
    }
  },
  methods: {}
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

关于javascript - Bootstrap Vue perPage 用于自定义列或行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52135382/

相关文章:

vue.js - 如何在子事件发出的事件中捕获父组件事件

javascript - 在导航栏组件中组合 bootstrap-vue 和 vue-router

javascript - 使用 Jquery 的 AJAX 无法跨 JSP 页面工作

vue.js - 如何使用 Vue/Vuex 正确排序调用

javascript - mootools 比 jquery 有什么优势?

javascript - GET 后 Vue.js 组件数据未更新

vue.js - 如何在 vuex nuxt 中获取嵌套 getter

javascript - Vue : props doesn't get assigned automatically; when assigned manually - Avoid mutating a prop directly - error

javascript - 使用 Javascript 将 <li> 类设置为事件状态,不起作用?

javascript - 在 JavaScript 中导入自定义元素