关于ElementUI自定义Table支持render

 更新时间:2022年10月21日 09:56:00   作者:蒋固金  
这篇文章主要介绍了关于ElementUI自定义Table支持render,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

ElementUI自定义Table支持render

ElementUI中的Table组件可以通过render-header属性通过render函数渲染表头,对于数据单元格并没有相关支持,虽然可以通过<template slot-scope="scope"></template >自定义列,但是在某些操作中直接用·render·形式进行渲染会更加有效,我一般喜欢通过数据的形式配置表格的内容,所以对ElementUI中的Table组件进行二次封装。

首先编写用于表头和数据单元格的部分:

TableHeaderCell.js

export default {
  name: 'TableHeadCell',
  functional: true,
  props: {
    render: Function,
    index: Number,
    column: Object,
    scopeColumn: Object,
    columns: Array,
    data: Array
  },
  render: (h, ctx) => {
    if (typeof ctx.props.render === 'function') {
      const params = {
        index: ctx.props.index,
        column: ctx.props.column,
        scopeColumn: ctx.props.scopeColumn,
        columns: ctx.props.columns,
        data: ctx.props.data,
        _self: ctx
      }
      return ctx.props.render.call(ctx.parent.$parent, h, params)
    } else {
      return h('span', ctx.props.column.label || ctx.props.column.prop || ctx.props.scopeColumn.property)
    }
  }
}

TableCell.js

export default {
  name: 'TableCell',
  functional: true,
  props: {
    row: Object,
    render: Function,
    index: Number,
    column: Object,
    scopeColumn: Object,
    columns: Array,
    data: Array
  },
  render: (h, ctx) => {
    if (typeof ctx.props.render === 'function') {
      const params = {
        row: ctx.props.row,
        index: ctx.props.index,
        column: ctx.props.column,
        scopeColumn: ctx.props.scopeColumn,
        columns: ctx.props.columns,
        data: ctx.props.data,
        _self: ctx
      }
      return ctx.props.render.call(ctx.parent.$parent, h, params)
    } else {
      if (typeof ctx.props.column.formatter === 'function') {
        return h('span', 
          ctx.props.column.formatter(
            ctx.props.row, ctx.props.scopeColumn,
            ctx.props.row[ctx.props.column.prop],
            ctx.props.index
          )
        )
      }
      return h('span', ctx.props.row[ctx.props.column.prop])
    }
  }
}

最后编写表格主要部分:index.vue

<template>
  <el-table
    ref="targetTable"
    :data="data"
    v-bind="$attrs"
    v-on="$listeners"
  >
    <slot slot="empty" name="empty" />
    <slot slot="append" name="append" />
    <slot name="columns">
      <el-table-column
        v-for="column in computedColumns"
        :key="column.prop"
        v-bind="column"
      >
        <template slot="header" slot-scope="scope">
          <tabel-head-cell :column="column" :scope-column="scope.column"
            :index="scope.$index" :render="column.headerRender" :columns="columns" :data="data" />
        </template>
        <template slot-scope="scope">
          <tabel-cell :row="scope.row" :column="column" :scope-column="scope.column"
            :index="scope.$index" :render="column.render" :columns="columns" :data="data" />
        </template>
      </el-table-column>
    </slot>
  </el-table>
</template>
<script>
import TabelCell from './TableCell'
import TabelHeadCell from './TableHeadCell'
const TATGET_TABLE_REF = 'targetTable'
export default {
  name: 'RenderTable',
  components: { TabelHeadCell, TabelCell },
  props: {
    columns: { type: Array, default: () => {} },
    data: { type: Array, default: () => {} }
  },
  computed: {
    computedColumns() {
      return this.columns && this.columns.filter(column => column.visible === undefined
        || column.visible === null || !!column.visible)
    }
  },
  methods: {
    // 表格原始方法
    clearSelection() {
      this.$refs[TATGET_TABLE_REF].clearSelection()
    },
    toggleRowSelection(row, selected) {
      this.$refs[TATGET_TABLE_REF].toggleRowSelection(row, selected)
    },
    toggleAllSelection() {
      this.$refs[TATGET_TABLE_REF].toggleAllSelection()
    },
    toggleRowExpansion(row, expanded) {
      this.$refs[TATGET_TABLE_REF].toggleRowExpansion(row, expanded)
    },
    setCurrentRow(row) {
      this.$refs[TATGET_TABLE_REF].setCurrentRow(row)
    },
    clearSort() {
      this.$refs[TATGET_TABLE_REF].clearSort()
    },
    clearFilter(columnKey) {
      this.$refs[TATGET_TABLE_REF].clearFilter(columnKey)
    },
    doLayout() {
      this.$refs[TATGET_TABLE_REF].doLayout()
    },
    sort(prop, order) {
      this.$refs[TATGET_TABLE_REF].sort(prop, order)
    }
  }
}
</script>

使用示例:

<template>
    <render-table
      :columns="columns"
      :data="list"
    />
