zoukankan      html  css  js  c++  java
  • vue中使用computed搜索解决分页后进行搜索出现页数错误的现象(getSearchInfo)

    <template>
      <div>
        <!-- 基于el-table二次封装的一个table组件,功能包括:搜索、编辑、删除、分页 -->
        <el-table
          :data="
            getSearchInfo.slice(
              (currpage - 1) * Data.pageSize,
              currpage * Data.pageSize
            )
          "
          style=" 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>

     父组件请看下一篇文章。

  • 相关阅读:
    灰度发布
    rabbitmq应用场景
    redis设置cpu核数与内存
    使用word2010发布博客到博客园
    讲师
    UML-6.3-用例-详述示例
    UML-6.2-用例-用例模型/用例/场景关系
    UML-6.1-用例-示例
    数据库增量同步开源软件
    UML-5-进化式需求
  • 原文地址:https://www.cnblogs.com/lyt520/p/14721229.html
Copyright © 2011-2022 走看看