a-table设置滚动条以及数据内容过多省略号显示鼠标悬浮显示全部内容

文章讲述了如何在Vue组件a-table中设置滚动条,并处理内容过多时的行高撑开问题,采用样式控制实现内容省略和悬浮显示,同时展示了如何通过插槽添加title属性来展示完整内容。涉及到的特性包括动态渲染数据、列宽控制、自定义列模板等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

a-table是动态渲染数据的一个表格标签

滚动条:

<a-table :scroll="{x: '100%', y: '400px'}"></a-table>

 也可以在样式中设置  数据没有超过最大高度的时候不会显示滚动条

style="max-height: 250px;overflow-y: auto;"

内容过多就会导致行高撑开那么在样式里面添加以下几项(溢出内容省略号显示)

:not(:last-child)样式排除最后一列(操作按钮不需要做内容省略操作)

.table-wrap /deep/ .ant-table-tbody > tr > td :not(:last-child){
    height: 100px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    content:attr(data-title);
    padding: 12px 16px;
}

vue组件的label文字超出省略显示

    width: 102px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;

给动态渲染的数据做一个插槽添加title属性进行鼠标悬浮显示文字

:columns="columns"  每列信息

:data-source="data"  数据来源

:row-selection="rowSelection"  复选框

:selectedRowKeys="selectedRowKeys"  关键词列表

:pagination="pagination"  分页

:scroll="{x: '100%', y: '400px'}"  滚动条

<a-table :columns="columns" :data-source="data" :row-selection="rowSelection" :selectedRowKeys="selectedRowKeys" :pagination="pagination" :scroll="{x: '100%', y: '400px'}">
    <template slot="operation" slot-scope="text,record">
        <a @click="方法(record)" style="margin-right:16px;color:#f68f6e">关联固件 <img src="../../../public/img/关联固件.svg" alt="" style="width: 14px;height: 13px;transform: translateY(-2px);" ></a>
        <a @click="方法(record)" style="margin-right:16px;color:#2ab62b">上传poc <img src="../../../public/img/上传.svg" alt="" style="width: 14px;height: 13px;transform: translateY(-2px);" ></a>
        <a @click="方法(record)"  style="color:#1890FF">查看poc <img src="../../../public/img/搜索或查看.svg" alt="" style="width: 13px;height: 13px;transform: translateY(-2px);" ></a>
    </template>
    <template slot="severity" slot-scope="text,record">
       <a-tag color="#cd201f" v-if="text==1">高</a-tag>
       <a-tag color="#f68f6e" v-else-if="text==2">中</a-tag>
       <a-tag color="#f7c870" v-else-if="text==3">低</a-tag>
       <a-tag color="#68b1d2" v-else>未知</a-tag>
    </template>
    <template slot="mode" slot-scope="text,record">
       <td  v-if="text==1">手动上传</td>
       <td  v-else-if="text==2">检测发现</td>
    </template>
    <template slot="downloadUrl" slot-scope="text,record">
        <a href="text" :title="text">{{text}}</a>
    </template>
    <template slot="details" slot-scope="text,record">
      <p :title="text">{{text}}</p>
    </template>
    <template slot="tags" slot-scope="text,record">
      <p :title="text">{{text}}</p>
    </template>
</a-table>

添加插槽关系

columns:[
    {
        title: '详细信息',
        dataIndex: 'details',
        align: 'center',
        scopedSlots: { customRender: 'details' },
    },
    {
        title: '严重程度',
        dataIndex: 'exposure',
        align: 'center',
        scopedSlots: { customRender: 'severity' },
    },
    {
        title: '范围',
        dataIndex: 'tags',
        align: 'center',
      scopedSlots: { customRender: 'tags' },
    },
    {
        title: '地址',
        dataIndex: 'downloadUrl',
        align: 'center',
        scopedSlots: { customRender: 'downloadUrl' },
    },
    {
        title: '来源',
        dataIndex: 'mode',
        align: 'center',
        scopedSlots: { customRender: 'mode' },
    },
    {
        title: '操作',
        dataIndex: 'operation',
        align: 'center',
        scopedSlots: { customRender: 'operation' },
    },
]

