Vue项目中线上搜索接口不可用,提供搜索和搜索后可以分页功能的解决方法。

项目技术:html5、css3、javascript、vue、less、node.js

1、结构使用Element ui框架

//搜索结构
 <el-input
        placeholder="请输入搜索内容## "
        clearable
        @clear="clearKey()"
        v-model="keyword"
        class="input-with-select"
        style="width: 400px"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="search()"
        ></el-button>
      </el-input>
 // 分页结构
  <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[1, 2, 3, 4]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

2、methods中定义方法

clearkey() {
      this.keyword = "";
      this.isSearch = false;
      this.load();
    },
    search() {
      this.isSearch = true;
      // 清空原数据列表数据
      this.arr = [];
      // 重新请求全部的列表数据
      this.$axios
        .get("orders", {
          params: {
            pagenum: 1,
            pagesize: 100,
          },
        })
        .then((res) => {
          // 定义数组,可用使用数组filter,indexof方法,模糊查找符合条件的数据
          let arr = res.data.data.goods.filter((item) => {
            return item.order_number.indexOf(this.keyword) >= 0;
          });
          // 分页时数据只有一页
          if (arr.length <= this.pagesize) {
            this.arr = arr
          } else {
            // 多页时可用使用数组的slice方法使用公式:从(pagenum-1)*pagesize开始,到pagenum*pagesize前结束
            this.arr = arr.slice(
              (this.pagenum - 1 )* this.pagesize,
               this.pagenum * this.pagesize
            );
          }
          // 分页总条数
          this.total = arr.length;
        });
    }

3、定义分页方法

 handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pagesize = val;
      // data中定义isSearch变量为false,
      if (this.isSearch) {
        // 通过变量控制页面渲染搜索内容还是全部内容
        this.search();
      } else {
        this.load();
      }
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagenum = val;
      if (this.isSearch) {
        this.search();
      } else {
        this.load();
      }
    },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MZiXiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值