vxe-table自定义表头搜索表格内容

该文章展示了如何在vxe-table中利用pug模板语法和插槽机制实现自定义表头的搜索框功能,将搜索组件封装成popSearch组件,并通过props传递数据和事件,实现表格数据的动态过滤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文档官网:
vxe-table v3https://vxetable.cn/v3/#/table/api

产品要求将表格内容搜索放在表格表头上,效果图如下:

 

 自定义表头,根据文档可以使用插槽slot="header"进行实现

 项目用的是pug模板语法,为了更好的维护代码,我将搜索框封装成了组件popSearch

 父组件代码如下:

<template lang='pug'>
.boxCommon
  .boxTable
    vxe-toolbar(ref="xToolbar" custom )
    vxe-table(
      :data="tableData",
      ref="tableRef"
      resizable
      border,
      class="vxe_class"
      :header-cell-style="{ 'text-align': 'center' }"
      :custom-config="{storage: true}"
      :tree-config="{children: 'children'}"
      id="id"
    )
      vxe-column(type="checkbox",fixed="left", align="center" width="60")
      vxe-column(type="seq", title="序号", fixed="left" width="60" align="center")
      vxe-column(
        field="route_abbreviation"
        title="简称"
        minWidth="90" 
        align="center"
        show-overflow
      )
        template(slot="header")
          popSearch(
            :type="0" 
            prop="route_abbreviation"
            text="简称" 
            :keywords="searchObj.route_abbreviation"  
            :propsData="propsData" 
            :url = "popUrl" 
            @clickSearch="clickSearch" 
          )
      vxe-column(title="操作", align="center"  fixed="right" width="120")
        template(slot-scope="scope")
    xt-pagination(
      :total="total",
      @change="changePage",
      :page="pageForm.page",
      :page-size="pageForm.pageSize"
    )
</template>

<script>
import { mapGetters } from "vuex";
import popSearch from "./pop-search.vue";
export default {
  name: "",
  props:{
  },
  components: { popSearch },
  data() {
    let vm = this;
    return {
      popUrl:'',
      searchObj: {},
      propsData:{},
      total: 0, 
      pageForm: {
        page: 1,
        pageSize: 20,
      },
    };
  },
  computed: {
  },
  watch: {
  },
  methods: {
    //获取表格数据
    getTableData(){

    },
    //接收子组件传的值,改变搜索对象中的值,更新表格数据
    clickSearch(key,value) {
      this.searchObj[key] = value
      this.getTableData();
    },
    //翻页
    changePage(row) {
    },
  },
  created() {
  },
  mounted() {
  },
};
</script>
<style scoped lang="scss">
</style>

popSearch组件代码如下:

<template lang="pug">
//- 为el-select添加filterable属性即可启用搜索功能
.pop
  el-popover( 
    placement="bottom" 
    width="250px" 
    trigger="click"  
    @show="popShow"  
    ref='popRef' 
  )
    el-select(
      v-model="newKeywords",
      filterable,
      clearable
      reserve-keyword,
      ref="refSelect"
      placeholder="请输入关键词",
      :loading="loading",
      :width="width",
      v-if="type == 0",
      @change="handleSearch()"
    )
      el-option( v-for="item in options"  :label="item.label"  :value="item.value"  )
    el-date-picker( 
      v-model="newKeywords" 
      v-if="type == 1"  
      type="daterange" 
      range-separator="至" 
      start-placeholder="开始日期" 
      end-placeholder="结束日期" 
    ) 
    el-date-picker(
      v-model="newKeywords",
      v-if="type == 2",
      type="datetimerange",
      range-separator="至",
      value-format="yyyy-MM-dd HH:mm:ss"
      start-placeholder="开始日期",
      end-placeholder="结束日期"
      @change="handleSearch()"
    ) 
    el-input( 
      v-model="newKeywords" 
      v-if="type == 3" 
      placeholder="请输入" 
      clearable  
      @change="handleSearch()" 
    )
    div(slot="reference")
      span {{ text }}
      i.el-icon-search 
