【问题标题】:Pagination in vue.js frameworkvue.js 框架中的分页
【发布时间】:2021-01-24 21:53:36
【问题描述】:

视图页面中 vue.js 框架中的分页错误,分页栏工作正常,但我在同一页面上有 50 条记录。分页栏是 17 => 50/3 。帖子显示在 div 部分而不是在表格中。

<template>
  <div class="blog">
    <h2>{{ pageDescreption }}</h2>
    <hr />

    <div class="alert alert-success" role="alert" v-text="alertTitel"></div>

    <div class="row">
      <div class="col-md-8">
        <div class="posts-area">
          <PostList
            id="PostList"
            v-for="post in posts"
            :key="post.id"
            :post="post"
            :current-page="currentPage"
            :per-page="perPage"
          />
          <div class="overflow-auto">
            <b-pagination
              v-model="currentPage"
              :total-rows="rows"
              :per-page="perPage"
              aria-controls="PostList"
            ></b-pagination>

            <p class="mt-3">Current Page: {{ currentPage }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import PostList from "@/components/Blogs/PostList.vue";
import PostJson from "../Gatewaye/post.json";

export default {
  data: function() {
    return {
      pageName: "blog",
      pageDescreption: "This is an Blog page",
      alertTitel: "List of all Posts",
      posts: PostJson, // array of posts [50 records]
      perPage: 3,
      currentPage: 1
    };
  },
  name: "Blog",
  components: {
    PostList
  },
  computed: {
    rows() {
      return this.posts.length;
    }
  }
};
</script> 

而在组件文件Blog.vue中是:

<template>
  <div class="PostList">
    <div class="post-container text-left">
      <span class="post-views badge badge-primary" title="Views">{{
        post.views
      }}</span>
      <h3 class="post-title" title="Title">{{ post.title }}</h3>
      <span class="post-date" title="Date">{{ post.date }}</span>
      <p class="post-body">
        {{ post.contant }}
      </p>
      <div class="row">
        <div class="col-sm-6">
          <span class="post-author" title="Auther">{{ post.auther }}</span>
        </div>
        <div class="col-sm-6 text-right">
          <span class="post-category" title="Category">{{
            post.category
          }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["post"],
  name: "PostList"
};
</script>

分页栏工作正常,但我在同一页面上有 50 条记录...谢谢

【问题讨论】:

    标签: vue.js bootstrap-4 pagination


    【解决方案1】:

    使用计算属性对父组件中的post 数组进行切片:

    computed: {
      paginatedposts() {
        return this.posts.slice(this.perPage*(this.currentPage-1), (this.perPage*(this.currentPage-1))+this.perPage);
      }
    }
    

    现在你只需要绑定这个计算属性:

    <PostList
     id="PostList"
     v-for="post in paginatedposts"
     :key="post.id"
     :post="post"
    />
    

    【讨论】:

      猜你喜欢
      • 2017-06-17
      • 2012-02-06
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 2018-05-17
      • 2019-08-11
      • 2015-11-06
      • 1970-01-01
      相关资源
      最近更新 更多