VUE3.0表格的公共组件

本文介绍如何在Vue.js项目中封装一个简洁易用的表格公共组件,该组件适用于后台控制台等场景,但不支持深层数据展示。通过示例代码展示了组件的使用方法,包括设置表格列配置,如选择框、序号、普通内容和具名插槽,并提供了在其他组件中调用公共组件的模板结构,以及如何通过插槽实现操作列的功能。

一、目的

一般在后台控制台之类的项目中,表格和图标是应用最多的地方,我们今天来封装一个表格的公共组件,用起来非常的简单。

二、表格公共组件代码

// src/components/mytable.vue

<template>
  <el-table
      :data="tableData"
      stripe
      style="width: 100%">
    <el-table-column v-for="col in tableConfig" :key="col.label" :type="col.type" :label="col.label" :prop="col.prop"
                     :align="col.align">
      <template v-slot="scope" v-if="col.type === null">
        <slot :name="col.name" v-if="col.slot" :row="scope.row"
              :index="scope.$index"></slot>
        <span v-else>{{ scope.row[col.prop] || '-' }}</span>
      </template>
    </el-table-column>
  </el-table>
</template>

<script lang="ts">
import { defineComponent} from "vue";
export default defineComponent({
  name:"mytable",
  props:{
    tableData:{
      default(data:any){
        return data
      }
    },
    tableConfig:{
      default(data:any){
        return data
      }
    },
  },

  setup() {
    return {}
  },
  components:{},
})
</script>

上面就是一个简单的展示用的表格,
缺点是不能展示深层数据。
下面说一下 如何使用

// 在其他组件中调用公共组件mytable
<template>
	<div>
		<mytable :tableData="tableData" :tableConfig="tableConfig">
		      <template v-slot:caozuo="shuju">
		        <el-button @click="caozuofunc(shuju.row.name)">操作</el-button>
		      </template>
		    </mytable>
	</div>
</template>

<script lang="ts">
import {defineComponent, ref} from "vue";
import Mytable from "@/components/mytable.vue";

export default defineComponent({
  name: "imagevue",
  setup() {
    const tableData = ref([
      {
        name: "sgsfsdfs",
        content: "61561651",
      },
      {
        name: "651651",
        content: "sdfsdfsd",
      },
    ])
    const tableConfig = ref([
      {type: "selection", align: "center"},
      {type: "index", align: "center"},
      {type: null, prop: "name", align: "center", label: "名字"},
      {type: null, prop: "content", align: "center", label: "内容"},
      {type: null, slot: true, name: "caozuo", label: "操作"},
    ])

    function caozuofunc(name: string) {
      console.log(name, "name")
    }

    return {
      tableData,
      tableConfig,
      caozuofunc,
    }
  },
  components: {
    Mytable
  },
})
</script>

{type: “selection”, align: “center”}, // 选择框
{type: “index”, align: “center”}, // 序号
{type: null, prop: “name”, align: “center”, label: “名字”}, // 普通内容
{type: null, prop: “content”, align: “center”, label: “内容”}, // 普通内容
{type: null, slot: true, name: “caozuo”, label: “操作”}, // 插槽,具名插槽,必须有name属性

template v-slot:caozuo=“shuju” 这里的shuju,就是我们给slot绑定的数据name,index,row
也就是说,你绑定了什么,就可以直接使用什么,比如shuju.index,shuju.row。
也可以直接解构, template v-slot:caozuo="{row,name,index}"

如果给mytable加上ref,就可以直接获取组件内的被选中的数据。
还可以扩展其他属性 ,比如fit,比如width等等,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值