简体   繁体   English

我如何在vue js中实现分页

[英]how can i achieve pagination in vue js

please i'm trying to paginate data but i'm getting this error in my console请我正在尝试对数据进行分页,但我在控制台中收到此错误

Property or method "$index" is not defined on the instance but referenced during render.

this is my html template which displays the data in which i'll want to paginate这是我的 html 模板,它显示了我要在其中分页的数据

<div id="heroes">
      <table class="table table-striped">
        <thead>
          <tr>
            <td>Hero Name</td>
            <td>Universe</td>
          </tr>
        </thead>
        <tbody>
          <tr
            v-for="hero in heroes "
            v-show="setPaginate($index)"
          >
            <td>{{ hero.name }}</td>
            <td>{{ hero.universe }}</td>
          </tr>
        </tbody>
      </table>
      <div id="pagination">
        <a
          href="#"
          class="btn btn-default"
          v-for="page_index in paginate_total"
          @click.prevent="updateCurrent(page_index + 1)"
        >
          {{ page_index + 1 }}
        </a>
      </div>
    </div>

this is my script tag这是我的脚本标签


<script>
export default {
 
  data() {
    return {

      current: 1,
      heroes: [
        { name: "Wolverine", universe: "Marvel" },
        { name: "Batman", universe: "DC" },
        { name: "Beast", universe: "Marvel" },
        { name: "Superman", universe: "DC" },
        { name: "Wonder Woman", universe: "DC" },
        { name: "Iceman", universe: "Marvel" },
        { name: "Black Panther", universe: "Marvel" },
        { name: "Beast Boy", universe: "DC" },
        { name: "Captain America", universe: "Marvel" },
        { name: "Hawkgirl", universe: "DC" },
        { name: "Cyclops", universe: "Marvel" },
        { name: "Green Lantern", universe: "DC" },
      ],
      paginate: 5,
      paginate_total: 0
    };
  },
  created() {
    this.paginate_total = this.heroes.length / this.paginate;
  },
  methods: {
    setPaginate: function(i) {
      console.log(i);
      if (this.current == 1) {
        return i < this.paginate;
      } else {
        return (
          i >= this.paginate * (this.current - 1) &&
          i < this.current * this.paginate
        );
      }
    },
    setStatus: function(status) {
      this.status_filter = status;
      this.$nextTick(function() {
        this.updatePaginate();
      });
    },
    updateCurrent: function(i) {
      this.current = i;
    },
    updatePaginate: function() {
      this.current = 1;
      this.paginate_total = Math.ceil(
        document.querySelectorAll("tbody tr").length / this.paginate
      );
    }
  }
};
</script>

i got the example from codepen which works fine when using it on codepen but i'm getting an error in my code,here's the link我从 codepen 获得了示例,在 codepen 上使用它时效果很好,但我的代码出现错误,这是链接

https://codepen.io/lorena-tapia/details/VwZzpRZ

please how can i go about this请问我怎么能 go 关于这个

The $index is not defined anywhere, you could define it in the v-for loop like: $index没有在任何地方定义,您可以在 v-for 循环中定义它,例如:

<tr v-for="(hero,index) in heroes " v-show="setPaginate(index)">
    <td>{{ hero.name }}</td>
    <td>{{ hero.universe }}</td>
</tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM