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'
        }
      ]
    }
  ]
}
相关推荐
Asort5 分钟前
React函数组件深度解析:从基础到最佳实践
前端·javascript·react.js
Justineo21 分钟前
TemPad Dev:设计与实现
前端·javascript·css
AAA简单玩转程序设计30 分钟前
救命!HTML按钮点了没反应?3分钟学会给网页“装脑子”
javascript·html
神秘的猪头32 分钟前
浏览器是如何渲染 HTML/CSS/JS 页面的?——从代码到像素的完整流程
前端·javascript
啷咯哩咯啷37 分钟前
el-table-v2 实现自适应列宽
前端·javascript·vue.js
3秒一个大38 分钟前
JavaScript 中 var、let 和 const 的区别与应用
javascript
inx1771 小时前
微信小程序开发实战:从结构到数据绑定的完整实现
javascript·微信小程序
青咕咕1 小时前
REACT系列:1、React 中 render() 的目的
react.js
一枚前端小能手1 小时前
📚 JavaScript 数据类型与数据结构全攻略 - 原始值、对象、Map/Set与弱引用实战
前端·javascript
景早1 小时前
小黑记账清单案例(axios,echarts,vue)
前端·vue.js·echarts