关于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-prop父组件向子组件进行传值的方法

    vue-prop父组件向子组件进行传值的方法

    下面小编就为大家分享一篇vue-prop父组件向子组件进行传值的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • 分分钟玩转Vue.js组件(二)

    分分钟玩转Vue.js组件(二)

    这篇文章教大家如何分分钟玩转Vue.js组件,完善了vue.js组件的学习资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • vue 实现模糊检索并根据其他字符的首字母顺序排列

    vue 实现模糊检索并根据其他字符的首字母顺序排列

    这篇文章主要介绍了vue 实现模糊检索,并根据其他字符的首字母顺序排列,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • vue封装实现自动循环滚动的列表

    vue封装实现自动循环滚动的列表

    在做数据大屏开发的过程中,经常出现需要对列表进行自动滚动的需求,所以本文就来为大家介绍一下如何利用vue封装一个自动循环滚动的列表吧
    2023-09-09
  • vue实现循环滚动列表

    vue实现循环滚动列表

    这篇文章主要为大家详细介绍了vue实现循环滚动列表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06
  • vue系列之动态路由详解【原创】

    vue系列之动态路由详解【原创】

    下面小编就为大家带来一篇vue系列之动态路由详解【原创】。小编觉得挺不错的,现在就想给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • Vue实现五子棋小游戏

    Vue实现五子棋小游戏

    这篇文章主要为大家详细介绍了Vue实现五子棋小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Vue3中使用Pinia修改State的五种方式

    Vue3中使用Pinia修改State的五种方式

    这篇文章主要介绍了Vue3中使用Pinia修改State的五种方式,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧
    2023-11-11
  • 关于vuex更新视图引发的一些思考详析

    关于vuex更新视图引发的一些思考详析

    我们在vuex中操作数据时遇见视图不更新的情况,下面这篇文章主要给大家介绍了关于vuex更新视图引发的一些思考,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • vue3项目启动自动打开浏览器以及server配置过程

    vue3项目启动自动打开浏览器以及server配置过程

    这篇文章主要介绍了vue3项目启动自动打开浏览器以及server配置过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03

最新评论