</template>
<script >
export default {
  name: "Pops",
  props: {
    width: {
      type: String,
      default: "250px",
    },
    text: {
      type: String,
      default: "",
    },
    // type用于接受不同类型的弹框内容形式
    // 为了符合我的项目要求,定义了四种不同形式
    // type=:0:select选择器可搜索模式
    // type=1:DatePicker选择日期范围
    // type=2:DateTimePicker选择日期和时间范围
    // type=3:input基础输入框
    type: {
      type: Number,
      default: 0,
    },
    prop: {
      type: String,
      default: "",
    },
    url: {
      type: String,
      default: "",
    },
    keywords: {
      type: String,
      default: "",
    },
    propsData: {
      type: Object,
      default:()=>{
        return {}
      }
    },
  },
  data() {
    return {
      newKeywords: "",
      options: [],
      loading: false,
    };
  },
  created() {},
  watch: {
    keywords: {
      handler(v) {
        this.newKeywords = this.keywords
      },
      deep: true
    },
  },
  methods: {
    //弹出框显示时触发
    popShow() {
      this.filterMethod()
    },
    //获取选项的值
    async filterMethod(query) {
      this.loading = true;
      let params = {
        field:this.prop,
        keyword:this.newKeywords,
        ...this.propsData
      }
      //await this.$api[this.url](params) 封装的获取接口的方法,根据自己方式调整写法
      let res = await this.$api[this.url](params)
      this.options = res.result.map(item =>{
        return { value: item, label: item}
      })
      this.loading = false;
    },
    handleSearch() {
      if(this.type == 2 && this.newKeywords) {
        this.newKeywords = this.newKeywords.join('至')
      }
      //将子组件输入的值传给父组件
      this.$emit("clickSearch", this.prop, this.newKeywords);
      this.$refs.popRef.showPopper = false
    },
  },
};
</script>
<style scoped>
::v-deep(.el-select-dropdown, .el-popper) {
  margin: 0px !important;
}
</style>

props接收父组件传给子组件的值,上面的用于我的项目,大家可以根据自己的需求调整,emit传值给父组件,父组件通过clickSearch事件接收父组件的值,更新表格数据。

vxe-table是一个基于vue的表格组件,支持增删改查、虚拟滚动、懒加载、快捷菜单、数据校验、树形结构、打印导出、表单渲染、数据分页、模态窗口、自定义模板、灵活的配置项、丰富的扩展插件等... 设计理念: 面向现代浏览器,高效的简洁 API 设计 模块化表格、按需加载、插件化扩展 为单行编辑表格而设计,支持增删改查及更多扩展,强大的功能的同时兼具性能 功能: Basic table (基础表格) Grid (高级表格) Size (尺寸) Striped (斑马线条纹) Table with border (带边框) Cell style (单元格样式) Column resizable (列宽拖动) Maximum table height (最大高度) Resize height and width (响应式宽高) Fixed column (固定列) Grouping table head (表头分组) Highlight row and column (高亮行、列) Table sequence (序号) Radio (单选) Checkbox (多选) Sorting (排序) Filter (筛选) Rowspan and colspan (合并行或列) Footer summary (表尾合计) Import (导入) Export (导出) Print (打印) Show/Hide column (显示/隐藏列) Loading (加载中) Formatted content (格式化内容) Custom template (自定义模板) Context menu(快捷菜单) Virtual Scroller(虚拟滚动) Expandable row (展开行) Pager(分页) Form(表单) Toolbar(工具栏) Tree table (树形表格) Editable CRUD(增删改查) Validate(数据校验) Data Proxy(数据代理) Keyboard navigation(键盘导航) Modal window(模态窗口) Charts(图表工具) 更新日志: v4.0.20 table 修改单选框、复选框获取值错误问题 grid 修复 KeepAlive 中报错问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值