• 前端开发攻略---合并表格单元格,表格内嵌套表格实现手风琴效果。


    1、演示

    2、思路

    1、用传统的

    表格标签来实现比较麻烦。因此通过模拟  表格标签 的写法用
    来实现

    2、表头和表格列数是相同的,因此可以确定代码结构

    1. <div class="table">
    2. <div class="header">
    3. <div class="th">div>
    4. <div class="th">div>
    5. <div class="th">div>
    6. <div class="th">div>
    7. <div class="th">div>
    8. div>
    9. <div class="body">
    10. <div class="tr">
    11. <div class="td">div>
    12. <div class="td">div>
    13. <div class="td">div>
    14. <div class="td">div>
    15. <div class="td">div>
    16. div>
    17. div>
    18. div>

    3、上述代码表示为一个一行五列的表格

    4、通过flex进行布局

    5、通过数组的长度来平分嵌套表格每一列的宽度/高度

    3、全部代码

    1. <template>
    2. <div class="table">
    3. <div class="header">
    4. <div class="th">Iddiv>
    5. <div class="th">名字div>
    6. <div class="th">年龄div>
    7. <div class="th">朋友div>
    8. <div class="th">性别div>
    9. div>
    10. <div class="body">
    11. <div class="tr" v-for="(item, index) in data">
    12. <div class="td">{{ item.id }}div>
    13. <div class="td">{{ item.name }}div>
    14. <div class="td">{{ item.age }}div>
    15. <div class="td" style="flex-direction: column">
    16. <p @click="item.hide = !item.hide">展开详情p>
    17. <div
    18. class="content"
    19. :style="{ height: item.hide ? '0px' : `${item.detail.length * 36}px` }"
    20. :class="item.hide ? '' : 'haveTopBorder'"
    21. :key="index"
    22. >
    23. <div class="content-row" v-for="item1 in item.detail">
    24. <div class="content-td" v-for="item2 in item1" :style="{ '--l': item1.length }">{{ item2 }}div>
    25. div>
    26. div>
    27. div>
    28. <div class="td">div>
    29. div>
    30. div>
    31. div>
    32. template>
    33. <script setup>
    34. import { ref, reactive } from 'vue'
    35. const data = ref([
    36. {
    37. id: 1,
    38. name: '刘备',
    39. age: '18',
    40. detail: [
    41. ['1111', '1111', '', '1111', '1111', '1111', '1111', '1111', '1111', '1111', '1111'],
    42. ['1111', '1111', '', '1111', '1111', '1111', '1111', '1111', '1111', '1111', '1111'],
    43. ['1111', '1111', '', '1111', '1111', '1111', '1111', '1111', '1111', '1111', '1111'],
    44. ['1111', '1111', '', '1111', '1111', '1111', '1111', '1111', '1111', '1111', '1111'],
    45. ],
    46. status: '男',
    47. hide: false,
    48. },
    49. {
    50. id: 2,
    51. name: '张飞',
    52. age: '50',
    53. detail: [
    54. ['1111', '1111', '1111', '1111', '1111', '1111', '1111', '1111', '1111', '', '1111'],
    55. ['1111', '1111', '1111', '1111', '1111', '1111', '1111', '1111', '1111', '1111', '1111'],
    56. ],
    57. status: '男',
    58. hide: false,
    59. },
    60. {
    61. id: 3,
    62. name: '关羽',
    63. age: '29',
    64. detail: [
    65. ['', '1111', '1111', '1111', '1111', '1111', '', '1111', '1111', '', '1111'],
    66. ['1111', '1111', '', '1111', '1111', '1111', '1111', '1111', '1111', '1111', ''],
    67. ['1111', '', '1111', '1111', '', '1111', '1111', '1111', '1111', '1111', '1111'],
    68. ['1111', '', '1111', '1111', '', '1111', '1111', '1111', '1111', '1111', '1111'],
    69. ['1111', '', '1111', '1111', '', '1111', '1111', '1111', '1111', '1111', '1111'],
    70. ['1111', '', '1111', '1111', '', '1111', '1111', '1111', '1111', '1111', '1111'],
    71. ['1111', '', '1111', '1111', '', '1111', '1111', '1111', '1111', '1111', '1111'],
    72. ['1111', '', '1111', '1111', '', '1111', '1111', '1111', '1111', '1111', '1111'],
    73. ],
    74. status: '男',
    75. hide: false,
    76. },
    77. ])
    78. script>
    79. <style scoped lang="scss">
    80. * {
    81. box-sizing: border-box;
    82. margin: 0;
    83. padding: 0;
    84. }
    85. .table {
    86. width: 1000px;
    87. border: 1px solid #eee;
    88. font-size: 14px;
    89. .header {
    90. display: flex;
    91. justify-content: space-between;
    92. align-items: stretch;
    93. background-color: #b6bece;
    94. color: #3c3c3c;
    95. padding: 8px 0;
    96. .th {
    97. padding-left: 8px;
    98. }
    99. }
    100. .body {
    101. width: 100%;
    102. .tr {
    103. display: flex;
    104. width: 100%;
    105. justify-content: space-between;
    106. align-items: stretch;
    107. border-bottom: 1px solid #eee;
    108. .td {
    109. border-right: 1px solid #eee;
    110. padding: 8px 4px;
    111. display: flex;
    112. justify-content: flex-start;
    113. align-items: center;
    114. }
    115. .td:last-child {
    116. border-right: 0;
    117. }
    118. }
    119. .tr:last-child {
    120. border-bottom: 0;
    121. }
    122. p {
    123. width: 100%;
    124. display: flex;
    125. align-items: center;
    126. text-align: left;
    127. padding: 8px 0;
    128. cursor: pointer;
    129. user-select: none;
    130. }
    131. }
    132. .th,
    133. .td {
    134. text-align: left;
    135. }
    136. .th:nth-child(1),
    137. .td:nth-child(1) {
    138. width: 70px;
    139. }
    140. .th:nth-child(2),
    141. .td:nth-child(2) {
    142. width: 100px;
    143. }
    144. .th:nth-child(3),
    145. .td:nth-child(3) {
    146. width: 130px;
    147. }
    148. .th:nth-child(4),
    149. .td:nth-child(4) {
    150. flex: 1;
    151. padding: 0 !important;
    152. }
    153. .th:nth-child(5),
    154. .td:nth-child(5) {
    155. width: 70px;
    156. }
    157. .content {
    158. width: 100%;
    159. overflow: hidden;
    160. transition: height 0.2s;
    161. .content-row {
    162. display: flex;
    163. width: 100%;
    164. border-bottom: 1px solid #eee;
    165. .content-td {
    166. padding: 8px;
    167. width: calc(100% / var(--l));
    168. border-right: 1px solid #eee;
    169. }
    170. .content-td:last-child {
    171. border-right: 0;
    172. }
    173. }
    174. .content-row:last-child {
    175. border-bottom: 0;
    176. }
    177. .content-row:nth-child(even) {
    178. background-color: rgb(116, 182, 218);
    179. }
    180. }
    181. .haveTopBorder {
    182. border-top: 1px solid #eee;
    183. }
    184. }
    185. style>

    4、温馨提示

    您可以找个干净的页面直接整个复制,根据您的需求更改即可

  • 相关阅读:
    数据采集与预处理课设——人在回路的气温数据动态处理与可视化
    SpringMVC-1-Hello SpringMVC
    ES 客户端 RestHighLevelClient Connection reset by peer 亲测有效 2022-11-05
    Springboot的Controller中的参数接收以及@Mapper和@Repository的区别
    ceval 数据集明文位置编码嵌入
    Android三种数据存储的方式
    深度解读中国DevOps现状调查报告(2022)
    【Java 数据结构及算法实战】系列 017:HJ3 明明的随机数
    【Tailwind】footer固定底部
    在Ubuntu20.04安装StarRocks On Docker并在DataGrip配置JDBC协议连接容器内StarRocks2.3.2
  • 原文地址:https://blog.csdn.net/nibabaoo/article/details/137904765