首页 > 其他分享 >el-table封装

el-table封装

时间:2023-06-09 11:34:17浏览次数:28  
标签:el 封装 val default return table type page

1、新建Pagination.vue文件

<template>
  <div :class="{'hidden':hidden}" class="pagination-container">
    <el-pagination
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
 
<script>
 
export default {
  name: 'Pagination',
  props: {
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 10
    },
    pageSizes: {
      type: Array,
      default () {
        return [10, 20, 30, 50]
      }
    },
    layout: {
      type: String,
      default: 'total, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    currentPage: {
      get () {
        return this.page
      },
      set (val) {
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get () {
        return this.limit
      },
      set (val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    handleSizeChange (val) {
      this.$emit('pagination', { page: this.currentPage, limit: val })
    },
    handleCurrentChange (val) {
      this.$emit('pagination', { page: val, limit: this.pageSize })
    }
  }
}
</script>
 
<style lang="scss" scoped>
.pagination-container {
  background: #fff;
  .el-pagination {
      text-align: right;
      .btn-prev, .btn-next {
          color: #FF5F17;
      }
      span:not([class*=suffix]), button {
          color: #000000;
          font-size: 12px;
          padding: 0;
      }
      button:hover {
          color: #FF5F17;
          background: rgba(255, 95, 23, 0.15);
          border-radius: 8px;
          min-width: 26px;
          height: 26px;
          margin: 0px 5px;
      }
      .btn-prev, .btn-next {
          padding: 0;
      }
      .el-input--medium .el-input__inner {
          width: 32px;
          height: 22px;
          border-radius: 6px;
          border: 1px solid #FF5F17 !important;
          padding: 0 !important;
      }
  }
  .el-pager li {
      color: #C4CED7;
  }
  .el-pager li.active {
      color: #FFFFFF;
      background-color: #000000;
      height: 26px;
      min-width: 26px;
      min-height: 26px;
      line-height: 26px;
      cursor: default;
      border-radius: 8px;
  }
  .el-pager li:hover {
      color: #FF5F17 ;
  }
}
.pagination-container.hidden {
  display: none;
}
 
</style>

2、然后再创建一个Table.vue。代码如下

<template>
    <div class="zcm__table">
        <el-table :data="tableData" @cell-click="cellClick" :border="border">
            <el-table-column
                v-if="index"
                type="index"
                label="序号"
                align="center"
                width="50">
                <template slot-scope="scope">
                    <span>{{ scope.$index + (page.current - 1) * page.size + 1 }}</span>
                </template>
            </el-table-column>
            <template v-for="(item, index) in tableOption">
                <el-table-column
                    :key="index"
                    :prop="item.prop"
                    :label="item.label"
                    :align="item.align || 'center'"
                    :show-overflow-tooltip="item.overHidden || true">
                    <template slot-scope="scope">
                        <slot
                        v-if="item.slot"
                        :name="scope.column.property"
                        :row="scope.row"
                        :$index="scope.$index"
                        />
                        <span v-else>{{ scope.row[scope.column.property] }}</span>
                    </template>
                </el-table-column>
            </template>
            <el-table-column
              v-if="operation"
              label="操作"
              align="center">
              <template slot-scope="scope">
                  <slot
                      name="menu"
                      :row="scope.row"
                      :$index="scope.$index"
                      />
              </template>
            </el-table-column>
        </el-table>
        <Pagination
            v-show="page.total>0"
            :total="page.total"
            :page.sync="page.current"
            :limit.sync="page.size"
            @pagination="pageChange"
            />
    </div>
</template>
 
<script>
import Pagination from '../Pagination'
export default {
  name: 'Table',
  components: {
    Pagination
  },
  props: {
    index: {
      type: Boolean,
      default: function () {
        return true
      }
    }, // 是否设置序号,默认设置
    border: {
      type: Boolean,
      default: function () {
        return false
      }
    }, // 是否设置边框,默认不要
    operation: {
      type: Boolean,
      default: function () {
        return false
      }
    }, // 是否有操作列,默认无
    tableData: {
      type: Array,
      default: function () {
        return []
      }
    }, // 列表数据
    tableOption: {
      type: Array,
      default: function () {
        return []
      }
    }, // 表头
    page: {
      type: Object,
      default: function () {
        return {
          total: 0,
          current: 1,
          page: 10
        }
      }
    } // 分页
  },
  methods: {
    pageChange () {
      this.$emit('page-change')
    },
    cellClick (row, column, cell, event) {
      this.$emit('cell-click', { row, column, cell, event })
    }
  }
}
</script>

3、然后直接在页面中调用即可

<template>
  <div class="container">
    <Table
      :tableData="tableData"
      :page="page"
      :tableOption.sync="tableOption"
      @page-change="getList"
      @cell-click="cellClick">
        <template slot="ranks" slot-scope="scope">
            <el-tag>{{ scope.row.name}}</el-tag>
        </template>
        <template slot="menu" slot-scope="scope">
            <el-button type="text" size="mini" icon="el-icon-delete"                  
                @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
    </Table>
  </div>
</template>
 
<script>
import Table from '@/components/table'
export default {
  components: {
    Table
  },
  data () {
    return {
      tableOption: [
        { label: '姓名', prop: 'name' },
        { label: '性别', prop: 'sex' },
        { label: '身份证号', prop: 'idNumber' },
        { label: '职务', prop: 'duty' },
        { label: '职级', prop: 'ranks', solt: true }/ / 这里表示自定义列
      ],
      page: {
        total: 0,
        current: 1,
        size: 10
      },
      tableData: []
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {}, // 数据获取
    cellClick (e) {},
    deleteHandle()
  }
}
</script>
 
<style lang="scss" scoped></style>

 

标签:el,封装,val,default,return,table,type,page
From: https://www.cnblogs.com/zcm1688/p/17468720.html

相关文章

  • OpenHarmony 3.2 Release新特性解读之驱动HCS
    OpenAtomOpenHarmony(以下简称“OpenHarmony”)开源社区,在今年4月正式发布了OpenHarmony3.2Release版本,标准系统能力进一步完善,提升了系统的整体性能、稳定性和安全性。此次版本对于驱动也提供了一些新的特性,极大的方便了驱动开发工作。其中针对HCS文件开发提供新的开发思路,本文就......
  • Oracle重建data pump(expdpd,impdp)How To Reload Datapump Utility EXPDP/IMPDP (Doc ID
    APPLIESTO:OracleDatabaseExadataExpressCloudService-VersionN/AandlaterOracleDatabaseBackupService-VersionN/AandlaterOracleDatabase-EnterpriseEdition-Version10.1.0.2andlaterOracleDatabaseCloudSchemaService-VersionN/Aand......
  • [ABC166E] This Message Will Self-Destruct in 5s
    ThisMessageWillSelf-Destructin5sの传送门Solution首先看到\(j-i=A_i+A_j\)转换一下,\(i+a_i=j-a_j\)。接下来,对于每一个\(i\)(\(1\lei\len\)),用一个map存\(i-a_i\)的数量。最后枚举\(i\)(\(1\lei\len\)),每次将\(ans\)加上\(i+a_i\)在map里的数......
  • Java利用xml将大批量数据导出到excel的一个方法
    笔者在Java开发中常常会遇到将数据库数据导出到Excel的要求,比如在我的一个项目中,客户要求所有查询结果都可以导出到Excel,对于数据量不大的(几万条),这比较容易实现,但对于数据量比较大的(几十万及以上,具体要看导出内容的字段个数和长短),则在数据库查询和生成excel文件上都会有麻烦。......
  • Elasticsearch 修改字段类型
    由于ES不能像关系型数据库直接修改类型,所以要借助中间索引来完成1、创建中间索引(字段维护正确的)put:https://localhost/track_bak{"settings":{"index":{"number_of_shards":"3","number_of_replicas":"1&quo......
  • Linux Shell 脚本创建日期文件夹
    mk-time-dir.sh#!/bin/bashdir_name=$(date'+%Y%m%d%H%M')mkdir$dir_nameecho"已创建文件夹${dir_name}"#已创建文件夹202306090936参考:Linuxdate命令......
  • 自定义系统级无窗口全局快捷键热键-Delphi7_Lite_Full_Edition_Setup_7.3.4.3_Build_2
      自定义系统级无窗口全局快捷键热键-Delphi7_Lite_Full_Edition_Setup_7.3.4.3_Build_20110801-2023年6月9日 programProject1_SetHotkeyBaiduSyncDisk;usesForms,Unit1_SetHotkeyBaiduSyncDiskin'Unit1_SetHotkeyBaiduSyncDisk.pas'{Form1};{$R*.res}b......
  • Field Reduction USACO - 641
    题目链接:http://www.usaco.org/index.php?page=viewproblem2&cpid=641&lang=en题意:有n(3<n<50000)头牛你需要给这n头牛建造围栏。坐标范围1-40,000。围栏的面积越小越好。你需要删除1头牛来减小围栏面积思路:1.因为只能删除1头牛来减少围栏面积,所以只能删除最靠边缘的牛,否则......
  • spring中获取属性的方法SpelExpressionParser()
    Aa=newA();Bb=newB();声明el表达式ExpressionParserpaeser=newSpelExpressionParser();设置你要通过el表达式取的字段Expressionaid=paeser.parseExpression("id");代表内置对象(b)中的id属性Expressionbid=paeser.parseExpression(b.id);如果b对象......
  • C#将DataTable中的某列转换成数组或者List
    DataTableThisDT_Time=useAS.GetDataTableSQL("selectDateTimefromCurveData1");//获取表内容object[]DTTA=ThisDT_Time.AsEnumerable().Select(v=>v.Field<object>("DataTime")).ToArray();//DataTime为转化列的名称List<object>DTTL......