</template>
<script>
import RenderTable from '_c/RenderTable'
export default {
  name: 'RenderTableTest',
  components: { RenderTable},
  data() {
    return {
      columns: [
        {
          prop: 'appId',
          label: '应用编号',
          fixed: true,
          align: 'center'
        },
        {
          prop: 'appName',
          label: '应用名称',
          align: 'center'
        },
        {
          prop: 'enabled',
          label: '是否启用',
          align: 'center',
          formatter(row, column, cellValue, index) {
            return cellValue ? '是' : '否'
          }
        },
        {
          fixed: 'right',
          label: '操作',
          align: 'center',
          render(h, { row }) {
            const _this = this
            return h('el-button-group', [
              h('el-button', {
                props: {
                  size: 'mini',
                  type: 'primary'
                },
                on: {
                  'click'() {
                    _this.handleEdit(row)
                  }
                }
              }, '编辑')
            ])
          }
        }
      ],
      list: []
    }
  },
  methods: {
    handleEdit(row) {
    }
  }
}
</script>

ElementUI-Table表头排序

ElementUI-Table表头自带排序功能,和排序事件,但是目前只是对当前界面的数据进行排序。

  • 项目需求:点击表头排序的时候,对所有数据进行排序。
  • 初步方案:在点击排序按钮的时,在排序事件sort-change 中,进行数据请求,此时会先重拍一次当前页面的数据,再渲染接口返回数据。用户体验不是很好。
  • 优化方案:使用render-header自定义tableHeader,此时要使用render函数来创建表头。
getheaderTime(h) {
      const This = this
      return h('div', {
      },
        ['告警时间',
          h('span', {
            class: 'iline-table-sort'
          },
            [
              h('i', {
                'class': {
                  'el-icon-caret-bottom': This.orderByType === 'desc',
                  'el-icon-caret-top': This.orderByType === 'asc',
                  'active': This.orderBy === 'daqTime'
                },
                attrs: {
                  'orderByType': 'desc',
                  'orderType': 'daqTime'
                },
                on: {
                  click: This.clickHandler
                },
                style: {
                  fontSize: '22px'
                }
              })
            ]
          )
        ])
    }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • vue+elementUI中表格高亮或字体颜色改变操作

    vue+elementUI中表格高亮或字体颜色改变操作

    这篇文章主要介绍了vue+elementUI中表格高亮或字体颜色改变操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • Element Input组件分析小结

    Element Input组件分析小结

    这篇文章主要介绍了Element Input组件分析小结,详细的介绍了Input组件的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • Vue中Class和Style实现v-bind绑定的几种用法

    Vue中Class和Style实现v-bind绑定的几种用法

    项目开发中给元素添加/删除 class 是非常常见的行为之一, 例如网站导航都会给选中项添加一个 active 类用来区别选与未选中的样式,那么在 vue 中 我们如何处理这类的效果呢?下面我们就一起来了解一下
    2021-05-05
  • vue实现element表格里表头信息提示功能(推荐)

    vue实现element表格里表头信息提示功能(推荐)

    小编最近接了这样一个需求,需要在element表格操作一栏添加提示功能,下面小编给大家带来了基于vue实现element表格里表头信息提示功能,需要的朋友参考下吧
    2019-11-11
  • Vue2路由动画效果的实现代码

    Vue2路由动画效果的实现代码

    本篇文章主要介绍了Vue2路由动画效果的实现代码,可以根据不同的路径去改变动画的效果,有兴趣的可以了解一下
    2017-07-07
  • vue接入ts基本方法

    vue接入ts基本方法

    这篇文章主要介绍了vue接入ts基本方法,关于ts,也出了很久,下面我们就来简单学习了下ts并且在原有项目上接入基本ts语法,也算是一个初级尝试,简单梳理下基础的接入配置和已经遇到的问题,供需要的小伙伴们参考
    2022-01-01
  • VUE动态生成word的实现

    VUE动态生成word的实现

    这篇文章主要介绍了VUE动态生成word的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • Vue鼠标点击事件和键盘事件举例详解

    Vue鼠标点击事件和键盘事件举例详解

    在Vue框架中我们经常需要绑定各种JS事件,如"点击事件"、"鼠标移动事件"、"键盘事件"等等,这篇文章主要给大家介绍了关于Vue鼠标点击事件和键盘事件的相关资料,需要的朋友可以参考下
    2024-01-01
  • vue实现定时刷新数据,每隔5分钟执行一次

    vue实现定时刷新数据,每隔5分钟执行一次

    这篇文章主要介绍了vue实现定时刷新数据,每隔5分钟执行一次问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • 详解vue2路由vue-router配置(懒加载)

    详解vue2路由vue-router配置(懒加载)

    本篇文章主要介绍了详解vue2路由vue-router配置(懒加载),实例分析了vue-router懒加载的技巧,非常具有实用价值,需要的朋友可以参考下
    2017-04-04

最新评论