element-UI el-table二次封装

Part.1 为什么要二次封装?

这是 Element 网站的 table 示例:

  <template>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="日期" width="180"></el-table-column>
      <el-table-column prop="name" label="姓名" width="180"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
    </el-table>
  </template>

  <script>
    export default {
      data() {
        return {
          tableData: [{
            date: '2016-05-02',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
            date: '2016-05-04',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1517 弄'
          }, {
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1519 弄'
          }, {
            date: '2016-05-03',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1516 弄'
          }]
        }
      }
    }
  </script>

上面的表格字段较少,代码数量不多,但是当我们在开发项目的时候,可能表格字段很多并且多处用到表格,那我们的代码量就会非常多而且冗杂,所以我们需要进行二次封装,从而精简代码量

Part.2 遇到的问题

按照暂时项目所需进行的二次封装,遇到的问题如下

1. 为表格添加序号列时,翻页操作发现每一页的序号都是从1开始

2. 操作列如何封装/需要给某一个列自定义怎么办?

Part.3 解决

问题一 可参考:https://www.cnblogs.com/langxiyu/p/10641060.html

问题二 关于操作列/自定义列我使用了 slot  , 具体实现如下:

封装:

<template>
    <div class="data-table">
        <el-table
             :data="tableData"
             style="width: 100%"
             border
             v-loading="loading">
            <el-table-column
                    label="序号"
                    type="index"
                    width="50"
                    align="center">
                <template scope="scope">
                    <!-- 有分页时,序号显示 -->
                    <span v-if="pageObj">{{(pageObj.currentPage - 1)*pageObj.size + scope.$index + 1}}</span>
                    <!-- 无分页时,序号显示 -->
                    <span v-else>{{scope.$index + 1}}</span>
                </template>
            </el-table-column>
            <template v-for="(col, index) in columns">
                <!-- 操作列/自定义列 -->
                <slot v-if="col.slot" :name="col.slot"></slot>
                <!-- 普通列 -->
                <el-table-column v-else
                                 :key="index"
                                 :prop="col.prop"
                                 :label="col.label"
                                 :width="col.width"
                                 :formatter="col.formatter"
                                 align="center">
                </el-table-column>
            </template>
        </el-table>
        <!-- 是否调用分页 -->
        <el-pagination v-if="pageObj" background
                    layout="total, prev, pager, next, jumper"
                    :page-size="pageObj.size"
                    :total="pageObj.total"
                    :current-page="pageObj.currentPage"
                    @current-change="pageObj.func">
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "dataTable",
        props: ['tableData', 'columns', 'loading', 'pageObj']
    }
</script>

调用:

HTML

       <lxy-table :tableData="tableData" :columns="columns" :loading="loading" :pageObj="pageObj">
            <el-table-column slot="operate" label="操作" align="center" fixed="right" width="300">
                <template slot-scope="scope">
                    <el-button size="small" type="warning"
                               icon='el-icon-edit'
                               @click="edit(scope.row)">编辑
                    </el-button>
                    <el-button size="small" type="primary"
                               icon='el-icon-success'
                               @click="startUsing(scope.row)">启用
                    </el-button>          
                </template>
            </el-table-column>
        </lxy-table>

JS

 tableData:[],
 columns: [
      {label: '名称', prop: 'adName'},
      {label: '链接', prop: 'adUrl'},
      {label: '排序', prop: 'sort'},
      {slot: 'operate'}], // 操作列
 loading: true,
 pageObj: {
     size: 10,
     total: 1,
     currentPage: 1,
     func:(currentPage) =>{
        this.pageTurning(currentPage)
     }
 },

二次封装解决,这样执行每个需要调用表格的地方代码可操作性更强,代码更加清晰明了!当然更多表格功能可自行再次添加~~~

 

转载于:https://www.cnblogs.com/langxiyu/p/10641822.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue基于Element UI Table的二次封装可以通过创建一个自定义的组件来实现。以下是一个简单的示例,演示了如何封装一个基于Element UI Table的组件: ```vue <template> <el-table :data="tableData" :row-key="rowKey" :height="height"> <!-- 渲染表头 --> <el-table-column v-for="column in columns" :key="column.prop" :prop="column.prop" :label="column.label"> <!-- 自定义插槽 --> <template slot-scope="scope"> <slot :column="column" :scope="scope"></slot> </template> </el-table-column> </el-table> </template> <script> export default { name: 'CustomTable', props: { tableData: { type: Array, required: true }, columns: { type: Array, required: true }, rowKey: { type: String, required: true }, height: { type: String, default: 'auto' } } } </script> ``` 在这个示例中,我们创建了一个名为`CustomTable`的组件。它接受`tableData`、`columns`、`rowKey`和`height`作为props,分别表示表格数据、表格列配置、行数据的唯一标识以及表格的高度。 在模板中,我们使用`el-table`和`el-table-column`来渲染Element UI的表格。我们使用了`v-for`指令来循环渲染表格列,并通过`slot-scope`来传递数据给插槽。插槽可以在父组件中定义,并在插槽中使用自定义的组件来渲染表格单元格内容。 通过这种方式,我们可以在父组件中使用这个封装的自定义表格组件,并通过插槽来定制表格的内容和样式。 希望这个简单的示例能帮助到你进行Vue基于Element UI Table的二次封装。如果有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值