省略跟鼠标悬浮显示全部内容最简单粗暴的方法直接在columns中要省略的列添加ellipsis: true

如下:

title: '详细信息',
dataIndex: 'details',
align: 'center',
ellipsis: true,
width: 100

span标签

<span style="  display: inline-block;width: 100px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;" :title="item.firmwareName">{{item.firmwareName}}</span>

要实现鼠标悬浮显示完整内容,并且在文本前面加省略号,可以使用 Vue 的 `j-ellipsis-head` 组件并自定义样式和逻辑。以下是具体步骤: ### 修改 `j-ellipsis-head` 组件的使用方式 1. **修改组件使用**: 在使用 `j-ellipsis-head` 组件的地方,将其修改为能够满足需求的形式。你可以使用 `slot` 插槽来自定义内容。 2. **添加自定义样式**: 添加 CSS 样式来处理鼠标悬浮显示完整内容的效果。 3. **更新模板**: 在模板中使用自定义样式和逻辑。 ### 示例代码 #### 修改组件使用 ```vue <a-tooltip placement="topLeft"> <template slot="title"> {{ text }} </template> <span class="ellipsis-container" :title="text"> {{ text ? `...${text.substring(text.length - 15)}` : '-' }} </span> </a-tooltip> ``` #### 添加自定义样式 在组件或全局样式文件中添加以下 CSS: ```css .ellipsis-container { display: inline-block; max-width: 150px; /* 根据实际需要调整宽度 */ white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } ``` ### 完整示例 假设你在一个表格列中使用这个功能,以下是完整的模板部分: ```vue <template> <a-table :columns="columns" :dataSource="data"> <template slot="userSlot" slot-scope="text, record"> <a-tooltip placement="topLeft" v-if="text && (record.deviceType != 1)"> <template slot="title"> 户名:{{ text.householder }}<br> 户号:{{ text.householdNum }}<br> 电话 1:{{ text.telNum }}<br> <span v-if="text.contactInformation">电话 2:{{ text.contactInformation }}<br></span> 地址:{{ text.address }} </template> <span class="ellipsis-container" :title="text.address"> {{ text.address ? `...${text.address.substring(text.address.length - 15)}` : '-' }} </span> </a-tooltip> <a-tooltip placement="topLeft" v-else-if="record.deviceType == 1 && record.bizMeterExtPurchaseProvisionGas"> <template slot="title"> 地址:{{ record.bizMeterExtPurchaseProvisionGas.address }} </template> <span class="ellipsis-container" :title="record.bizMeterExtPurchaseProvisionGas.address"> {{ record.bizMeterExtPurchaseProvisionGas.address ? `...${record.bizMeterExtPurchaseProvisionGas.address.substring(record.bizMeterExtPurchaseProvisionGas.address.length - 15)}` : '-' }} </span> </a-tooltip> </template> </a-table> </template> <style scoped> .ellipsis-container { display: inline-block; max-width: 150px; /* 根据实际需要调整宽度 */ white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } </style> ``` ### 解释 1. **`a-tooltip` 组件**: - 使用 `a-tooltip` 组件来提供鼠标悬浮提示功能。 - `slot="title"` 插槽用于定义鼠标悬浮显示的完整内容。 - `slot-scope="text, record"` 获取单元格的数据。 2. **自定义样式**: - `.ellipsis-container` 类用于限制文本宽度并在文本溢出时显示省略号- `max-width: 150px` 可以根据实际需要调整。 3. **动态生成省略号**: - `text ? `...${text.substring(text.length - 15)}` : '-'` 动态生成省略号和最后 15 个字符。 通过以上步骤,你可以实现在鼠标悬浮显示完整内容,同时在文本前面加上省略号的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值