当前位置:   article > 正文

el-table封装

el-table封装

        向后台管理一类的系统,基本都是表格,使用el-table时,不是在复制就是再复制的路上。所以咱们把el-table封装起来,这样页面看起来就更加整洁了。

        在封装el-table之前先写一个分页器的组件Pagination.vue吧

  1. <template>
  2. <div :class="{'hidden':hidden}" class="pagination-container">
  3. <el-pagination
  4. :current-page.sync="currentPage"
  5. :page-size.sync="pageSize"
  6. :layout="layout"
  7. :page-sizes="pageSizes"
  8. :total="total"
  9. v-bind="$attrs"
  10. @size-change="handleSizeChange"
  11. @current-change="handleCurrentChange"
  12. />
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. name: 'Pagination',
  18. props: {
  19. total: {
  20. required: true,
  21. type: Number
  22. },
  23. page: {
  24. type: Number,
  25. default: 1
  26. },
  27. limit: {
  28. type: Number,
  29. default: 10
  30. },
  31. pageSizes: {
  32. type: Array,
  33. default () {
  34. return [10, 20, 30, 50]
  35. }
  36. },
  37. layout: {
  38. type: String,
  39. default: 'total, prev, pager, next, jumper'
  40. },
  41. background: {
  42. type: Boolean,
  43. default: true
  44. },
  45. autoScroll: {
  46. type: Boolean,
  47. default: true
  48. },
  49. hidden: {
  50. type: Boolean,
  51. default: false
  52. }
  53. },
  54. computed: {
  55. currentPage: {
  56. get () {
  57. return this.page
  58. },
  59. set (val) {
  60. this.$emit('update:page', val)
  61. }
  62. },
  63. pageSize: {
  64. get () {
  65. return this.limit
  66. },
  67. set (val) {
  68. this.$emit('update:limit', val)
  69. }
  70. }
  71. },
  72. methods: {
  73. handleSizeChange (val) {
  74. this.$emit('pagination', { page: this.currentPage, limit: val })
  75. },
  76. handleCurrentChange (val) {
  77. this.$emit('pagination', { page: val, limit: this.pageSize })
  78. }
  79. }
  80. }
  81. </script>
  82. <style lang="scss" scoped>
  83. .pagination-container {
  84. background: #fff;
  85. .el-pagination {
  86. text-align: right;
  87. .btn-prev, .btn-next {
  88. color: #FF5F17;
  89. }
  90. span:not([class*=suffix]), button {
  91. color: #000000;
  92. font-size: 12px;
  93. padding: 0;
  94. }
  95. button:hover {
  96. color: #FF5F17;
  97. background: rgba(255, 95, 23, 0.15);
  98. border-radius: 8px;
  99. min-width: 26px;
  100. height: 26px;
  101. margin: 0px 5px;
  102. }
  103. .btn-prev, .btn-next {
  104. padding: 0;
  105. }
  106. .el-input--medium .el-input__inner {
  107. width: 32px;
  108. height: 22px;
  109. border-radius: 6px;
  110. border: 1px solid #FF5F17 !important;
  111. padding: 0 !important;
  112. }
  113. }
  114. .el-pager li {
  115. color: #C4CED7;
  116. }
  117. .el-pager li.active {
  118. color: #FFFFFF;
  119. background-color: #000000;
  120. height: 26px;
  121. min-width: 26px;
  122. min-height: 26px;
  123. line-height: 26px;
  124. cursor: default;
  125. border-radius: 8px;
  126. }
  127. .el-pager li:hover {
  128. color: #FF5F17 ;
  129. }
  130. }
  131. .pagination-container.hidden {
  132. display: none;
  133. }
  134. </style>

        然后再创建一个ZtTable.vue。代码如下

  1. <template>
  2. <div class="zt__table">
  3. <el-table :data="tableData" @cell-click="cellClick" :border="border">
  4. <el-table-column
  5. v-if="index"
  6. type="index"
  7. label="序号"
  8. align="center"
  9. width="50">
  10. <template slot-scope="scope">
  11. <span>{{ scope.$index + (page.current - 1) * page.size + 1 }}</span>
  12. </template>
  13. </el-table-column>
  14. <template v-for="(item, index) in tableOption">
  15. <el-table-column
  16. :key="index"
  17. :prop="item.prop"
  18. :label="item.label"
  19. :align="item.align || 'center'"
  20. :show-overflow-tooltip="item.overHidden || true">
  21. <template slot-scope="scope">
  22. <slot
  23. v-if="item.slot"
  24. :name="scope.column.property"
  25. :row="scope.row"
  26. :$index="scope.$index"
  27. />
  28. <span v-else>{{ scope.row[scope.column.property] }}</span>
  29. </template>
  30. </el-table-column>
  31. </template>
  32. <el-table-column
  33. v-if="operation"
  34. label="操作"
  35. align="center">
  36. <template slot-scope="scope">
  37. <slot
  38. name="menu"
  39. :row="scope.row"
  40. :$index="scope.$index"
  41. />
  42. </template>
  43. </el-table-column>
  44. </el-table>
  45. <Pagination
  46. v-show="page.total>0"
  47. :total="page.total"
  48. :page.sync="page.current"
  49. :limit.sync="page.size"
  50. @pagination="pageChange"
  51. />
  52. </div>
  53. </template>
  54. <script>
  55. import Pagination from '../Pagination'
  56. export default {
  57. name: 'ZtTable',
  58. components: {
  59. Pagination
  60. },
  61. props: {
  62. index: {
  63. type: Boolean,
  64. default: function () {
  65. return true
  66. }
  67. }, // 是否设置序号,默认设置
  68. border: {
  69. type: Boolean,
  70. default: function () {
  71. return false
  72. }
  73. }, // 是否设置边框,默认不要
  74. operation: {
  75. type: Boolean,
  76. default: function () {
  77. return false
  78. }
  79. }, // 是否有操作列,默认无
  80. tableData: {
  81. type: Array,
  82. default: function () {
  83. return []
  84. }
  85. }, // 列表数据
  86. tableOption: {
  87. type: Array,
  88. default: function () {
  89. return []
  90. }
  91. }, // 表头
  92. page: {
  93. type: Object,
  94. default: function () {
  95. return {
  96. total: 0,
  97. current: 1,
  98. page: 10
  99. }
  100. }
  101. } // 分页
  102. },
  103. methods: {
  104. pageChange () {
  105. this.$emit('page-change')
  106. },
  107. cellClick (row, column, cell, event) {
  108. this.$emit('cell-click', { row, column, cell, event })
  109. }
  110. }
  111. }
  112. </script>

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

  1. <template>
  2. <div class="container">
  3. <zt-table
  4. :tableData="tableData"
  5. :page="page"
  6. :tableOption.sync="tableOption"
  7. @page-change="getList"
  8. @cell-click="cellClick">
  9. <template slot="ranks" slot-scope="scope">
  10. <el-tag>{{ scope.row.name}}</el-tag>
  11. </template>
  12. <template slot="menu" slot-scope="scope">
  13. <el-button type="text" size="mini" icon="el-icon-delete"
  14. @click="deleteHandle(scope.row.id)">删除</el-button>
  15. </template>
  16. </zt-table>
  17. </div>
  18. </template>
  19. <script>
  20. import ZtTable from '@/components/Zttable'
  21. export default {
  22. components: {
  23. ZtTable
  24. },
  25. data () {
  26. return {
  27. tableOption: [
  28. { label: '姓名', prop: 'name' },
  29. { label: '性别', prop: 'sex' },
  30. { label: '身份证号', prop: 'idNumber' },
  31. { label: '职务', prop: 'duty' },
  32. { label: '职级', prop: 'ranks', solt: true }/ / 这里表示自定义列
  33. ],
  34. page: {
  35. total: 0,
  36. current: 1,
  37. size: 10
  38. },
  39. tableData: []
  40. }
  41. },
  42. created () {
  43. this.getList()
  44. },
  45. methods: {
  46. getList () {}, // 数据获取
  47. cellClick (e) {},
  48. deleteHandle()
  49. }
  50. }
  51. </script>
  52. <style lang="scss" scoped></style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/621630
推荐阅读