關(guān)于ElementUI自定義Table支持render
ElementUI自定義Table支持render
ElementUI中的Table組件可以通過(guò)render-header屬性通過(guò)render函數(shù)渲染表頭,對(duì)于數(shù)據(jù)單元格并沒(méi)有相關(guān)支持,雖然可以通過(guò)<template slot-scope="scope"></template >自定義列,但是在某些操作中直接用·render·形式進(jìn)行渲染會(huì)更加有效,我一般喜歡通過(guò)數(shù)據(jù)的形式配置表格的內(nèi)容,所以對(duì)ElementUI中的Table組件進(jìn)行二次封裝。
首先編寫(xiě)用于表頭和數(shù)據(jù)單元格的部分:
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]) ? ? } ? } }
最后編寫(xiě)表格主要部分: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: '應(yīng)用編號(hào)', ? ? ? ? ? fixed: true, ? ? ? ? ? align: 'center' ? ? ? ? }, ? ? ? ? { ? ? ? ? ? prop: 'appName', ? ? ? ? ? label: '應(yīng)用名稱(chēng)', ? ? ? ? ? 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表頭自帶排序功能,和排序事件,但是目前只是對(duì)當(dāng)前界面的數(shù)據(jù)進(jìn)行排序。
- 項(xiàng)目需求:點(diǎn)擊表頭排序的時(shí)候,對(duì)所有數(shù)據(jù)進(jìn)行排序。
- 初步方案:在點(diǎn)擊排序按鈕的時(shí),在排序事件sort-change 中,進(jìn)行數(shù)據(jù)請(qǐng)求,此時(shí)會(huì)先重拍一次當(dāng)前頁(yè)面的數(shù)據(jù),再渲染接口返回?cái)?shù)據(jù)。用戶(hù)體驗(yàn)不是很好。
- 優(yōu)化方案:使用render-header自定義tableHeader,此時(shí)要使用render函數(shù)來(lái)創(chuàng)建表頭。
getheaderTime(h) { ? ? ? const This = this ? ? ? return h('div', { ? ? ? }, ? ? ? ? ['告警時(shí)間', ? ? ? ? ? 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' ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? }) ? ? ? ? ? ? ] ? ? ? ? ? ) ? ? ? ? ]) ? ? }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
簡(jiǎn)單聊聊Vue中的ref,toRef與toRefs
這篇文章主要是想和大家來(lái)簡(jiǎn)單聊聊Vue中的ref、toRef、toRefs這三個(gè)函數(shù)的使用與區(qū)別,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04vue基于Teleport實(shí)現(xiàn)Modal組件
Teleport 提供了一種干凈的方法,允許我們控制在 DOM 中哪個(gè)父節(jié)點(diǎn)下渲染了 HTML,而不必求助于全局狀態(tài)或?qū)⑵洳鸱譃閮蓚€(gè)組件。2021-05-05vue?el-table實(shí)現(xiàn)動(dòng)態(tài)添加行和列具體代碼
最近遇到一個(gè)動(dòng)態(tài)增加行和列的需求,所以這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于vue?el-table實(shí)現(xiàn)動(dòng)態(tài)添加行和列的相關(guān)資料,需要的朋友可以參考下2023-09-09使用vue-cli搭建SPA項(xiàng)目的詳細(xì)過(guò)程
vue-cli是vue.js的腳手架,用于自動(dòng)生成vue.js+webpack的項(xiàng)目模板,本文通過(guò)實(shí)例代碼給大家介紹vue-cli搭建SPA項(xiàng)目的詳細(xì)過(guò)程,感興趣的朋友跟隨小編一起看看吧2022-09-09Vue 防止短時(shí)間內(nèi)連續(xù)點(diǎn)擊后多次觸發(fā)請(qǐng)求的操作
這篇文章主要介紹了Vue 防止短時(shí)間內(nèi)連續(xù)點(diǎn)擊后多次觸發(fā)請(qǐng)求的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11VUE element上傳動(dòng)態(tài)設(shè)置action路徑和參數(shù)的坑及解決
這篇文章主要介紹了VUE element上傳動(dòng)態(tài)設(shè)置action路徑和參數(shù)的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07VUE使用docxtemplater導(dǎo)出word文檔實(shí)例(帶圖片)
docxtemplate支持的功能很多,語(yǔ)法包含變量替換、條件判斷、循環(huán)、列表循環(huán)、表格循環(huán)等,下面這篇文章主要給大家介紹了關(guān)于VUE使用docxtemplater導(dǎo)出word功能(帶圖片)的相關(guān)資料,需要的朋友可以參考下2023-06-06