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

typescript 复制代码
// 封装源码(子组件)
<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>
typescript 复制代码
// 父组件调用
<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根据情况自定义)

typescript 复制代码
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'
        }
      ]
    }
  ]
}
相关推荐
白总Server16 分钟前
pptp解说
前端·javascript·vue.js·物联网·网络协议·数据库架构·idc
前端李易安1 小时前
javascript中的数据类型以及存储上的区别
开发语言·javascript·ecmascript
她似晚风般温柔7892 小时前
Uniapp + Vue3 + Vite +Uview + Pinia 实现购物车功能(最新附源码保姆级)
开发语言·javascript·uni-app
布瑞泽的童话2 小时前
Vue 也能这样玩?Vuetify 打造超强大的UI体验
前端·vue.js·ui·开源
加勒比海涛2 小时前
ElementUI 快速入门:使用 Vue 脚手架搭建项目
前端·vue.js·elementui
sun_weitao2 小时前
extends in javascript
开发语言·前端·javascript
Jiaberrr3 小时前
轻松切换淘宝最新镜像源,加速npm依赖安装
前端·javascript·vue.js·npm·node.js
Teln_小凯3 小时前
VUE + NODE 历史版本安装
前端·javascript·vue.js
亦世凡华、3 小时前
探索 Electron:助力文档操作应用快速落地
前端·javascript·经验分享·electron·前端框架
waterHBO3 小时前
react js 使用 useEffect 钩子
前端·javascript·react.js