vue 简单实用的elementUI表格封装

在写这个表格组件之前,要了解 slot 插槽的使用

目录

1.子组件:子组件调用父组件的方法 this.$parent.方法名

2.父组件使用

2.1 父组件(普通表格):

2.2 父组件(表格中的某一项数据需要修改) 

2.3 父组件(带操作列的表格):

注意:


1.子组件:子组件调用父组件的方法 this.$parent.方法名

<template>
  <div id="table"  class="bg pd20">
    <!-- 表格 -->
    <el-table :data="tableData" border class="myTable" :header-cell-style="{background:'#D9FFF2'}" style="width:100%">
      <!-- 表格扩展 -->
      <el-table-column type="expand" width="30">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand clear">
            <template >
              <el-form-item v-for="(info, infoId) in tableInfo" :key="infoId" :label="info.value+':'"><span>{
  {props.row[info.key]}}:</span></el-form-item>
            </template>
          </el-form>
        </template>
      </el-table-column>

      <!-- 表格内容 -->
      <el-table-column type="index" :index="indexMethod" width="60" label="序号" align="center"/>
      <template v-for="(info, infoIndex) in tableInfo">
        <el-table-column :key="infoIndex" :prop="info.key" :label="info.value" align="center" :min-width="info.minWidth" show-overflow-tooltip >
          <!-- <slot :name="info.key" :data="info" :index="infoIndex"></slot> -->
          <!-- 给插槽命名、传值、序列号,使用插槽时记得传dealData -->
          <template slot-scope="scope">
            <slot :name="info.key" :data="scope.row" :index="infoIndex" v-if="info.dealData"></slot>
            <span v-else>{
  {scope.row[info.key]}}</span>
          </template>
        </el-table-column>
      </template>       

      <!-- 是否需要操作列,默认不需要,需要时父组件设置operateShow,记得传操作这一列的宽度,否则默认宽为200 -->
      <el-table-column v-if="operateShow" label="操作" align="center" :min-width="operateWidth" fixed="right">
        <template slot-scope="scope">
   
以下是一个简单Vue 2 Element UI 表格二次封装的示例代码: ``` <template> <el-table :data="pageData" :default-sort="{prop: &#39;id&#39;, order: &#39;ascending&#39;}"> <el-table-column label="ID" prop="id" sortable /> <el-table-column label="姓名" prop="name" /> <el-table-column label="年龄" prop="age" /> <el-table-column label="性别" prop="gender" /> <el-table-column label="操作"> <template slot-scope="scope"> <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button> <el-button size="small" @click="handleDelete(scope.row)">删除</el-button> </template> </el-table-column> <el-pagination :current-page.sync="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" :total="tableData.length" @size-change="handleSizeChange" @current-change="handleCurrentChange" /> </el-table> </template> <script> export default { props: { tableData: { type: Array, required: true, }, pageSize: { type: Number, default: 10, }, }, data() { return { currentPage: 1, }; }, computed: { pageData() { const start = (this.currentPage - 1) * this.pageSize; const end = start + this.pageSize; return this.tableData.slice(start, end); }, }, methods: { handleEdit(row) { // 编辑操作 console.log(&#39;编辑&#39;, row); }, handleDelete(row) { // 删除操作 console.log(&#39;删除&#39;, row); }, handleSizeChange(pageSize) { this.pageSize = pageSize; }, handleCurrentChange(currentPage) { this.currentPage = currentPage; }, }, }; </script> ``` 此示例中,将表格数据每页显示的数据量通过 props 传入组件。使用计算属性 pageData 计算当前页应该显示的数据。在表格下方添加分页组件,使用 v-model 绑定 currentPage,使用 @size-change @current-change 监听分页组件的事件,调用对应的方法进行相应操作。同时,使用 Element UI 的 el-table-column 组件中的 sortable 属性可以实现列排序。 通过这样的封装,可以使表格组件更加通用、简洁,避免重复的代码编写。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值