Vue3-Element表格组件(el-table)的简单封装

封装的表格-myTable.vue

 

<template>
    <div>
        <el-table :data="dataSource" style="width: 100%">
            <template v-for="(column, ind, key) in columns" :key="column">
                <el-table-column v-if="column.scopeVal" :prop="column.prop" :label="column.label">
                    <template #default="scope">
                        <slot 
                        :column="column" 
                        :record="scope.row" 
                        :text="scope.row[column.prop]" 
                        :index="dataSource.indexOf(scope.row)"
                        :name="column.prop">
                        </slot>
                    </template>
                </el-table-column>
                <el-table-column v-else :prop="column.prop" :label="column.label" />
            </template>
        </el-table>
    </div>
</template>

<script>
import reactive from 'vue'


export default {
    props: {
        dataSource: Array,
        columns: Array,
    }
}
</script>

父组件调用.vue

<template>
  <el-card>
    <my-table :columns="columns" :dataSource="dataSource">
      <template #method="{ index }">
        <my-select :options="method_options" v-model="dataSource[index].method" placeholder="待选择"></my-select>
      </template>
      <template #value="{ index }">
        <el-input v-model="dataSource[index].value" placeholder="待填写"></el-input>
      </template>
      <template #text="{ index }">
        <el-input v-model="dataSource[index].text" placeholder="待填写"></el-input>
      </template>
      <template #notes="{ column, record, text, index }">
        <el-input v-model="dataSource[index].notes" placeholder="待填写"></el-input>
      </template>

      <template #action="{ index }" class="action">
        <el-link><el-icon><CirclePlusFilled /></el-icon></el-link>
        <el-link><el-icon><RemoveFilled /></el-icon></el-link>
        <el-link><el-icon><ArrowUpBold /></el-icon></el-link>
        <el-link><el-icon><ArrowDownBold /></el-icon></el-link>
      </template>
    </my-table>
  </el-card>
</template>
  
<script setup>
import { reactive, ref } from "vue"
import myTable from "../../components/table/myTable.vue";
import mySelect from "../../components/select/myselect.vue"


components: { myTable, mySelect }

const method_options = [
  {
    label: '访问',
    value: '访问'
  },
  {
    label: '点击',
    value: '点击'
  },
  {
    label: '输入',
    value: '输入'
  },
  {
    label: '断言',
    value: '断言'
  },
]

const columns = reactive([
  {
    label: '方法',
    prop: 'method',
    scopeVal: true,
  },
  {
    label: '元素定位',
    prop: 'value',
    scopeVal: true,
  },
  {
    label: '输入',
    prop: 'text',
    scopeVal: true,
  },
  {
    label: '备注',
    prop: 'notes',
    scopeVal: true,
  },
  {
    label: '状态',
    prop: 'status',
  },
  {
    label: '操作',
    prop: 'action',
    scopeVal: true,
  },
])

let dataSource = reactive([
  {
    method: '访问',
    value: '',
    text: 'http://www.baidu.com',
    notes: '访问百度',
    status: '待运行',
  },
  {
    method: '点击',
    value: '//*[@id=kw]',
    text: '',
    notes: '点击搜索框',
    status: '待运行',
  },
])

说明

通过作用域插槽传递以下4个参数供使用

        column:表头

        record:当前行数据

        text:当前单元格数据

        index:当前行索引

若要自定义某一列的元素,在column中的对应列名下加上scopeVal: true

效果

最近在慢慢转vue3,过程中还是用vue2的写法和思维去写,虽说vue3兼容vue2,但有些地方还是有点区别,在作用域插槽上也花了点时间。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值