vue中使用computed搜索解决分页后进行搜索出现页数错误的现象(getSearchInfo)
<template> <div> <!-- 基于el-table二次封装的一个table组件,功能包括:搜索、编辑、删除、分页 --> <el-table :data=" getSearchInfo.slice( (currpage - 1) * Data.pageSize, currpage * Data.pageSize ) " style="width: 100%" > <template v-for="(item, index) in Data.tableTitle"> <el-table-column :prop="item.prop" :label="item.label" align="center" :key="index" :formatter="formatData" > </el-table-column> </template> <el-table-column align="center" v-if="Data.showEditAndDelete"> <template slot="header" slot-scope="scope"> <el-input v-model="search" size="mini" placeholder="输入关键字搜索" /> </template> <template slot-scope="scope"> <el-button size="mini" @click="handleEdit(scope.$index, scope.row)" >编辑</el-button > <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)" >删除</el-button > </template> </el-table-column> </el-table> <el-pagination background :current-page.sync="currentPage" :page-size="Data.pageSize" layout="total, prev, pager, next, jumper" :total="getSearchInfo.length" @current-change="handleCurrentChange" > </el-pagination> </div> </template> <script> export default { props: ["Data"], data() { return { search: "", currpage: 1, currentPage: null }; }, computed: { getSearchInfo() { // 搜索 let search = this.search; if (search) { this.currpage = 1; this.currentPage = 1; return this.Data.tableData.filter(data => { return Object.keys(data).some(key => { return ( String(data[key]) .toLowerCase() .indexOf(search) > -1 ); }); }); } return this.Data.tableData; } }, methods: { formatData(row, column) { // 格式化数据 if (row["date"] == "2016-05-02") { return (row["date"] = "这个是测试动态格式化"); } else { return row[column.property]; } }, handleEdit(index, row) { // 编辑 console.log(index, row); }, handleDelete(index, row) { // 删除 console.log(index, row); }, handleCurrentChange(cpage) { // 点击页数 this.currpage = cpage; } }, mounted() {} }; </script> <style scoped></style>
父组件请看下一篇文章。