vue3 + ts + element-plus(封装table表格)

一、安装 element-plus

element-plus官网icon-default.png?t=N7T8https://element-plus.org/zh-CN/


1、npm安装
npm install element-plus --save
2、yarn安装
yarn add element-plus
3、pnpm安装
pnpm install element-plus
4、main.ts导入(vue3)
import ElementPlus from 'element-plus'

import 'element-plus/dist/index.css'

app.use(ElementPlus)
二、封装table组件

// iot-table.vue
<template>
  <el-table :data="data" :height="height" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55" />
    <el-table-column
      v-for="column in columns"
      :key="column.prop"
      :prop="column.prop"
      :label="column.label"
      :width="column.width"
      :align="column.align"
      :fixed="column.fixed"
      :show-overflow-tooltip="column.showOverflowTooltip"
    >
      <template v-slot="scope">
        <component :is="column.render" v-bind="scope" v-if="column.render"></component>
        <span v-else>{{ scope.row[column.prop] }}</span>
      </template>
    </el-table-column>
  </el-table>
</template>

<script lang="ts" setup>
interface TableColumn {
  prop: string;
  label: string;
  width?: string | number;
  align?: string;
  fixed?: string | boolean;
  render?: (scope: any) => any;
  showOverflowTooltip?: boolean;
}

const props = defineProps<{
  data: any[];
  columns: TableColumn[];
  height?: string | number;
}>();

const emit = defineEmits<{
  (event: 'selection-change', selection: any[]): void;
}>();

const handleSelectionChange = (selection: any[]) => {
  emit('selection-change', selection);
};
</script>
三、使用上面封装的表格

<template>
    <iot-table :data="list" :columns="columns"  @selection-change="handleSelectionChange" />
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { ElIcon, ElButton } from 'element-plus'; // 导入button按钮或者其他的组件
import IotTable from '@/components/iot-table.vue'; // 导入封装好的table组件

const list = ref<any>([]); // 后端返回数组,接口获取到

const selectedRows =ref<any>([]) // 多选数组

const columns = ref<any>([
    {
        prop: 'name',
        label: '名字',
        width: 140, // 列宽度
        fixed: 'left', // 列固定
        render: (scope: any) => {
            // 判断后端返回的字段,可以用三元表达式,根据个人喜好
            if (scope.row.name === 'lisi') {
                return h('span', '李四')
            } else {
                return '张三';
            }
        },
    },
    {
        prop: 'messageId',
        label: '消息ID',
        align: 'center',
        width: 260,
        render: (scope: any) => {
            if (scope.row.messageId) {
                return h(
                    ElTag, // el-tag组件,还可以添加任意其他组件,在上面导入即可
                    {
                        type: "primary",
                        effect: "plain",
                    },
                    { default: () => scope.row.messageId }
                )
            }
        }
    },
    {
        prop: 'age',
        label: '年龄',
        align: 'center'
    },
    {
        prop: 'operate',
        label: '操作',
        align: 'center',
        fixed: 'right',
        width: 180,
        render: (scope: any) => {
            // 两个按钮以上用数组包裹,单个去掉数组包裹
            return [
                h(
                    ElButton,
                    {
                        onClick: () => openDetail(scope.row),
                        type: 'warning',
                        disabled: false,
                        size: 'small',
                        icon: 'View',
                        round: false,
                        circle: false,
                        plain: false,
                        link: true
                    },
                    { default: () => '详情' }
                ),
                h(
                    ElButton,
                    {
                        onClick: () => handleUpdate(scope.row),
                        type: 'primary',
                        disabled: false,
                        size: 'small',
                        icon: 'Edit',
                        round: false,
                        circle: false,
                        plain: false,
                        link: true
                    },
                    { default: () => '修改' }
                )
            ]
        }
    },
]);

const handleSelectionChange = (selection: any[]) => {
  selectedRows.value = selection.map(row => row.id);
};
</script>
  • 19
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
首先你需要在项目中安装 `element-plus`,可以通过以下命令进行安装: ``` npm install element-plus --save ``` 然后在你的 Vue 项目中引入 `element-plus` 的 `Table` 组件,以及需要使用的相关样式: ```js import { defineComponent } from 'vue'; import { ElTable, ElTableColumn } from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; export default defineComponent({ components: { ElTable, ElTableColumn }, data() { return { tableData: [], // 后台获取的数据 }; }, mounted() { // 在这里调用后台接口获取数据,并将返回的数据赋值给 tableData }, render() { return ( <div> <el-table data={this.tableData}> <el-table-column prop="name" label="名称"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </div> ); }, }); ``` 在上面的代码中,我们使用了 `ElTable` 和 `ElTableColumn` 组件来渲染表格,其中 `data` 属性绑定了从后台获取的数据 `tableData`,每个 `el-table-column` 标签的 `prop` 属性绑定了对应数据对象的属性名,`label` 属性则是表格列的标题。 当然,你还需要在项目中引入 `element-plus` 的样式,这里我们直接引入了整个 `index.css` 文件来覆盖默认样式。如果你只需要使用部分组件,可以按需引入对应的样式文件。 以上就是使用 `element-plus` 的 `Table` 组件渲染后台数据的基本方法,你可以根据具体需求进行进一步的定制和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值