Vue2基于Element-ui中的el-table组件进行二次封装(减少代码提高复用,扩展API)
提示:请先看第二步中的组件说明,和组件封装中其他扩展的api,的注释都在html中对应的代码结构都有主要依靠JSON数据动态执行和回调
特点:通过JSON数据动态渲染列表标题和数据列表,JSON数据驱动事件监听和回调以及数据过滤,和满足更多自定义需求组合
效果图:
下面这种模式可以点击图片,可以是一张图片,也可以数组中多个图片,点击图片可以预览切换
1.页面调用(记得进行引入组件后在调用)
:operation="operation" :total="total" headerAlign='center' @handleNumberChange="handleNumberChange" @sortChange="sortChange" @select="select" :headButton="true" @handleCurrentPage="handleCurrentPage"> 导出
import tableList from '@/components/tableList' components:{
tableList,
}, data(){ return{ tableData: [],//数据源 tableLabel:[ { prop: 'nickName', label: '用户昵称', align: 'center', }, { prop: 'friendCount', label: '好友数量', align: 'center', }, ], operation:{ width: '220', Button:[ { label: '查看', size: "mini", authority: false, callback(k) { console.log(k) } } ] }, total:10, } } , methods:{ handleNumberChange(val){},// 每一页展示多少条切换 handleCurrentPage(val){},//切换分页 sortChange(column){},// 排序 select(valArr){},//列表复选框 }
2.封装组件核心代码
if="headButton">
:highlight-current-row="true" @select-all="select"
:header-cell-style="{'text-align':headerAlign,'background':'#eef1f6'}" @current-change="handleCurrentChange"
:max-height="fixedHeight>0?fixedHeight:tableHeight" :ref="Math.random()">
if="choice" width="50" :align="headerAlign" :key="Math.random()">
if="serialNumber" width="50" type="index" :align="headerAlign"
:key="Math.random()">
{{searchData.limit * (searchData.page - 1) + (scope.$index + 1)}}
for="(item, index) in tableLabel" :key="index">
if="!item.form || item.form ==='string'" :prop="item.prop" :width="item.width"
:label="item.label" :sortable="item.sort" :align="item.align?item.align:'left'" :key="index"
show-overflow-tooltip>
else-if="item.form === 'filter'" :width="item.width" :label="item.label"
:sortable="item.sort" :align="item.align?item.align:'left'" :key="index" show-overflow-tooltip>
{{item.filter(scope.row)}}
else-if="item.form === 'img'" :width="item.width" :label="item.label"
:sortable="item.sort" :align="item.align?item.align:'left'" :key="index">
if="typeof scope.row[item.prop] === 'string'">
:src="scope.row[item.prop]?scope.row[item.prop]:defaultImg"
:preview-src-list="[scope.row[item.prop]?scope.row[item.prop]:defaultImg]">
else-if="scope.row[item.prop]!==null&& scope.row[item.prop].length>0">
:preview-src-list="scope.row[item.prop]" :z-index='2100'>
else-if="item.form === 'Button'" :width="item.width" :label="item.label"
:sortable="item.sort" :align="item.align?item.align:'left'" :key="index">
:type="item.configure&&item.configure.type?item.configure.type:''"
:size="item.configure&&item.configure.size?item.configure.size:''">
{{item.configure.text||'缺少按钮文字'}}
else-if="item.form === 'checkbox'" :width="item.width" :label="item.label"
:sortable="item.sort" :align="item.align?item.align:'left'" :key="index">
for="(v,i) in scope.row.authority" :key="i">
if="scope.row.authority&&scope.row.authority.length<1">暂无
if="JSON.stringify(operation)!== '{}'">
for="(item,index) in operation.Button" :key="index">
if="!item.authority"
@click="item.callback(Object.assign({},scope.row))" :type="item.type?item.type:''"
:size="item.size?item.size:''" :disabled="item.disabled&&item.disabled(scope.row)"
:plain="plain">{{item.label}}
v-else-if="item.authority && item.authorityEvent&& item.authorityEvent(item.label)"
@click="item.callback(Object.assign({},scope.row))" :type="item.type?item.type:''"
:size="item.size?item.size:''" :disabled="item.disabled&&item.disabled(scope.row)"
:plain="plain">{{item.label}}
if="paging" :style="{'text-align':pagingPosition}">
:current-page="searchData.page" :page-sizes="[10,15, 20, 30, 40,50]" :page-size="searchData.limit"
layout="total, sizes, prev, pager, next, jumper" :total="total">
我是马丁的车夫,欢迎转发收藏!