封装el-table组件

这篇博客介绍了如何在Vue中封装一个基于Element UI的表格组件,包括选择、排序和自定义插槽功能。组件接受`tableData`、`tableConfig`和`loading`等属性,允许父组件自定义列显示、操作按钮等内容,并通过`@selectionChange`事件传递选中行数据。

封装的el-table组件

<template>
  <el-table
    border
    v-loading="loading"
    :data="tableData"
    @selection-change="selectionChange"
    style="width: 100%">
    <el-table-column
      v-if="tableType"
      :type="tableType"
      width="55">
    </el-table-column>
    <template v-for="item in tableConfig">
      <el-table-column
        v-if="item.slotName"
        :sortable="item.sortable"
        :prop="item.field"
        :min-width="item.minWidth"
        :label="item.label"
      >
        <template slot-scope="scope">
          <slot :name="item.slotName" :scope="scope"></slot>
        </template>
      </el-table-column>
      <el-table-column
        v-else
        :sortable="item.sortable"
        :prop="item.field"
        :min-width="item.minWidth"
        :label="item.label"
      >
        <template slot-scope="scope">
          <span @click="() => {$emit(item.method, scope.row)}">{{scope.row[item.field]}}</span>
        </template>
      </el-table-column>
    </template>
  </el-table>
</template>

<script>
export default {
  props: {
    loading: {
      type: Boolean,
      default: false
    },
    tableType: String,
    /* tableType   表格类型
      属性值: index/selection/空
    */
    tableConfig:Array,
    /*
      tableConfig     配置项 | Array
        field: 列的名称 | string
        label: 列的名称 | string
        sortable:列是否排序 | boolean
        minWidth: 列的最小宽度 | number
        slotName: 插槽名称 | 列的类型为插槽时加上该属性 | string
        showPopover: 列是否需要展示popover组件 | boolean
        method: 绑定的事件名称 | string
      例:(不需要的属性可不加)
      tableConfig: [
        {
          field: 'name',
          label: '姓名',
          sortable: true,
          minWidth: 60
        },
        {
          field: 'sex',
          label: '年龄',
        },
        {
          label: '操作',
          slotName: 'operation',
        }
      ],
     */
    tableData: Array
  },
  methods: {
    selectionChange(selection) {
      this.$emit('selectionChange',selection)
    }
  }
}
</script>

父组件中的使用

<template>
	<MyTable 
	   :tableData="tableData" 
	   :tableConfig="tableConfig" 
	   :loading="loading" 
	   tableType="selection" >
	   <template v-slot:operation="slotData">
	   		<el-button type="primary" size="mini">添加</el-button>
	   		<el-button type="danger" size="mini">删除</el-button>
	   </template>
	</MyTable>
</template>

<script>
export default {
	data() {
	    return {
	    	loading: false,
	    	tableData:[{
	    		name:'张山',
	    		sex: 18
	    	}],
	    	tableConfig: [
	        {
	          field: 'name',
	          label: '姓名',
	          sortable: true,
	          minWidth: 60
	        },
	        {
	          field: 'sex',
	          label: '年龄',
	        },
	        {
	          label: '操作',
	          slotName: 'operation',
	        }
	      ],
	    }
	}
}
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值