• iview table 表格合并单元格


    一、如图所示

    二、实现方式

    表格用提供的span-method属性

    1. <template>
    2. <Table ref="table" border :span-method="handleSpan" :row-key="true" :columns="tableColumns" :data="tableData"
    3. no-data-text="暂无数据">
    4. Table>
    5. template>
    6. <script>
    7. export default {
    8. data() {
    9. return {
    10. tableColumns: [
    11. {
    12. title: '姓名',
    13. key: 'name',
    14. align: 'center',
    15. minWidth: 85,
    16. },
    17. {
    18. title: '年龄',
    19. key: 'age',
    20. align: 'center',
    21. minWidth: 85,
    22. },
    23. {
    24. title: '爱好',
    25. key: 'hobby',
    26. align: 'center',
    27. minWidth: 85,
    28. },
    29. {
    30. title: '等级',
    31. key: 'level',
    32. align: 'center',
    33. minWidth: 85,
    34. },
    35. {
    36. title: '年份',
    37. key: 'year',
    38. align: 'center',
    39. minWidth: 85,
    40. },
    41. {
    42. title: '地址',
    43. key: 'address',
    44. align: 'center',
    45. minWidth: 85,
    46. },
    47. {
    48. title: '电话',
    49. key: 'phone',
    50. align: 'center',
    51. minWidth: 85,
    52. },
    53. ],
    54. tableData: [],
    55. };
    56. },
    57. methods: {
    58. handleSpan({ row, column, rowIndex, columnIndex }) {
    59. // 爱好 【序号2】、等级【序号3】、年份【序号4】 是多行的
    60. if (columnIndex < 2 || columnIndex > 4) {
    61. // 其余的 保留并合并成一行
    62. if (row._columnStatus == 'first') {
    63. return {
    64. rowspan: row.hobbyList.length, // 爱好数量
    65. colspan: 1
    66. };
    67. } else if (row._columnStatus == 'next') {
    68. return {
    69. rowspan: 0,
    70. colspan: 0
    71. };
    72. }
    73. }
    74. },
    75. getData() {
    76. let origin = [
    77. {
    78. id: 1,
    79. name: 'lili',
    80. age: 18,
    81. hobbyList: [
    82. {
    83. hobby: '篮球',
    84. level: 'A',
    85. year: 1
    86. },
    87. {
    88. hobby: '足球',
    89. level: 'B',
    90. year: 2
    91. },
    92. {
    93. hobby: '羽毛球',
    94. level: 'C',
    95. year: 1.5
    96. }
    97. ],
    98. address: '山东',
    99. phone: '1978977767'
    100. },
    101. {
    102. id: 2,
    103. name: 'aazzz',
    104. age: 16,
    105. hobbyList: [],
    106. address: '新疆',
    107. phone: '13456444355'
    108. },
    109. {
    110. id: 3,
    111. name: 'ouz',
    112. age: 17,
    113. hobbyList: [{
    114. hobby: '唱歌',
    115. level: 'A',
    116. year: 3
    117. }],
    118. address: '新疆',
    119. phone: '13456444355'
    120. },
    121. {
    122. id: 4,
    123. name: 'eva',
    124. age: 19,
    125. hobbyList: [{
    126. hobby: '跳舞',
    127. level: 'B',
    128. year: 2
    129. },
    130. {
    131. hobby: '弹琴',
    132. level: 'A',
    133. year: 1
    134. }],
    135. address: '新疆',
    136. phone: '13456444355'
    137. }
    138. ];
    139. this.setData(origin);
    140. },
    141. setData(origin) {
    142. let tableData = [];
    143. origin.forEach(item => {
    144. if (item.hobbyList && item.hobbyList.length) {
    145. // 将爱好列表展开成多行
    146. item.hobbyList.forEach((it, i) => {
    147. tableData.push({
    148. ...item,
    149. ...it,
    150. _columnStatus: i == 0 ? 'first' : 'next',
    151. _rowIndex: i
    152. });
    153. });
    154. } else {
    155. tableData.push({
    156. ...item,
    157. _columnStatus: '',
    158. _rowIndex: 0
    159. });
    160. }
    161. });
    162. this.tableData = tableData;
    163. }
    164. },
    165. mounted() {
    166. this.getData();
    167. }
    168. };
    169. script>
    170. <style scoped>
    171. style>

  • 相关阅读:
    RJ45与网络变压器脚位及网线线序的关系?
    基于瞬时无功功率ip-iq的谐波信号检测Simulink仿真
    ChatGPT:something went wrong
    Using LDAP to authenticate users
    Jpa JdbcTemplate 批量插入效率对比
    基于web、dns、nfs的综合实验
    版本管理工具 SVN和git
    Apache Tomcat选择哪个版本好?
    nacos使用达梦数据库
    Machine Vision Technology:Lecture5 Local feature:Corners角点
  • 原文地址:https://blog.csdn.net/qq_35699198/article/details/134260707