当前位置:   article > 正文

vue3+ts 结合el-table二次封装表格组件_export interface tabledata {}

export interface tabledata {}

效果图

 Table组件

  1. <template>
  2. <el-table :data="tableData" style="width: 80%" v-bind="{ border: true }">
  3. <el-table-column type="selection" width="50" align="center" />
  4. <el-table-column type="index" label="序号" width="100" fixed="left" align="center">
  5. <template #default="{ $index }">
  6. <i>{{ $index + 1 }}</i>
  7. </template>
  8. </el-table-column>
  9. <el-table-column
  10. v-for="col in option"
  11. :key="col.prop"
  12. :label="col.label"
  13. :prop="col.prop"
  14. :width="col.width"
  15. :align="col.align"
  16. v-bind="{ ...col.attrs }"
  17. >
  18. <template #header>
  19. <!-- <span v-if="col.editable"><button>可编辑</button></span> -->
  20. <span>{{ col.label }}</span>
  21. </template>
  22. <template #default="{ row, $index }" v-if="col.slot">
  23. <slot :name="col.slot" :row="row" :index="$index"></slot>
  24. </template>
  25. </el-table-column>
  26. </el-table>
  27. </template>
  28. <script lang="ts" setup>
  29. import { TableOption, TableData } from './tableData'
  30. type Props = {
  31. option: Array<TableOption>
  32. tableData: Array<TableData>
  33. }
  34. const props = defineProps<Props>()
  35. watch(props.tableData, (newVal, oldVal) => {
  36. console.log(newVal, oldVal)
  37. })
  38. </script>
  39. <style></style>

其中表格的勾选列和序号列也可以添加v-if来控制隐藏

Table的类型接口和列

  1. export interface TableOption {
  2. // 字段名称
  3. prop?: string
  4. // 表头
  5. label: string
  6. // 对应列的宽度
  7. width?: string | number
  8. // 对齐方式
  9. align?: 'left' | 'center' | 'right'
  10. // 自定义列模板的插槽名
  11. slot?: string
  12. // 是否是操作项
  13. action?: boolean
  14. // 是否可以编辑
  15. editable?: boolean
  16. // 属性
  17. attrs?: object
  18. }
  19. export interface TableData {
  20. date?: string
  21. name?: string
  22. address?: string
  23. }
  24. export interface FormData {
  25. name?: string
  26. }
  27. export const option: TableOption[] = [
  28. {
  29. label: 'Date',
  30. prop: 'date',
  31. width: 200,
  32. slot: 'dateSlot',
  33. editable: true,
  34. attrs: {
  35. sortable: ''
  36. }
  37. },
  38. {
  39. label: 'Name',
  40. prop: 'name',
  41. slot: 'nameSlot',
  42. attrs: {
  43. sortable: ''
  44. }
  45. },
  46. {
  47. label: 'Address',
  48. prop: 'address',
  49. slot: 'addressSlot',
  50. attrs: {
  51. sortable: ''
  52. }
  53. },
  54. {
  55. label: 'Edit',
  56. prop: 'edit',
  57. slot: 'editSlot'
  58. }
  59. ]

 调用

  1. <template>
  2. <el-row class="mb-4">
  3. <el-button type="primary" @click="addRow">新增行</el-button>
  4. <el-button type="success">Success</el-button>
  5. </el-row>
  6. <common-table :option="option" :tableData="tableData">
  7. <template #dateSlot="{ row }">
  8. <div>{{ row.date }}</div>
  9. </template>
  10. <template #nameSlot="{ row }">
  11. <div>
  12. {{ row.name }}
  13. </div>
  14. </template>
  15. <template #addressSlot="{ row }">
  16. <div>
  17. {{ row.address }}
  18. </div>
  19. </template>
  20. <template #editSlot="{ row, index }">
  21. <div>
  22. <el-button @click="deleteRow(row, index)">删除</el-button>
  23. <el-button @click="viewRow(row)">查看</el-button>
  24. </div>
  25. </template>
  26. </common-table>
  27. </template>
  28. <script setup lang="ts">
  29. import CommonTable from '../commonTable/CommonTable.vue'
  30. import { option, TableData } from './tableData'
  31. const tableData = reactive<TableData[]>([
  32. {
  33. date: '2016-05-03',
  34. name: '1',
  35. address: '北京'
  36. },
  37. {
  38. date: '2016-05-02',
  39. name: '2',
  40. address: '上海'
  41. },
  42. {
  43. date: '2016-05-04',
  44. name: '3',
  45. address: '南京'
  46. }
  47. ])
  48. const addRow = () => { // 添加行
  49. const data = {
  50. date: '22222',
  51. name: 'asjdlasjdlad',
  52. address: 'asjd;sad'
  53. }
  54. tableData.push(data)
  55. }
  56. const deleteRow = (row: TableData, index: number) => { // 删除行
  57. tableData.splice(index, 1)
  58. }
  59. </script>
  60. <style></style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/701577
推荐阅读