基于Vue+element-ui 的Table二次封装的实现

yizhihongxing

基于Vue+element-ui 的Table二次封装的实现的攻略如下:

1. 概述

在使用Vue+element-ui进行前端开发时,经常会使用element-ui中的Table组件进行表格展示。但是,由于项目需求和个性化设计的不同,可能需要对Table组件进行二次封装。本攻略主要讲解如何基于Vue+element-ui进行Table二次封装。

2. Table二次封装的实现

Table二次封装主要分为以下步骤:

步骤一:创建Table封装组件

首先,需要在Vue工程中创建Table封装组件,在组件中引入element-ui Table组件,并进行相关配置。代码示例:

<template>
  <el-table
    :data="tableData"
    :width="tableWidth"
    :max-height="pageHeight"
    :border="border"
    :fit="fit"
    :stripe="stripe"
    :highlight-current-row="highlightCurrentRow"
    :row-class-name="rowClassName"
    :tooltip-effect="tooltipEffect"
    @sort-change="sortChange"
    @current-change="currentChange"
    @select="select"
    @select-all="selectAll">
    <!-- 列头 -->
    <el-table-column
      v-for="column in columns"
      :key="column.prop"
      :prop="column.prop"
      :label="column.label"
      :width="column.width"
      :min-width="column.minWidth"
      :fixed="column.fixed"
      :resizable="column.resizable"
      :formatter="column.formatter"
      :sortable="column.sortable"
      :sort-method="column.sortMethod"
      :sort-by="column.sortBy"
      :sort-orders="column.sortOrders"
      :show-overflow-tooltip="column.showOverflowTooltip"
      :align="column.align"
      :header-align="column.headerAlign"
      :class-name="column.className"
      :label-class-name="column.labelClassName"
      :selectable="column.selectable"
      v-if="!column.hidden"
    >
      <!-- 插槽 -->
      <template v-for="slot in Object.keys(column.slots)">
        <template v-slot:[slot]="{row}">
          <slot :name="slot" :row="row"></slot>
        </template>
      </template>

      <!-- 默认插槽 -->
      <template v-if="!column.slots.default">
        {{ column.prop }}
      </template>

      <!-- 隐藏列 -->
      <template v-if="column.hidden">
        <el-table-column
          :key="`hidden_${column.prop}`"
          :prop="column.prop"
          :label="column.label"
          :class-name="column.className"
          :label-class-name="column.labelClassName"
          :selectable="column.selectable"
          :show-overflow-tooltip="column.showOverflowTooltip"
          :formatter="(row) => ''"
          :sortable="false"
          :sort-by="column.sortBy"
          :sort-orders="column.sortOrders"
          :width="0"
          :min-width="0"
          :fixed="column.fixed"
          :resizable="column.resizable"
          :align="column.align"
          :header-align="column.headerAlign"
        ></el-table-column>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  name: 'MyTable',
  props: {
    // 表格数据
    tableData: {
      type: Array,
      required: true
    },
    // 表格列配置
    columns: {
      type: Array,
      required: true
    },
    // 表格宽度
    tableWidth: {
      type: String,
      default: '100%'
    },
    // 分页高度
    pageHeight: {
      type: String,
      default: ''
    },
    // 是否显示边框
    border: {
      type: Boolean,
      default: true
    },
    // 是否宽度适应容器
    fit: {
      type: Boolean,
      default: true
    },
    // 是否交替显示背景色
    stripe: {
      type: Boolean,
      default: true
    },
    // 是否高亮选中行
    highlightCurrentRow: {
      type: Boolean,
      default: true
    },
    // 行的自定义类名
    rowClassName: {
      type: [String, Function],
      default: ''
    },
    // tooltip的打开方式
    tooltipEffect: {
      type: String,
      default: 'dark'
    }
  },
  methods: {
    // 回调函数:列排序改变时触发
    sortChange({ column, prop, order }) {
      console.log(column, prop, order);
    },
    // 回调函数:当前行发生变化时触发
    currentChange(currentRow, oldCurrentRow) {
      console.log(currentRow, oldCurrentRow);
    },
    // 回调函数:选中行时触发
    select(selection, row) {
      console.log(selection, row);
    },
    // 回调函数:全选时触发
    selectAll(selection) {
      console.log(selection);
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

步骤二:根据需求进行封装

在创建好Table封装组件后,根据项目需求进行封装。主要包括以下步骤:

  1. 封装分页组件;
  2. 封装表格查询组件,进行数据过滤;
  3. 封装自定义列组件;
  4. 封装表头固定组件;
  5. 封装表格编辑组件。
  6. 插入slot以实现自定义需求

以上步骤可以根据实际需要进行选择,也可以根据需求进行自定义封装。

步骤三:引入封装的Table组件

在需要使用封装的Table组件的地方,通过import引入自定义的Table组件即可。代码示例:

<template>
  <div>
    <my-table
      :table-data="tableData"
      :columns="columns"
      :table-width="tableWidth"
      :page-height="pageHeight"
      :border="border"
      :fit="fit"
      :stripe="stripe"
      :highlight-current-row="highlightCurrentRow"
      :row-class-name="rowClassName"
      :tooltip-effect="tooltipEffect"
      @sort-change="sortChange"
      @current-change="currentChange"
      @select="select"
      @select-all="selectAll">
    </my-table>
  </div>
</template>

<script>
import MyTable from '@/components/MyTable.vue'

export default {
  components: {
    MyTable
  },
  data() {
    return {
      tableData: [],
      columns: [
        {
          label: '姓名',
          prop: 'name'
        },
        {
          label: '年龄',
          prop: 'age'
        }
      ],
      tableWidth: '100%',
      pageHeight: '',
      border: true,
      fit: true,
      stripe: true,
      highlightCurrentRow: true,
      rowClassName: '',
      tooltipEffect: 'dark'
    }
  },
  methods: {
    sortChange({ column, prop, order }) {
      console.log(column, prop, order);
    },
    currentChange(currentRow, oldCurrentRow) {
      console.log(currentRow, oldCurrentRow);
    },
    select(selection, row) {
      console.log(selection, row);
    },
    selectAll(selection) {
      console.log(selection);
    }
  }
}
</script>

<style lang="scss">
</style>

3. 示例说明

示例一:Table组件实现分页功能

在二次封装中,新增了分页组件,在Table组件中通过引入el-pagination组件实现分页功能。代码示例:

<template>
  <div>
    <el-pagination
      :background="background"
      :page-size="pageSize"
      :page-sizes="pageSizes"
      :total="total"
      @current-change="_currentChange"
      @size-change="_sizeChange"
      layout="total, sizes, prev, pager, next, jumper"
    />
  </div>
</template>

<script>
export default {
  name: 'MyPagination',
  props: {
    // 是否有背景颜色
    background: {
      type: Boolean,
      default: true
    },
    // 当前页码
    currentPage: {
      type: Number,
      required: true
    },
    // 每页展示条数
    pageSize: {
      type: Number,
      default: 10
    },
    // 每页展示条数选择器
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 50, 100];
      }
    },
    // 数据总条数
    total: {
      type: Number,
      default: 0
    }
  },
  methods: {
    // 回调函数:当前页码发生变化时触发
    _currentChange(currentPage) {
      this.$emit('current-change', currentPage);
    },
    // 回调函数:每页展示条数发生变化时触发
    _sizeChange(pageSize) {
      this.$emit('size-change', pageSize);
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

示例二:Table组件实现自定义列功能

在二次封装中,新增了自定义列组件,在Table组件中通过引入可编辑列插件table-editor,实现自定义列功能。代码示例:

<template>
  <div>
    <el-dropdown @command="handleCommand">
      <span class="el-dropdown-link">
        <i class="el-icon-setting"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item v-for="column in allColumns" :key="column.prop" :command="column.prop" :disabled="column.required">
          {{ column.label }}
          <i class="el-icon-check" v-if="column.required"></i>
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>

<script>
export default {
  name: 'MyColumnCustomize',
  props: {
    // 默认列配置
    defaultColumns: {
      type: Array,
      required: true
    },
    // 可选列配置
    allColumns: {
      type: Array,
      required: true
    }
  },
  methods: {
    // 回调函数:选择自定义列时触发
    handleCommand(command) {
      this.$emit('change-columns', command);
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

以上是基于Vue+element-ui的Table二次封装的实现的攻略,通过封装Table组件,可以更加灵活方便地满足项目需求。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:基于Vue+element-ui 的Table二次封装的实现 - Python技术站

(0)
上一篇 2023年6月25日
下一篇 2023年6月25日

相关文章

  • SQL实现递归及存储过程中In()参数传递解决方案详解

    下面我将为你详细讲解“SQL实现递归及存储过程中In()参数传递解决方案详解”的完整攻略。 SQL实现递归 什么是递归 递归(Recursion)指的是在函数内部调用函数本身的方法。在SQL中,递归主要使用WITH RECURSIVE语句来实现。 WITH RECURSIVE语句 WITH RECURSIVE语句是递归查询的核心语句,它的语法如下: WITH…

    other 2023年6月27日
    00
  • Java实现UDP通信过程实例分析【服务器端与客户端】

    Java实现UDP通信过程实例分析【服务器端与客户端】 本文将详细介绍如何使用Java语言实现UDP(用户数据报协议)通信,其中包含了服务器端与客户端的实现过程。UDP是一种无连接的传输协议,相较于传输控制协议TCP而言,UDP具备更高的传输速度,但是它不保证消息的可靠性,容易造成消息的丢失和乱序等问题,因此仅在特定场合使用。 1. TCP和UDP协议的区别…

    other 2023年6月27日
    00
  • vue项目多租户环境变量的设置

    下面我将分享一下“Vue项目多租户环境变量的设置”的完整攻略。 什么是多租户? “多租户”指的是一种软件架构,帮助多个用户或组织以独立、安全且自主控制的方式共享单个实例的应用程序。在多租户系统中,每个用户(或组织)都有自己的数据、配置、用户界面,但是所有这些内容都在同一个共享实例中运行。 在开发一些软件时,我们需要针对多个租户(即多个客户)构建具有不同配置的…

    other 2023年6月27日
    00
  • Spring的@Validation和javax包下的@Valid区别以及自定义校验注解

    Spring的@Validation和javax包下的@Valid区别 在Java中,我们经常需要对输入数据进行校验,以确保数据的有效性和一致性。Spring框架和javax包都提供了校验注解来简化这个过程。下面将详细讲解Spring的@Validation和javax包下的@Valid的区别以及如何自定义校验注解。 @Validation注解 Spring…

    other 2023年7月28日
    00
  • 苹果 iOS 13.6/iPadOS 13.6 开发者预览/公测版 Beta 3推送

    下面是“苹果 iOS 13.6/iPadOS 13.6 开发者预览/公测版 Beta 3推送”的完整攻略: 步骤一:备份数据 在升级系统之前,建议对个人数据进行备份,以免数据丢失造成困扰。方法如下: 1.1 连接设备到电脑上,打开iTunes 或 Finder1.2 点击“备份”,等待备份完成1.3 可选步骤: 导出数据,以便稍后恢复使用 步骤二:下载 be…

    other 2023年6月26日
    00
  • Vue-Router2.X多种路由实现方式总结

    Vue-Router2.X多种路由实现方式总结 Vue-Router是Vue.js官方的路由管理器,用于实现单页应用的路由功能。在Vue-Router2.X版本中,有多种方式可以实现路由功能。本攻略将详细介绍这些实现方式,并提供两个示例说明。 1. 基本路由配置 Vue-Router的基本路由配置是通过定义路由表来实现的。路由表是一个数组,每个路由都是一个对…

    other 2023年7月28日
    00
  • 魅族mx4无限重启怎么办? 魅族mx4问题汇总及解决方法

    魅族MX4无限重启的解决方法 问题现象 在使用魅族MX4手机的过程中,可能会出现无限重启的问题,这会导致手机无法正常使用。问题一般表现为手机重启后进入欢迎界面后再次自动重启。 解决方法 方法一:恢复出厂设置 恢复出厂设置可以清除手机中的所有数据和程序,并重置手机到出厂状态。这种方法可以解决许多问题,包括无限重启的问题。注意,在执行此操作前请务必备份您的数据,…

    other 2023年6月27日
    00
  • 常用的HTML富文本编译器UEditor、CKEditor、TinyMCE、HTMLArea、eWebEditor、KindEditor简介

    常用的HTML富文本编辑器有UEditor、CKEditor、TinyMCE、HTMLArea、eWebEditor、KindEditor等。下面我将对每个编辑器进行简要的说明介绍。 1. UEditor UEditor是由百度开发的一款富文本编辑器,支持中英文输入、拼写检查、超链接、表情等功能,具有简单易用、插件丰富、可扩展性强的特点。 UEditor的集…

    other 2023年6月26日
    00
合作推广
合作推广
分享本页
返回顶部