• ant-design-vue3的table使用总结


    vue2x的语法

    背景:使用table组件模仿windows文件列表

    效果:如下图

    文件列表不需要分页,因此这里没有做分页功能。

    基础结构:

    1. <div class="full" style="overflow: auto;user-select: none;position:relative;" >
    2. <a-table class="file_list_table"
    3. :columns="columns"
    4. :data-source="data"
    5. :pagination="false"
    6. size="small"
    7. :showSorterTooltip="false"
    8. :customRow="setRow"
    9. @resizeColumn="handleResizeColumn"
    10. @change="change_table"
    11. style="padding:0 10px; "
    12. >
    13. a-table>
    14. div>

    class是为了用来修改当前a-table组件样式用的,并且不影响其他地方正常使用a-table的样式。

    columns放表头信息

    data-source放表数据信息

    pagination 是否展示分页组件

    size设置表大小,可以看做缩放大小,这里参考windows系统的文件管理器,设置最小

    showSorterTooltip 鼠标放在表头时,是否弹出提醒排序信息(很丑)

    customRow  设置行属性,主要是用来设置行点击事件,双击事件。(这里针对单双击事件冲突问题a-table没有处理,难题交给了我们开发者,同时设置单双击事件时,双击会触发一次双击事件和触发两次单击事件)

    resizeColumn 拖拽表头宽度的鼠标事件,会告知鼠标移动了多少距离,拖动的哪一个表头字段

    change  发生排序,筛选,分页页码切换会触发该事件

    .....太多了,不懂的看文档吧

    https://www.antdv.com/components/table-cnicon-default.png?t=M85Bhttps://www.antdv.com/components/table-cn

    定义字段

    根据windows文件管理器,我们肉眼可见需要四个字段:

    name文件名、type文件类型、update修改日期、size大小

    1. [
    2. {
    3. width: 500,
    4. title: '名称',
    5. dataIndex: 'name',
    6. key: 'name',
    7. sorter:(a, b) => this.compareName(a, b),
    8. sortOrder:this.sorted.columnKey === 'name' && this.sorted.order,
    9. ellipsis: true,
    10. resizable: true,
    11. },
    12. {width: 220,title: '修改日期', dataIndex: 'date',key: 'date',ellipsis: true, resizable: true,},
    13. {
    14. width: 170,
    15. title: '类型',
    16. dataIndex: 'type',
    17. key: 'type',
    18. sorter: (a, b) => this.compareType(a.type, b.type),
    19. sortOrder: this.sorted.columnKey === 'type' && this.sorted.order,
    20. ellipsis: true,resizable: true,
    21. },
    22. { width: 140,title: '大小', dataIndex: 'size',key: 'size',ellipsis: true,resizable: true, },
    23. { title: '', dataIndex: 'empty',key: 'empty',},
    24. ]

    这里最后一个字段,是做到最后才添加的,因为需要有一列自适应的单元格,才能确保其他列在table组件大小变化时保持宽度不变。windows文件管理器就是这么个效果。

    定义数据

    文件展示大小,文件夹不需要

    文件或者文件夹都需要展示图标,设置图标名称

    1、文件

    {
        "size":26851,
        "name":"456.txt",
        "icon":"txt",
        "update":"2022/09/10 23:56",
        "id":"a2",
        "type":"file"
    }

    2、文件夹

    {
        "name":"456",
        "icon":"dir",
        "update":"2022/09/10 23:56",
        "type":"dir"
    }
    

    自定义展示字段数据

    不想要普通的字符串,通过如下方式

    如下图:

    1. <a-table class="file_list_table"
    2. :columns="columns"
    3. :data-source="data"
    4. :pagination="false"
    5. size="small"
    6. :showSorterTooltip="false"
    7. :customRow="setRow"
    8. @resizeColumn="handleResizeColumn"
    9. @change="change_table"
    10. style="padding:0 10px; "
    11. >
    12. <template v-slot:bodyCell="{column,record}">
    13. <template v-if="column.dataIndex==='name'">
    14. <div :id="'FileList_'+record.name">
    15. <img class="file_list_icon" :src="getIconUrl(record)">
    16. <span style="font-size: 15px;">{{ record.name }}span>
    17. div>
    18. template>
    19. template>
    20. a-table>