2016-02-24 22 views
17

Ho codice come questo:
VueJs come effettuare l'impaginazione con il limitatore e la portata ...?

var demoList = new Vue({ 
 
    el: '#demoList', 
 
    data: { 
 
    items: [{ 
 
     "id": 1, 
 
     "name": "Tom" 
 
    }, { 
 
     "id": 2, 
 
     "name": "Kate" 
 
    }, { 
 
     "id": 3, 
 
     "name": "Jack" 
 
    }, { 
 
     "id": 4, 
 
     "name": "Jill" 
 
    }, { 
 
     "id": 5, 
 
     "name": "aa" 
 
    }, { 
 
     "id": 6, 
 
     "name": "bb" 
 
    }, { 
 
     "id": 7, 
 
     "name": "cc" 
 
    }, { 
 
     "id": 8, 
 
     "name": "dd" 
 
    }, { 
 
     "id": 1, 
 
     "name": "Tom" 
 
    }, { 
 
     "id": 2, 
 
     "name": "Kate" 
 
    }, { 
 
     "id": 3, 
 
     "name": "Jack" 
 
    }, { 
 
     "id": 4, 
 
     "name": "Jill" 
 
    }, { 
 
     "id": 5, 
 
     "name": "aa" 
 
    }, { 
 
     "id": 6, 
 
     "name": "bb" 
 
    }, { 
 
     "id": 7, 
 
     "name": "cc" 
 
    }, { 
 
     "id": 8, 
 
     "name": "dd" 
 
    }, ], 
 
    loading: false, 
 
    order: 1, 
 
    searchText: null, 
 
    ccn: null, 
 
    currentPage: 0, 
 
    itemsPerPage: 2, 
 
    resultCount: 0 
 
    }, 
 
    computed: { 
 
    totalPages: function() { 
 
     console.log(Math.ceil(this.resultCount/this.itemsPerPage) + "totalPages"); 
 
     return Math.ceil(this.resultCount/this.itemsPerPage); 
 

 
    } 
 
    }, 
 
    methods: { 
 
    setPage: function(pageNumber) { 
 
     this.currentPage = pageNumber; 
 
     console.log(pageNumber); 
 
    } 
 
    }, 
 
    filters: { 
 
    paginate: function(list) { 
 
     this.resultCount = this.items.length; 
 
     console.log(this.resultCount + " Result count"); 
 
     console.log(this.currentPage + " current page"); 
 
     console.log(this.itemsPerPage + " items per page"); 
 
     console.log(this.totalPages + " Total pages 2"); 
 
     if (this.currentPage >= this.totalPages) { 
 
     this.currentPage = Math.max(0, this.totalPages - 1); 
 
     } 
 
     var index = this.currentPage * this.itemsPerPage; 
 
     console.log(index + " index"); 
 
     console.log(this.items.slice(index, index + this.itemsPerPage)); 
 
     return this.items.slice(index, index + this.itemsPerPage); 
 
    } 
 
    } 
 
});
a { 
 
    color: #999; 
 
} 
 
.current { 
 
    color: red; 
 
} 
 
ul { 
 
    padding: 0; 
 
    list-style-type: none; 
 
} 
 
li { 
 
    display: inline; 
 
    margin: 5px 5px; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.16/vue.js"></script> 
 
<div id="demoList"> 
 
    <div class="containerTable"> 
 
    <table class="table"> 
 
     <thead> 
 
     <tr class="header"> 
 
      <th> 
 
      <div><a @click="sortvia('provider_name')">Provider</a> 
 
      </div> 
 
      </th> 
 
     </tr> 
 
     </thead> 
 
     <tbody> 
 
     <tr v-for="item in items | paginate"> 
 
      <td>{{item.name}}</td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 
    </div> 
 
    <ul> 
 
    <li v-for="pageNumber in totalPages"> 
 
     <a href="#" @click="setPage(pageNumber)">{{ pageNumber+1 }}</a> 
 
    </li> 
 
    </ul> 
 
</div>

Im bloccato cercando di creare un cercapersone con vuejs, così mi è stato da meravigliarsi se qualcuno può nominare un esempio di come fare un pager come questo se è possibile "1-2-3-4-5 ... 55" ??, grazie ancora per qualsiasi aiuto.

+0

Potete spiegare ulteriormente quale è l'output desiderato? –

+0

Voglio in numero di impaginazione come questo "1-2-3-4-5 ... last" non tutti 1-2-3-4-5-6 -7-8 – b4dQuetions

+0

si potrebbe fare un ngif = "$ index - pagina <5 " –

risposta

23

Guarda questo codice:

https://jsfiddle.net/os7hp1cy/48/

html:

<ul> 
    <li v-for="pageNumber in totalPages" 
     v-if="Math.abs(pageNumber - currentPage) < 3 || pageNumber == totalPages - 1 || pageNumber == 0"> 
    <a href="#" @click="setPage(pageNumber)" 
     :class="{current: currentPage === pageNumber, last: (pageNumber == totalPages - 1 && Math.abs(pageNumber - currentPage) > 3), first:(pageNumber == 0 && Math.abs(pageNumber - currentPage) > 3)}"> 
     {{ pageNumber+1 }}</a> 
    </li> 
</ul> 

css:

a.first::after { 
    content:'...' 
} 

a.last::before { 
    content:'...' 
} 

Fondamentalmente, mostra solo impaginazione che è dentro 2 pagine della corrente pagina. Quindi mostrerà anche la pagina 1 e l'ultima pagina e inserirà "..." prima o dopo il numero con CSS. Quindi, se siete a pagina 10, mostrerà:

1... 8 9 10 11 12 ...21

+0

Ho provato questo codice nel mio esempio e per qualche motivo mi dà un avvertimento: [Vue warn]: La proprietà o il metodo" paginare "non è definito sull'istanza ma fatto riferimento durante il rendering. Assicurati di dichiarare le proprietà dei dati reattivi nell'opzione dati. (trovato nell'istanza di root) Sto avendo la funzione di paginazione nei filtri, che sono dentro i metodi. – linous

1

ho biforcato @ codice di Jeff e fare una versione funzionante di Vue 2 a causa di questa migrazione filtro https://vuejs.org/v2/guide/migration.html#Filters-Outside-Text-Interpolations-removed.

metodo paginate viene spostato calcolato:

paginate: function() { 
     if (!this.users || this.users.length != this.users.length) { 
      return 
     } 
     this.resultCount = this.users.length 
     if (this.currentPage >= this.totalPages) { 
      this.currentPage = this.totalPages 
     } 
     var index = this.currentPage * this.itemsPerPage - this.itemsPerPage 
     return this.users.slice(index, index + this.itemsPerPage) 
    } 

ATTENZIONE: Non ho applicato il filtro di testo, solo l'impaginazione prima.

https://jsfiddle.net/c1ghkw2p/