• vue2 Ant-design-vue <a-table 实现整列合并,第一列整列合并为一个单元格


    项目要求:

    实现一个左侧第一列为表头,且第一列所有单元格合并为一格的表格

    最终效果图:

    网上冲浪找了很多没有找到直接合并第一列整列的,表头跟内容设置合并是分开的。

    只能换个思路实现:

    把表头隐藏:

    :showHeader="false"

    然后列名再复写在第一行,第一列所有单元合并成为一格

    以下是源码

    代码有点多,关键代码是:在构造表格内空时先把列名写在第一名。

    1. this.dataSource.push(
    2. {
    3. projName: '项目',
    4. type: '产品线',
    5. key: '5',
    6. needTotal: 'NRE',
    7. actualTotal: '实现投入人力'
    8. })

    然后是::showHeader="false" 设置表头隐藏。

    1. <template>
    2. <div class="tabletest rowColor headBold firstgreen">
    3. ///带合并行的单元格功能表,根据内容来合并,并要求实现点击功能
    4. <div>
    5. <a-table bordered
    6. :columns="columns"
    7. :data-source="dataSource"
    8. :pagination="false"
    9. @load="loading"
    10. rowKey="id"
    11. :showHeader="false"
    12. ></a-table>
    13. </div>
    14. <br/>
    15. <br/>
    16. </div>
    17. </template>
    18. <script>
    19. let ct = 0
    20. export default {
    21. name: "AllProjectTable",
    22. data() {
    23. return {
    24. msg: "data-home",
    25. dataSource: [],
    26. countnum: 0,
    27. columns: [
    28. {
    29. title: '部门',
    30. dataIndex: 'count',
    31. key: 'count',
    32. customRender: (value, row, rowIndex) => { //只针对第一列
    33. // console.log('遍历value,row, index ,this.countnum,ct', value, row, index, this.countnum, ct);
    34. console.log('针对第一列进行合并操作')
    35. const obj = {
    36. children: '01项目',
    37. attrs: {},
    38. };
    39. if (rowIndex === 0) { // 第一行数据开始,跨行合并的长度为数据data的长度
    40. obj.attrs.rowSpan = this.dataSource.length; // len表示该数据占几行:u014行,合并4
    41. }
    42. if (1 <= rowIndex) { // 从第一行往后的所有行表格均合并,0表格第二行以后的单元格不画
    43. obj.attrs.rowSpan = 0;
    44. }
    45. return obj;
    46. },
    47. customCell: (row, index) => {
    48. return {
    49. style: {
    50. 'font-weight': 'bold' ,
    51. 'background': 'rgba(0, 255, 120, 1)',
    52. 'cursor': 'pointer',
    53. 'width':'100px'
    54. },
    55. on: {
    56. click: (e) => {
    57. console.log('点击事件,在第1列')
    58. this.selectRow(row, index, 1);
    59. },
    60. },
    61. };
    62. },
    63. },
    64. {
    65. title: '产品线',
    66. dataIndex: 'type',
    67. key: 'type',
    68. customCell: (row, index) => {
    69. return {
    70. style: {'cursor': 'pointer'},
    71. on: {
    72. click: (e) => {
    73. console.log('点击事件,在第2列')
    74. this.selectRow(row, index, 2);
    75. },
    76. },
    77. };
    78. },
    79. customRender: (value, record, index) => {
    80. // return this.mergeColumn2Ok(value, row, rowIndex)
    81. console.log('针对第二列进行合并,执行OK', record, index, record.type)
    82. console.log('')
    83. const obj = {
    84. children: value,
    85. attrs: {},
    86. };
    87. let arr = this.dataSource.filter((res) => {
    88. //这里type是我需要判断的字段名(相同就合并)
    89. return res.type == record.type;
    90. });
    91. console.log('arr', arr)
    92. if (index === 0 || this.dataSource[index - 1].type != record.type) {
    93. console.log('rowspan index =', index)
    94. obj.attrs.rowSpan = arr.length;
    95. } else {
    96. console.log('rowspan is =0 , at=', index)
    97. obj.attrs.rowSpan = 0;
    98. }
    99. return obj;
    100. },
    101. },
    102. {
    103. title: '项目',
    104. dataIndex: 'projName',
    105. key: 'projName',
    106. customHeaderCell: () => ({
    107. style: {
    108. textAlign: 'center', //头部单元格水平居中
    109. },
    110. }),
    111. customCell: (row, index) => {
    112. return {
    113. style: {'cursor': 'pointer'},
    114. on: {
    115. click: (e) => {
    116. console.log('点击事件,在第3列')
    117. this.selectRow(row, index, 3);
    118. },
    119. },
    120. };
    121. },
    122. },
    123. {
    124. title: 'NRE Total(人)',
    125. dataIndex: 'needTotal',
    126. key: 'needTotal',
    127. align: 'center',
    128. },
    129. {
    130. title: '实际投入人力',
    131. dataIndex: 'actualTotal',
    132. key: 'actualTotal',
    133. },
    134. ]
    135. ,
    136. };
    137. },
    138. mounted() {
    139. this.getData(null)
    140. },
    141. methods: {
    142. mergeColumn2Ok(value, row, rowIndex) {
    143. console.log('针对第二列进行合并,执行OK', row, rowIndex, row.type)
    144. const obj = {
    145. children: row.type,
    146. attrs: {},
    147. };
    148. if (rowIndex === 0) { // 第一行数据开始,跨行合并的长度为数据data的长度
    149. obj.attrs.rowSpan = 2; // 合并当前列:第一行与第二行
    150. }
    151. if (1 === rowIndex) { //0表示:当前列的第二行的单元格不画
    152. obj.attrs.rowSpan = 0;
    153. }
    154. return obj
    155. },
    156. selectRow(rowValue, rowIndex, columnIndex) {
    157. console.log('>>点击:第', rowIndex, '行 ', '第', columnIndex, '列, ', rowValue)
    158. },
    159. getRowClassName(record) {
    160. if (record.serial == this.serial) {
    161. return 'class-name'
    162. }
    163. return ''
    164. }
    165. ,
    166. getData: function (record) {
    167. let data = {}
    168. console.log(record)
    169. data = {
    170. "extend": {
    171. "KEY_DATA": {
    172. "VALUE_MAP": {
    173. "新业务": [
    174. {
    175. "id": 2,
    176. "projectName": "Se",
    177. "projectStage": "NI",
    178. "actualTotal": 3,
    179. "nreTotal": 80
    180. }
    181. ],
    182. "Hearable": [
    183. {
    184. "id": 7,
    185. "projectName": "Ra",
    186. "projectStage": "NPI",
    187. "actualTotal": 0,
    188. "nreTotal": 24
    189. }
    190. ],
    191. "Home": [
    192. {
    193. "id": 1,
    194. "projectName": "C4",
    195. "projectStage": "NPI",
    196. "actualTotal": 3,
    197. "nreTotal": 50
    198. }
    199. ]
    200. }
    201. }
    202. }
    203. }
    204. this.dataSource = []
    205. this.dataSource.push(
    206. {
    207. projName: '项目',
    208. type: '产品线',
    209. key: '5',
    210. needTotal: 'NRE',
    211. actualTotal: '实现投入人力'
    212. })
    213. for (const key in data.extend.KEY_DATA.VALUE_MAP) {
    214. console.log('key', key)
    215. data.extend.KEY_DATA.VALUE_MAP[key].forEach(item => {
    216. let obj = {
    217. projName: item.projectName,
    218. type: key,
    219. key: key,
    220. needTotal: item.nreTotal,
    221. actualTotal: item.actualTotal
    222. }
    223. console.log('item=>', item)
    224. this.dataSource.push(obj)
    225. })
    226. }
    227. console.log(this.dataSource)
    228. return this.dataSource
    229. }
    230. ,
    231. loading() {
    232. this.countnum = 60
    233. console.log('-')
    234. }
    235. }
    236. };
    237. </script>
    238. <style lang="less" scoped>
    239. .tabletest {
    240. .title {
    241. font-size: 40px;
    242. letter-spacing: 2px;
    243. }
    244. }
    245. //a-table的父级盒子选择器 >>> 取消 表格鼠标移入的行 背景色样式
    246. ::v-deep .ant-table-tbody > tr:hover:not(.ant-table-expanded-row) > td, .ant-table-row-hover, .ant-table-row-hover > td {
    247. background: none !important;
    248. }
    249. </style>

    我这里源码还包括对第二列的内容相同的,进行动态合并。

  • 相关阅读:
    DPDK的VFIO
    magisk 启动 安全 app
    数据库管理-第152期 Oracle Vector DB & AI-04(20240220)
    基于单片机的感应自动门系统
    物联网常见协议篇
    UI自动化怎么做?不会代码能不能做
    HTML5期末大作业:美食网页主题网站设计与实现——HTML+CSS+JavaScript月饼美食食品企业网站html模板9页面
    【Jmeter】提取和引用Token
    第三章:form表单
    Backbone 网络-ResNet v2 详解
  • 原文地址:https://blog.csdn.net/LlanyW/article/details/127922294