87,993
社区成员
发帖
与我相关
我的任务
分享
<div id="td">
<br><br>
<table id="tb">
<thead>
<tr>
<th>序号</th>
<th>账号</th>
<th>密码</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(person,index) in currentPageData">
<td>{{(currentPage-1)*pageSize+index+1}}</td>
<td>{{person.username}}</td>
<td>{{person.password}}</td>
<td><input value="删除" type="submit" @click="deleteUser(person.id)"></td>
</tr>
</tbody>
</table>
<br><br>
<footer>
<input value="首页" type="submit" @click="firstPage()">
<input value="上一页" type="submit" @click="prevPage()">
<span>{{currentPage}}/{{totalPage}}</span>
<input value="下一页" type="submit" @click="nextPage()">
<input value="尾页" type="submit" @click="endPage()">
<input type="number" v-model="skipPage" id="skippage">
<input value="跳转" type="submit" @click="btnPage()">
</footer>
</div>
new Vue({
el:"#td",
data:{
skipPage:"",//获取要跳转的页数
totalSize:[],//保存所有数据的数组
currentPage:1,//当前页
totalPage:1,//总的页数
pageSize:2,//每页显示的数据大小
currentPageData:[]//当前页的数据(绑定表格的数组)
},
mounted:function(){
this.btn();
},
methods:{
btn(){
axios.post("table").then(response=>{
this.totalSize = response.data;
// 计算一共有几页
this.totalPage = Math.ceil(this.totalSize.length / this.pageSize);
// 计算得0时设置为1
this.totalPage = this.totalPage == 0 ? 1 : this.totalPage;
this.getCurrentPageData();
}).catch(error=>{
alert("获取失败");
alert(error);
})
},
getCurrentPageData() {
let begin = (this.currentPage - 1) * this.pageSize;
let end = this.currentPage * this.pageSize;
this.currentPageData = this.totalSize.slice(
begin,
end
);
},
//删除
deleteUser(uid) {
$.post("delete_user",{"id":uid},function(data){
if(data=="success"){
alert("删除成功!");
var cPage=this.currentPage;//保存当前页
this.btn();//重新获取数据的ajax
this.currentPage=cPage;//把保存的当前页赋值给新的当前页
this.getCurrentPageData();//把要展示的数据存进绑定dom的数组
}
else {
alert("删除失败!");
}
})
},
}
})
