【问题标题】:vue js pagination with simple vuejs plugin使用简单的 vuejs 插件进行 vue js 分页
【发布时间】:2021-10-28 14:04:47
【问题描述】:

我正在尝试使用来自 API 的数据创建一个分页的 flexbox,但尽管我逐步阅读了设置,但还是遇到了困难。

这是我没有样式的代码:

<template>
  <div id="app">
    <paginate name="articles" :list="articles" class="paginate-list">
      <li v-for="item in paginated('articles')">
        {{ item }}
      </li>
    </paginate>
    <paginate-links for="items" :show-step-links="true"></paginate-links>
    <paginate-links for="items" :limit="2" :show-step-links="true"> 
    </paginate-links>
    <paginate-links for="items" :simple="{ next: 'Next »', prev: '« Back' }">
    </paginate-links>
  </div>
</template>
<script> 
  import axios from 'axios'; 
  import VuePaginate from 'vue-paginate' 

  export default {
    data() {
      return {
        items:[],
        paginate: [articles]
      }
    }, 
    created() {
      axios.get(https://zbeta2.mykuwaitnet.net/backend/en/api/v2/media-center/press-release/?page_size=61&type=5) 
        .then(response => { 
          this.items = response.data 
        }) 
    } 
  } 
</script>

【问题讨论】:

  • 即脚本部分:

标签: vue.js pagination


【解决方案1】:

第 1 步: 安装 npm install --save vue-paginate 第二步:在main.js中导入vue-paginate组件

import VuePaginate from "vue-paginate";
Vue.use(VuePaginate);

第 3 步: HTML 模板会像,

 <template>
  <div id="app">
    <paginate ref="paginator" class="flex-container" name="items" :list="items">
      <li
        v-for="(item, index) in paginated('items')"
        :key="index"
        class="flex-item">
        <h4>{{ item.pub_date }}, {{ item.title }}</h4>
        <img :src="item.image && item.image.file" />
        <div class="downloads">
          <span
            v-for="downloadable in item.downloadable.filter(
              (d) => !!d.document_en
            )"
            :key="downloadable.id">
            <a :href="downloadable.document_en.file">Download</a>
          </span>
        </div>
      </li>
    </paginate>
    <paginate-links
      for="items"
      :limit="2"
      :show-step-links="true"></paginate-links>
  </div>
</template>

第四步:你的组件scriptlike

<script>
  import axios from "axios";
  export default {
    data() {
      return {
        items: [],
        paginate: ["items"],
      };
    },
    created() {
      this.loadPressRelease();
    },
    methods: {
      loadPressRelease() {
        axios.get(`https://zbeta2.mykuwaitnet.net/backend/en/api/v2/media-center/press-release/?page_size=61&type=5`)
        .then((response) => {
          this.items = response.data.results;
        });
      }
    }
  };
</script>

第 5 步: CSS 风格

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
ul.flex-container {
  padding: 0;
  margin: 0;
  list-style-type: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  flex-direction: row wrap;
  flex-wrap: wrap;
  justify-content: space-around;
}
li img {
  display: initial;
  height: 100px;
}
.flex-item {
  background: tomato;
  width: calc(100% / 3.5);
  padding: 5px;
  height: auto;
  margin-top: 10px;
  color: white;
  font-weight: bold;
  text-align: center;
}
.downloads {
  margin-top: 10px;
}
ul.paginate-links.items li {
  display: inline-block;
  margin: 5px;
}
ul.paginate-links.items a {
  cursor: pointer;
}
ul.paginate-links.items li.active a {
  font-weight: bold;
}
ul.paginate-links.items li.next:before {
  content: " | ";
  margin-right: 13px;
  color: #ddd;
}
ul.paginate-links.items li.disabled a {
  color: #ccc;
  cursor: no-drop;
}
</style>

DEMO Link

【讨论】:

  • 非常感谢,Jebasuthan,这项工作,但我可以看到您没有使用分页模板,而是创建了自定义分页,对吗?
  • @BalazsKelemen 不,我使用了相同的pagination 组件,但是当我们单击名为rest api 的页码时我添加了。单击下一页时,您不需要rest api 电话吗?你只需要为this.items响应添加分页,而不是一次又一次地调用api吗?
  • @Jebasuthan,我很困惑,所以你不需要在脚本开头导入它吗?另外,我想我不需要休息 API
  • 还有,可以避免Loader和moment吗?
  • @BalazsKelemen 好的,知道了。让我也更新一下代码和演示链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多