console
var app = new Vue({
el: '#app',
data: {
tempalteCurrentPage: "",
total:"100",
size: "10",
currentPage: "1",
limit:"5",
align: "center",
},
methods: {
toInteger(val) {
return parseInt(val, 10);
},
_firstFunction() {
this.tempalteCurrentPage = 1;
},
_prevFunction() {
this.tempalteCurrentPage = this.tempalteCurrentPage - 1;
},
_currentFunction(item) {
this.tempalteCurrentPage = item;
},
_nextFunction() {
this.tempalteCurrentPage = this.tempalteCurrentPage + 1;
},
_lastFunction() {
this.tempalteCurrentPage = this.nlyPgPages;
},
_sizeFunction() {
},
_tempalteArray() {
let nlyPgItemArrayAll = this.nlyPgItemArray;
nlyPgItemArrayAll.splice(0, 0, "‹");
nlyPgItemArrayAll.splice(0, 0, "‹‹");
nlyPgItemArrayAll.push("›");
nlyPgItemArrayAll.push("››");
return nlyPgItemArrayAll;
}
},
computed: {
nlyPgTotal: function() {
return isNaN(this.toInteger(this.total))
? 1
: this.toInteger(this.total) <= 0
? 1
: this.toInteger(this.total);
},
nlyPgSize: function() {
return isNaN(this.toInteger(this.size))
? 10
: this.toInteger(this.size) <= 0
? 10
: this.toInteger(this.size);
},
nlyPgCurrentPage: function() {
return this.toInteger(this.tempalteCurrentPage);
},
nlyPgLimit: function() {
return isNaN(this.toInteger(this.limit))
? 5
: this.toInteger(this.limit) <= 4
? 5
: this.toInteger(this.limit);
},
nlyPgPages: function() {
return Math.ceil(this.nlyPgTotal / this.nlyPgSize);
},
nlyPgItemArray: function() {
if (
this.nlyPgCurrentPage + 1 <= this.nlyPgLimit &&
this.nlyPgPages < this.nlyPgLimit
) {
const itemList = Array.from({ length: this.nlyPgPages }).map(
(v, k) => k + 1
);
return itemList;
} else if (
this.nlyPgCurrentPage + 1 <= this.nlyPgLimit &&
this.nlyPgPages == this.nlyPgLimit
) {
const itemList = Array.from({ length: this.nlyPgLimit }).map(
(v, k) => k + 1
);
return itemList;
} else if (
this.nlyPgCurrentPage + 2 <= this.nlyPgLimit &&
this.nlyPgPages > this.nlyPgLimit
) {
const itemList = Array.from({ length: this.nlyPgLimit - 1 }).map(
(v, k) => k + 1
);
itemList.push("···");
return itemList;
} else if (
this.nlyPgPages - this.nlyPgCurrentPage + 2 < this.nlyPgLimit &&
this.nlyPgPages > this.nlyPgLimit
) {
const itemList = Array.from({ length: this.nlyPgLimit - 1 }).map(
(v, k) => k + (this.nlyPgPages - this.nlyPgLimit + 2)
);
itemList.splice(0, 0, "···");
return itemList;
} else if (
this.nlyPgPages - this.nlyPgCurrentPage + 2 < this.nlyPgLimit &&
this.nlyPgPages == this.nlyPgLimit
) {
const itemList = Array.from({ length: this.nlyPgLimit - 1 }).map(
(v, k) => k + (this.nlyPgPages - this.nlyPgLimit + 2)
);
return itemList;
} else {
const itemList = Array.from({ length: this.nlyPgLimit - 2 }).map(
(v, k) =>
k + this.nlyPgCurrentPage - Math.ceil(this.nlyPgLimit / 2) + 2
);
itemList.splice(0, 0, "···");
itemList.push("···");
return itemList;
}
},
alignClass: function() {
const align = this.align;
if (align === "center") {
return "justify-content-center";
} else if (align === "end" || align === "right") {
return "justify-content-end";
}
return "";
},
tempalteArray: function() {
return this._tempalteArray();
}
},
created() {
this.tempalteCurrentPage = isNaN(this.toInteger(this.currentPage))
? 1
: this.toInteger(this.currentPage) <= 1
? 1
: this.toInteger(this.currentPage);
},
watch: {
tempalteCurrentPage: function() {
this.$emit("getCurrentPage", this.tempalteCurrentPage);
},
currentPage: function(newval, oldval) {
if (newval != oldval) {
this.tempalteCurrentPage = this.toInteger(this.currentPage);
}
},
nlyPgPages: function(newval, oldval) {
if (newval != oldval) {
this.tempalteCurrentPage =
this.tempalteCurrentPage > newval ? newval : this.tempalteCurrentPage;
}
},
nlyPgSize: function(newval, oldval) {
if (newval != oldval) {
this._sizeFunction();
}
}
}
})
<div id="app">
<form
class="form-inline justify-content-center"
style="
margin-bottom: 20px;
margin-top: 20px;"
>
<label for="total">total总条数</label>
<input type="number" class="form-control" id="total" v-model="total" />
<label for="size">size每页数量:</label>
<select class="form-control" id="size" v-model="size">
<option>5</option>
<option>10</option>
<option>15</option>
<option>20</option>
<option>25</option>
<option>30</option>
<option>35</option>
<option>40</option>
</select>
</form>
<ul :class="'pagination ' + alignClass">
<template v-for="(item, index) in tempalteArray">
<template v-if="item == '‹‹'">
<li
class="page-item disabled"
:key="index"
v-if="nlyPgCurrentPage == 1"
>
<a class="page-link"> {{ item }}</a>
</li>
<li
class="page-item nly-pagination-item"
:key="index"
v-else
@click="_firstFunction"
>
<a class="page-link"> {{ item }}</a>
</li>
</template>
<template v-else-if="item == '‹'">
<li
class="page-item disabled"
:key="index"
v-if="nlyPgCurrentPage == 1"
>
<a class="page-link"> {{ item }}</a>
</li>
<li
class="page-item nly-pagination-item"
:key="index"
v-else
@click="_prevFunction"
>
<a class="page-link"> {{ item }}</a>
</li>
</template>
<template v-else-if="item == '···'">
<li class="page-item disabled" :key="index">
<a class="page-link"> {{ item }}</a>
</li>
</template>
<template v-else-if="item == '››'">
<li
class="page-item disabled"
:key="index"
v-if="nlyPgCurrentPage == nlyPgPages"
>
<a class="page-link"> {{ item }}</a>
</li>
<li
class="page-item nly-pagination-item"
:key="index"
v-else
@click="_lastFunction"
>
<a class="page-link"> {{ item }}</a>
</li>
</template>
<template v-else-if="item == '›'">
<li
class="page-item disabled"
:key="index"
v-if="nlyPgCurrentPage == nlyPgPages"
>
<a class="page-link"> {{ item }}</a>
</li>
<li
class="page-item nly-pagination-item"
:key="index"
v-else
@click="_nextFunction"
>
<a class="page-link"> {{ item }}</a>
</li>
</template>
<template v-else-if="item == tempalteCurrentPage">
<li
class="page-item active nly-pagination-item"
:key="index"
@click="_currentFunction(item)"
>
<a class="page-link"> {{ item }}</a>
</li>
</template>
<template v-else>
<li
class="page-item nly-pagination-item"
:key="index"
@click="_currentFunction(item)"
>
<a class="page-link"> {{ item }}</a>
</li>
</template>
</template>
</ul>
</div>
.nly-pagination-item {
cursor: pointer;
color: #007bff;
}
.nly-pagination-item:hover {
cursor: pointer;
color: rgb(255, 255, 255);
}