element el-table的封装以及使用

6 篇文章 0 订阅
2 篇文章 0 订阅

1、封装table.vue组件

<template>
	<div>
		<el-table
			:data='tableData'>
				  <!--多选列-->
			     <el-table-column
	                        v-if='selectionShow'
	                        type="selection"
	                        width="50"
	                        align="center"
	                        highlight-current-row>
	             </el-table-column>
	             <!--自定义添加排序列-->
                <el-table-column
                        v-if='indexShow'
                        align="center"
                        :label="indexLabel"
                        :width="indexWidth">
                    <template slot-scope="scope">
                    	<span>{{scope.$index + 1}} </span>
                    </template>
                </el-table-column>
				<el-table-column
                        v-for="(item, index) in tableHead"
                        :header-align="item.headerAlign"
                        :align="item.align"
                        :key="index"
                        :fixed="item.fixed"
                        :width="ColumnWidth(item)"
                        :prop="item.prop"
                        :label="item.label"
                        :sortable="item.sortable" >
                        <div class="showTable" style="min-height: 20px;">
                                <div 
                                :title="scope.row[item.prop]"
                                 v-html="normalFormatter(item.prop,scope.row[item.prop],item)">{{scope.row[item.prop]}}</div>
                            </div>
               </el-table-column>        
		</el-table>
	</div>
</template>

<script>
	export default {
		props:{
			indexShow: Boolean, // 是否显示排序列
            selectionShow: Boolean, // 是否显示多选列
            indexLabel: null, // 排序列名称
            indexWidth: null, // 排序列宽度
            tableHead: Array,  // 表头数据
            tableData: Array, // 表格数据
		},
		data(){
			return{
				
			}
		},
		 watch: {
		    // 监听表格数据
		 	tableData: {
		 		handler(val, oldVal) {
		 			this.tableData= val
		 		}
		 	},
		 	// 监听表头数据
		 	tableHead: {
		 		handler(val, oldVal) {
		 			this.tableHead= val
		 		}
		 	}
		 }

	}
</script>

2、使用table.vue组件
在新建一个tableDemo.vue文件使用,table.vue组件

<template>
	<div>
		<new-table 
			:tableData="tableData"
            :tableHead="tableHead" 
             :selectionShow=true
             :indexShow=true
             indexLabel="序号"
             indexWidth="50">
		</new-table>
	</div>
</template>
<script>
	export default {
		data(){
			return{
				// 表头数据
                tableHead:[
                    {
                        width: 150,
                        label: '日期',
                        prop: 'date',
                        align: 'right',   // 列对齐方式
                        headerAlign:'center',   // 列头对其方式
                        fixed: false,  // 列是否固定
                        filterShow: true,  //  是否开启过滤
                        sortable: true, //是否开启排序
                    },
                    {
                        width: 150,
                        label: '姓名',
                        prop: 'name',
                        align: 'left',
                        headerAlign:'center',
                        fixed: false,
                        filterShow: true,
                        sortable: true
                    },
                    {
                        width: 250,
                        label: '地址',
                        prop: 'address',
                        align: 'left',
                        headerAlign:'center',
                        fixed: false,
                        sortable: true
                    }
                  }
                ],
                // 表格基础数据
                tableData:[
                	{
			            date: '2016-05-02',
			            name: '王小虎',
			            address: '上海市普陀区金沙江路 1518 弄'
			          }, {
			            date: '2016-05-04',
			            name: '王小虎',
			            address: '上海市普陀区金沙江路 1517 弄'
			          }, {
			            date: '2016-05-01',
			            name: '王小虎',
			            address: '上海市普陀区金沙江路 1519 弄'
			          },
                ],
			}
		}
	}
</script>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值