当前位置:   article > 正文

移动端 vue table 组件简单封装_移动端table组件

移动端table组件

1、背景:移动端封装table组件,组件分为三部分:tableHeader、tableBody、index具体效果图如下:

2、tableHeader.vue页面代码:

  1. <template>
  2. <thead>
  3. <tr>
  4. <th
  5. v-for="item in columns"
  6. :width="item.width"
  7. :align="item.align || 'left'"
  8. :key="item.prop"
  9. :style="{
  10. 'min-width': `${item['min-width']}`,
  11. 'max-width': `${item['max-width']}`
  12. }"
  13. v-html="item.label"
  14. ></th>
  15. </tr>
  16. </thead>
  17. </template>
  18. <script>
  19. export default {
  20. name: 'tableHeader',
  21. props: {
  22. columns: {
  23. type: Array,
  24. default: () => []
  25. }
  26. },
  27. methods: {
  28. renderToHtml(col) {
  29. if (typeof col.render === 'function') {
  30. this.$slots[col.prop] = [col.render(row)];
  31. return;
  32. }
  33. return;
  34. }
  35. }
  36. };
  37. </script>
  38. <style scoped>
  39. th {
  40. font-weight: bold;
  41. padding: 0.09rem 0.02rem;
  42. background: #f7f9f9;
  43. color: #8695a1;
  44. word-wrap: break-word;
  45. word-break: break-all;
  46. }
  47. </style>

3、tableBody页面代码:

  1. <template>
  2. <tbody>
  3. <tr
  4. v-for="(item, index) in data"
  5. :key="index"
  6. :class="[stripe && 'stripe', rowClassName]"
  7. >
  8. <td
  9. v-for="(cItem, cIndex) in columns"
  10. :width="cItem.width"
  11. :align="cItem.align"
  12. :key="cIndex"
  13. :style="{
  14. 'min-width': cItem['min-width'],
  15. 'max-width': cItem['max-width']
  16. }"
  17. >{{ item[cItem.prop] }}</td
  18. >
  19. </tr>
  20. </tbody>
  21. </template>
  22. <script>
  23. export default {
  24. name: 'tableBody',
  25. props: {
  26. columns: {
  27. type: Array,
  28. default: () => []
  29. },
  30. data: {
  31. type: Array,
  32. default: () => []
  33. },
  34. stripe: {
  35. type: Boolean,
  36. default: false
  37. },
  38. rowClassName: {
  39. type: String,
  40. default: ''
  41. }
  42. }
  43. };
  44. </script>
  45. <style scoped>
  46. .stripe:nth-child(even) {
  47. background-color: #f8faff;
  48. }
  49. td {
  50. padding: 0.13rem 0.02rem;
  51. border-bottom: 0.01rem solid #e8edf0;
  52. color: #253440;
  53. word-wrap: break-word;
  54. word-break: break-all;
  55. }
  56. </style>

4、index.vue页面代码

  1. <template>
  2. <div class="table">
  3. <table>
  4. <table-header
  5. v-if="showHeader"
  6. ref="tableHeader"
  7. :columns="columns"
  8. ></table-header>
  9. <table-body
  10. v-if="data.length > 0"
  11. ref="bodyWrapper"
  12. :stripe="stripe"
  13. :row-class-name="rowClassName"
  14. :row-style="rowStyle"
  15. :style="{
  16. width: bodyWidth
  17. }"
  18. :columns="columns"
  19. :data="data"
  20. >
  21. </table-body>
  22. </table>
  23. <p v-if="data.length == 0" class="empty-data">暂无数据</p>
  24. </div>
  25. </template>
  26. <script>
  27. import TableHeader from './tableHeader.vue';
  28. import TableBody from './tableBody.vue';
  29. export default {
  30. name: 'yssTable',
  31. components: {
  32. TableHeader,
  33. TableBody
  34. },
  35. props: {
  36. showHeader: {
  37. type: Boolean,
  38. default: true
  39. },
  40. columns: {
  41. type: Array,
  42. default: () => []
  43. },
  44. data: {
  45. type: Array,
  46. default: () => []
  47. },
  48. stripe: {
  49. type: Boolean,
  50. default: false
  51. },
  52. rowClassName: {
  53. type: String,
  54. default: ''
  55. },
  56. rowStyle: {
  57. type: Object,
  58. default: () => {}
  59. },
  60. bodyWidth: {
  61. type: Number,
  62. default: null
  63. },
  64. emptyBlockStyle: {
  65. type: Object,
  66. default: () => {}
  67. },
  68. emptyText: {
  69. type: String,
  70. default: ''
  71. }
  72. }
  73. };
  74. </script>
  75. <style scoped>
  76. table {
  77. width: 100%;
  78. border-collapse: collapse;
  79. font-size: 0.14rem;
  80. }
  81. .empty-data {
  82. text-align: center;
  83. font-size: 14px;
  84. color: #666;
  85. margin-top: 15px;
  86. }
  87. </style>

5、使用

  1. <template>
  2. <div class="table-demo">
  3. <table-demo :columns="columns" :data="tableData"></table-demo>
  4. </div>
  5. </template>
  6. <script>
  7. import TableDemo from './tableDemo';
  8. export default {
  9. name: 'tableDemo',
  10. components: {
  11. TableDemo
  12. },
  13. data() {
  14. return {
  15. columns: [
  16. {
  17. prop: 'name',
  18. label: '姓名',
  19. width: '30%',
  20. 'min-width': '0.6rem'
  21. },
  22. {
  23. prop: 'age',
  24. label: '年龄',
  25. align: 'right',
  26. width: '20%',
  27. 'min-width': '0.6rem'
  28. },
  29. {
  30. prop: 'high',
  31. label: '<span>身高<br>(cm)</span>',
  32. align: 'right',
  33. width: '30%',
  34. 'min-width': '0.7rem'
  35. },
  36. {
  37. prop: 'gender',
  38. label: '性别',
  39. align: 'right',
  40. width: '20%',
  41. 'min-width': '0.7rem'
  42. }
  43. ],
  44. tableData: []
  45. };
  46. }
  47. };
  48. </script>
  49. <style scoped>
  50. .table-demo {
  51. background-color: #fff;
  52. padding: 15px;
  53. }
  54. </style>

思考:习惯了UI库的使用,渐渐对组件背后的东西缺乏探索,在写table组件的时候,发现经常使用的组件,其实写起来并不容易,这激起了一丢丢对UI库源码的好奇心,希望我能将这份好奇心付之行动,也希望下次再进行分享时,可以是对某个UI组件源码学习的分享。

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