封装一个每列带有search的表格

利用element ui 的自定义的头部实现,效果:
这里写图片描述
代码:

<template>
  <div class='searchtable'>
    <el-table
      :data="tableData"
      border
      @sort-change='sortColumns'
      @selection-change="handleSelectionChange"
      @row-click='clickRow'
      ref='commontable'
      :cell-class-name='changeCellStyle'
      style="width: 100%">

  <!-- 为了防止循环第一项显示在最后一项 -->
      <el-table-column>
        <el-table-column
          width="1"
        >
        </el-table-column>
      </el-table-column>
      <!-- 中间循环列 -->
      <!-- 当设置宽度的时候,循环此div,生成有固定宽度的表格 -->
      <div
       v-for='item in ColumnsData'
       :key='item.id'
       v-if='columnwidth'
      >
        <el-table-column
         :label='item.label'
         align='center'
         fixed='left'
         :width="columnwidth"
         v-if='item.id==0||item.id==1'
         sortable='custom'
         show-overflow-tooltip
         >
          <el-table-column
            :prop="item.prop"
            :width="columnwidth"
            :label='item.label'
            :render-header='addSearch'
            show-overflow-tooltip
            >
          </el-table-column>
        </el-table-column>
        <el-table-column
         :label='item.label'
         align='center'
         :width="columnwidth"
         v-else
         sortable='custom'
         show-overflow-tooltip
         >
          <el-table-column
            :prop="item.prop"
            :width="columnwidth"
            :label='item.label'
            :render-header='addSearch'
            show-overflow-tooltip
            >
          </el-table-column>
        </el-table-column>
      </div>
</template>

固定前两列
:render-header='addSearch' 自定义头部
在methods中显示:

    addSearch (h, {column}) {
      let inputValue = {}
      return h('Input', {
        props: {
          placeholder: 'Search' + ' ' + column.label,
          icon: 'ios-search-strong'
        },
        style: {
          paddingRight: '5px'
        },
        on: {
          input: val => {
            inputValue = val
            if (!inputValue) {
              this.vaildInputValue(column.label, inputValue)
            }
          },
          class: 'ivu-input-icon',
          'on-click': () => {
            this.vaildInputValue(column.label, inputValue)
          },
          'on-enter': () => {
            console.log('enter')
            this.vaildInputValue(column.label, inputValue)
          }
        }
      })
    },

render JS 渲染
相当于动态生成DOM元素,还可以给DOM元素添加事件,在jQuery中是createElement 来动态生成DOM元素。
vaildInputValue 函数是判断用户的输入

    vaildInputValue (label, inputValue) {
      if (!inputValue) {
        this.$set(this.search, label, '')
        this.sendSearchVal()
        return
      }
      this.$set(this.search, label, inputValue)
      this.sendSearchVal()
    },

给每一列的数据添加搜索的对象数据,默认的是空,如果发生改变就会改变search数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值