Javascript vue.js table pagination, Ajax asynchronous Load Data _ basics

Source: Internet
Author: User

Page General and table together, pagination link as part of the table, the paging link encapsulated into a separate component, and then as a child component embedded in the table component, this is more reasonable.

Effect:

Code:

1. Register a component

Js

 Vue.component (' pagination ', {Template: ' #paginationTpl ', replace:true, props:[')
          Cur ', ' all ', ' pagenum '], methods:{//page number Click event Btnclick:function (Index) {if (index!= this.cur) {
        This.cur = index;
      }}, watch:{"cur": function (val,oldval) {this. $dispatch (' Page-to ', Val);
        }, computed:{indexes:function () {var list = [];
        Calculates the left and right pages var mid = parseint (THIS.PAGENUM/2);//median value var left = Math.max (this.cur-mid,1);
        var right = Math.max (this.cur + this.pagenum-mid-1,this.pagenum);
          if (right > This.all) {right = This.all} While [left <= right] {List.push (left);
        Left + +;
      } return list;
      }, Showlast:function () {return this.cur!= this.all;
      }, Showfirst:function () {return this.cur!= 1;
}
    }
  }); 

Template:

<script type= "Text/template" id= "Paginationtpl" >
  <nav v-if= "all > 1" >
    <ul class= " Pagination ">
      <li v-if=" Showfirst "><a href=" javascript: "@click =" cur--">«</a></li>
      <li v-for= "index in Indexes": class= "{' active ': cur = = index}" >
        <a @click = "Btnclick (index)" href= " javascript: ">{{index}}</a>
      </li>
      <li v-if=" Showlast "><a @click =" cur++ "href=" javascript: ">»</a></li>
      <li><a> Total <i>{{all}}</i> page </a></li >
    </ul>
  </nav>
</script>

Html:

<div id= ' item_list ' > ...
  <pagination:cur= "1": all= "Pageall":p age-num= "ten" @page-to= "loadlist" ></pagination>
</div>

When clicking on the paging link, the Watch cur distributes the Page-to event through the child component, specifies that the parent component Loadlist method is used to handle the event through the @page-to= "loadlist" tab, the parent component receives the page value and then the AJAX loads the data. Returns the calculation and updates its own Pageall value based on the service side, because the subcomponent prop dynamically binds the Pageall object of the parent component through: all= "Pageall", so the subassembly is updated.

Attach an example of a simple table component:

var vm = new Vue ({
    el: "#item_list",
    data: {
      items: [],
      //paging parameter
      pageall:0,//Total number of pages, calculated according to the service-side return totals
      perpage:10//per page number
    },
    methods: {
      loadlist:function (page) {
        var = this;
        $.ajax ({
          URL: "/getlist",
          type: "Post",
          data:{"page":p Age, "Perpage": This.perpage},
          DataType: "JSON",
          error:function () {alert (' request list failed ')},
          success:function (res) {
            if (Res.status = 1) {
              That.items = res.data.list;
              That.perpage = Res.data.perPage;
              That.pageall = Math.ceil (res.data.total/that.perpage);//Calculate total number of pages
            }}
        );
      },
      //Initialize
      Init:function () {
        this.loadlist (1);
      }}}
  );
  Vm.init ();



Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.