vue3+el-table封装示例详解(编辑、删除、查看详情按钮一起封装)

 更新时间:2024年09月13日 11:19:36   作者:CallMe_CrabXie  
在Vue3中,利用Element Plus UI库封装表格组件,实现编辑、删除和查看详情的功能,通过定义tableData和tableDataHeader来管理表格数据和表头,其中tableData通常从后端获取,而tableHeader可根据具体需求自定义,感兴趣的朋友跟随小编一起看看吧

vue3+el-table封装(编辑、删除、查看详情按钮一起封装),具体代码如下所示:

// 封装源码(子组件)
<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column
      v-for="(column, index) in tableDataHeader"
      :label="column.label"
      :key="index"
      :prop="column.prop"
      :width="column.width"
    >
      <template #default="scope" v-if="column.operate">
        <el-button
          v-for="item in column.children"
          :key="item.prop"
          :type="item.type"
          @click="btnClick(item.method, scope.row, scope.$index)"
          >{{ item.label }}</el-button
        >
      </template>
    </el-table-column>
  </el-table>
</template>
<script setup lang="ts">
const props = defineProps<{
  tableData: Array<any>
  tableDataHeader: Array<any>
}>()
const emits = defineEmits(['deleteRow', 'editRow', 'detailRow'])
const btnClick = (method, row, index) => {
  console.log('method: ', method)
  emits(method, row, index)
}
</script>
<style scoped></style>
// 父组件调用
<template>
  <CustomTable
    :tableData="tableData"
    :tableDataHeader="tableDataHeader"
    @deleteRow="deleteRow"
    @editRow="edit"
    @detailRow="detail"
  >
  </CustomTable>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref, type Ref } from 'vue'
import CustomTable from '@/components/Custom-table.vue'
import { data } from '@/data/data.ts'
const tableData: Ref<Array> = ref(data.tableData)
const tableDataHeader = ref(data.tableDataHeader)
const deleteRow = (row: any, index: number) => {
  tableData.value.splice(index, 1)
  console.log('this tableData: ', tableData)
  pagenation.value.total = tableData.value.length
}
const edit = (row, index) => {
  console.log('row: ', row, index)
}
const detail = (row, index) => {
  console.log('row: ', row, index)
}
</script>
<style scoped></style>

对应的tableData和tableDataHeader文件(实际开发中,应该从后端拿tableData,tableHeader根据情况自定义)

export const data = {
  tableData: [
    {
      name: 'knife1',
      date: '2024-09-22',
      type: 'butterfly'
    },
    {
      name: 'knife2',
      date: '2024-09-22',
      type: 'M9'
    },
    {
      name: 'knife3',
      date: '2024-09-22',
      type: 'butterfly'
    }
  ],
  tableDataHeader: [
    {
      label: 'Knife Name',
      prop: 'name',
      width: 180
    },
    {
      label: 'Favorite Date',
      prop: 'date',
      width: 180
    },
    {
      label: 'Knife Type',
      prop: 'type',
      width: 180
    },
    {
      label: 'Operation',
      operate: true,
      prop: 'Operation',
      width: '280',
      children: [
        {
          label: 'edit',
          prop: 'edit',
          method: 'editRow',
          type: 'primary'
        },
        {
          label: 'Delete',
          prop: 'Delete',
          method: 'deleteRow',
          type: 'warning'
        },
        {
          label: 'Detail',
          prop: 'Detail',
          method: 'detailRow',
          type: 'info'
        }
      ]
    }
  ]
}

到此这篇关于vue3+el-table封装示例详解(编辑、删除、查看详情按钮一起封装)的文章就介绍到这了,更多相关vue3 el-tale封装内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue3.2+ts实现在方法中可调用的拟态框弹窗(类el-MessageBox)

    vue3.2+ts实现在方法中可调用的拟态框弹窗(类el-MessageBox)

    这篇文章主要介绍了vue3.2+ts实现在方法中可调用的拟态框弹窗(类el-MessageBox),这个需求最主要的是要通过方法去调用,为了像el-messagebox使用那样方便,需要的朋友可以参考下
    2022-12-12
  • Vue使用Echarts实现大屏可视化布局示例详细讲解

    Vue使用Echarts实现大屏可视化布局示例详细讲解

    这篇文章主要介绍了Vue使用Echarts实现大屏可视化布局示例,本文通过实例代码图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-01-01
  • Vue privide 和inject 依赖注入的使用详解

    Vue privide 和inject 依赖注入的使用详解

    这篇文章主要介绍了Vue privide 和inject 依赖注入的用法,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-10-10
  • Vue双向数据绑定与响应式原理深入探究

    Vue双向数据绑定与响应式原理深入探究

    本节介绍双向数据绑定以及响应式的原理,回答了双向数据绑定和数据响应式是否相同,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-08-08
  • vue上传图片到oss的方法示例(图片带有删除功能)

    vue上传图片到oss的方法示例(图片带有删除功能)

    这篇文章主要介绍了vue上传图片到oss的方法示例(图片带有删除功能),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • 从0到1解锁Element-Plus组件二次封装El-Dialog动态调用的原理解析

    从0到1解锁Element-Plus组件二次封装El-Dialog动态调用的原理解析

    作者通过二次封装Element-Plus的el-dialog组件,实现动态显示与数据传递功能,提升项目灵活性和代码复用性,适用于用户编辑、文件上传确认、权限管理等多场景需求,本文给大家介绍从0到1解锁Element-Plus组件二次封装El-Dialog动态调用的原理解析,感兴趣的朋友一起看看吧
    2025-07-07
  • vue路由history模式页面刷新404解决方法Koa Express

    vue路由history模式页面刷新404解决方法Koa Express

    这篇文章主要为大家介绍了vue路由history模式页面刷新404解决方法(Koa Express)详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • 手把手教你搭建一个vue项目的完整步骤

    手把手教你搭建一个vue项目的完整步骤

    身为入行未深的小白前端,不断的学习是我们不可丢失的习惯,前端流行的框架也是层出不穷,vue在众多框架中脱颖而出,下面这篇文章主要给大家介绍了关于搭建一个vue项目的完整步骤,需要的朋友可以参考下
    2022-07-07
  • vue实现全选和反选功能

    vue实现全选和反选功能

    这篇文章主要为大家详细介绍了vue实现全选和反选功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • Vue程序调试和排错技巧分享

    Vue程序调试和排错技巧分享

    因为程序的调试非常重要,程序猿可以利用好的调试方法去查找定位自己的问题所在之处,从而,达到纠正自己程序错误的地方,健壮自己的程序,让问题变得越来越少,程序变得越来越健康,所以本文给大家介绍了Vue程序调试和排错技巧,需要的朋友可以参考下
    2024-12-12

最新评论