element表格 表头添加搜索框搜索功能

1. 效果

在这里插入图片描述

2. 代码

<el-table :data="accountData" >
   <template v-for="item in accountHead">
     <el-table-column
       v-if="item.id === 'object'"
       :key="item.id"
       :prop="item.id"
       :label="item.name"
     >
        <template #header>
          <el-popover placement="bottom" trigger="click">
            <div slot="reference">
              <span>标的 </span> 
              <i class="icon"></i> //标的后面的图标
            </div>
            <SearchObject />  //搜索框组件
          </el-popover>
        </template>
    </el-table-column>

    <!-- 其他列 -->
    <el-table-column
      v-else
      :key="item.id"
      :prop="item.id"
      :label="item.name">
    </el-table-column>
  </template>
</el-table>

SearchObject组件

<template>
  <div class="search-object">
    <div class="searchBox">
      <el-input
        class="inputArea"
        placeholder="请输入"
        v-model="inputContent"
        clearable>
      </el-input>
      <div class="buttonArea">
        <button class="search" @click="hiddenPopover">搜索</button>
        <button class="all" @click="hiddenPopover">全部</button>
      </div>
    </div>
  </div>
</template>
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将 element 表格表头自带筛选功能改成搜索框,可以使用 `el-input` 组件来实现。具体步骤如下: 1. 在表格外部添加一个 `el-input` 组件,作为搜索框。 2. 监听 `el-input` 组件的 `input` 事件,获取用户输入的搜索关键字。 3. 将搜索关键字传递给表格组件的 `filter-method` 属性,作为表格的过滤条件。 以下为示例代码: ```html <template> <div> <el-input v-model="keyword" placeholder="请输入搜索关键字" @input="handleSearch"></el-input> <el-table :data="tableData" :columns="tableColumns" :filter-method="filterTableData"> </el-table> </div> </template> <script> export default { data() { return { keyword: '', tableData: [ // 表格数据 ], tableColumns: [ { prop: 'name', label: '姓名' }, { prop: 'age', label: '年龄' }, // 表格列配置 ] } }, methods: { filterTableData(value, row, column) { const prop = column.property; return row[prop].indexOf(value) > -1; }, handleSearch() { this.$nextTick(() => { this.$refs.table.clearFilter(); this.$refs.table.filter(this.keyword, 'name'); }); } } } </script> ``` 在上面的示例代码中,我们添加了一个 `el-input` 组件,用于搜索表格数据。当用户在搜索框中输入关键字后,会触发 `handleSearch` 方法,将搜索关键字传递给表格组件的 `filter-method` 属性。在 `filter-method` 方法中,我们根据搜索关键字和表格数据进行匹配,返回符合条件的数据。 注意:在搜索时,我们只搜索表格中的 `name` 列。如果想搜索多列数据,可以在 `filter-method` 方法中进行扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值