表格的模糊搜索功能,前端也可以实现

今天学到了一个对于表格的模糊搜索,支持搜索全部列中文字的查询。

1. 首先是创建一个el-table

<el-table
      :data="tableData"
      height="600"
      style="width: 100%"
      :cell-style="rowClass"
      :header-cell-style="headClass"
    >
      <el-table-column prop="name" label="姓名" width="200" />
      <el-table-column label="学习进度" prop="progress">
        <template #default="scope">
          <el-progress
            type="line"
            :text-inside="true"
            :stroke-width="20"
            :percentage="scope.row.progress"
          ></el-progress>
        </template>
      </el-table-column>
      <el-table-column prop="grain" label="当前进行的活动" />
    </el-table>

2. 在table的上方添加一个输入框input

<el-input
      placeholder="请输入要搜索的内容"
      v-model="inputVal"
      @keyup.enter="search()"
      clearable
    >
    </el-input>

3. 声明

 data() {
    return {
      // 输入框的值
      inputVal: "",
      // 输入框搜索出来的值
      showTable: [],
      tableData: [],
    };

4. 监听输入框中的值,如果没有输入,则显示全部的数据

 watch: {
    //监听:如果为空,显示所有数据
    inputVal(item1) {
      if (item1 == "") {
        this.tableData = this.showTable;
      }
    },
  },
  //页面渲染时,显示所有的数据
  mounted() {
    this.showTable = this.tableData;
  },

5. 接下来是最重要的方法,搜索全部列的内容

     //搜索内容
    search() {
      const Search_List = [];
      let res1 = this.inputVal;
      const res = res1.replace(/\s/gi, "");
      let searchArr = this.showTable;
      searchArr.forEach((e) => {
        //绑定的table prop
        let name = e.name;
        let grain = e.grain;
        if (name.includes(res)) {
          if (Search_List.indexOf(e) == "-1") {
            Search_List.push(e);
          }
        }
        if (grain.includes(res)) {
          if (Search_List.indexOf(e) == "-1") {
            Search_List.push(e);
          }
        }
      });
      this.tableData = Search_List;
    },
  },

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值