• Ant Vue Table 合并单元格


    项目开发中,有时候需要实现单元格合并的需求,这里记录一下在Ant Design Vue的实现。

    1. <template>
    2. <div>
    3. <a-table bordered :data-source="dataSource" :columns="columns"></a-table>
    4. </div>
    5. </template>
    6. <script>
    7. const dataSource = [
    8. { key: '1', school: '林州一中', grade: '高一', class: '二班', name: '徐强' },
    9. { key: '2', school: '林州一中', grade: '高一', class: '一班', name: '林东'},
    10. { key: '3', school: '林州一中', grade: '高一', class: '二班', name: '李丹'},
    11. { key: '4', school: '林州一中', grade: '高二', class: '二班', name: '刘康'},
    12. { key: '5', school: '实验高中', grade: '高三', class: '二班', name: '杨江'},
    13. { key: '6', school: '实验高中', grade: '高三', class: '一班', name: '陈锋'},
    14. { key: '7', school: '实验高中', grade: '高三', class: '二班', name: '李华'},
    15. { key: '8', school: '实验高中', grade: '高四', class: '二班', name: '赵铭'}
    16. ]
    17. const columns = [
    18. {
    19. title: '学校', dataIndex: 'school', width: '25%',
    20. align: 'center', key: 'school',
    21. customRender (_, row) {
    22. return {
    23. children: row.school,
    24. attrs: {
    25. rowSpan: row.schoolRowSpan
    26. }
    27. }
    28. }
    29. },
    30. {
    31. title: '年级', dataIndex: 'grade', width: '25%',
    32. scopedSlots: { customRender: 'grade' }, align: 'center', key: 'grade',
    33. customRender (_, row) {
    34. return {
    35. children: row.grade,
    36. attrs: {
    37. rowSpan: row.gradeRowSpan
    38. }
    39. }
    40. }
    41. },
    42. {
    43. title: '班级', dataIndex: 'class', width: '25%',
    44. scopedSlots: { customRender: 'class' }, align: 'center'
    45. },
    46. {
    47. title: '姓名', dataIndex: 'name', width: '25%',
    48. scopedSlots: { customRender: 'name' }, align: 'center'
    49. }
    50. ]
    51. export default {
    52. name: 'TableMerge',
    53. data () {
    54. return {
    55. dataSource,
    56. columns
    57. }
    58. },
    59. methods: {
    60. // 合并单元格
    61. rowSpan (key, data) {
    62. const arr = data
    63. .reduce((result, item) => {
    64. if (result.indexOf(item[key]) < 0) {
    65. result.push(item[key])
    66. }
    67. return result
    68. }, [])
    69. .reduce((result, keys) => {
    70. const children = data.filter(item => item[key] === keys)
    71. result = result.concat(
    72. children.map((item, index) => ({
    73. ...item,
    74. [`${key}RowSpan`]: index === 0 ? children.length : 0
    75. }))
    76. )
    77. return result
    78. }, [])
    79. return arr
    80. },
    81. // 表格合并
    82. mergeRowCell (data) {
    83. let tableData = this.rowSpan('school', data) //合并学校
    84. tableData = this.rowSpan('grade', tableData) //合并年级
    85. this.dataSource = tableData
    86. }
    87. },
    88. mounted () {
    89. this.mergeRowCell(this.dataSource)
    90. }
    91. }
    92. </script>

    合并后:

  • 相关阅读:
    机器学习:什么是机器学习
    java中将List数据平均切分成N份
    HDLBits - Verilog学习笔记
    SCT71403F50,SCT71403F33,TPS7B82,TPS7B81
    Django路由层解析
    【探索Linux】—— 强大的命令行工具 P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
    安装docker
    Containerd 的镜像和容器管理
    新库上线 | CnOpenData房地产业工商注册企业基本信息数据
    transforme学习
  • 原文地址:https://blog.csdn.net/ZXH0122/article/details/133267068