表格的封装,vue3+TS+Elemntui

<template>
  <div>
    <el-table  ref="multipleTableRef" :data="tableData" style="text-align: center; width: 100% "
      @selection-change="handleSelectionChange">
      <!-- 复选框 -->
      <el-table-column type="selection" v-if="isTypeSelection" width="55" />
      <!-- 表格内容 -->
      <el-table-column 
      style="white-space: pre-wrap;"
      v-for="item in tableHeader" 
      :label="item.label" 
      :property="item.prop"
      :width="item.width || 'auto'"
      :show-overflow-tooltip="tooltip">
        <template #default="scope">
          <el-switch 
          v-if="item.type === 'switch'" 
          v-model="scope.row.is_use" 
          class="mb-2" 
          active-color="green"
          :active-value="1"
          :inactive-value="0" 
          inactive-color="#ccc"
          @change="$emit(item.event!, { id: scope.row.id, is_use: scope.row.is_use })" />
          <!-- 按钮组件的单个按钮 -->
          <div v-else-if="item.type === 'button'">
            <span v-for="(btn, index) in item.buttons" :key="index">
              <el-button link :type="btn.type" size="small" @click="$emit(btn.event, scope.row)">{{ btn.text }}</el-button>
              <span v-if="index !== 2 && item.buttons.length > 3 && index !== 5">|</span>
            </span>
          </div>

          <span v-else-if="item.type === 'text'" @click="$emit(item.event!, scope.row)">
            {{ scope.row.text }}
          </span>
          <span v-else>
            {{ item.formatter ? item.formatter(scope.row) : scope.row[item.prop!] }}
          </span>
        </template>
        
      </el-table-column>
      
    </el-table>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElTable } from 'element-plus'
import type { ITableHead } from "@/types/examination/test";
const props = withDefaults(defineProps<{
  isTypeSelection?: boolean,
  tooltip?: boolean
  tableHeader: any[],
  tableData: any[]
}>(), {
  isTypeSelection: true,
  tooltip: false,
  tableHeader: () => [],
  tableData: () => []
})
console.log(props);


interface User {
  date: string
  name: string
  address: string
}
const multipleTableRef = ref<InstanceType<typeof ElTable>>()
const multipleSelection = ref<User[]>([])
const handleSelectionChange = (val: User[]) => {
  multipleSelection.value = val
}
</script>

<style scoped>
.el-button {
  margin: 0 5px;
  
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值