首先,确保项目中已经安装并引入了 ant-design-vue
,然后在 Vue 组件中使用 Table
组件,并配置 customRow
属性。
<template>
<a-table :dataSource="data"
:columns="columns" :rowKey="record => record.key"
:customRow="customRow">
</a-table>
</template>
<script>
import { Table } from 'ant-design-vue';
export default { components: { ATable: Table },
data() {
return {
data: [
{ key: 1, name: 'John Doe', age: 28 }, { key: 2, name: 'Jane Doe', age: 22 },
],
columns: [
{ title: 'Name', dataIndex: 'name' }, { title: 'Age', dataIndex: 'age' },
]
};
},
methods: {
customRow(record, index) {
return {
on: {
click: () => {
this.handleRowClick(record);
},
mouseenter: () => {
console.log('Mouse entered row:', index);
}
},
style: {
cursor: 'pointer',
background: index % 2 === 0 ? '#f0f0f0' : '#ffffff'
}
};
},
handleRowClick(record) {
console.log('Clicked row:', record);
}
}
};
</script>
在这个示例中,我们通过 customRow
方法为每一行设置了不同的事件和样式:
handleRowClick
方法,并传递当前行的数据 record
,输出点击行的信息。mouseenter
事件中输出当前行索引,方便查看鼠标是否进入了该行。可以根据数据内容,动态设置行的样式或行为。例如,当用户的年龄大于 25 时,为行添加不同的背景色:
customRow(record, index) {
return {
on: {
click: () => {
this.handleRowClick(record);
}
},
style: {
cursor: 'pointer', background: record.age > 25 ? '#e6f7ff' : '#ffffff', // 根据条件设置背景色
color: record.age > 25 ? '#10ff' : '#000000' // 根据条件设置字体颜色
}
};
}
customRow
常见配置在 customRow
返回的对象中,可以配置以下属性:
click
、mouseenter
等),让行的交互更丰富。customRow
是一个灵活的配置项,允许开发者自定义每一行的事件和样式。通过 customRow
,你可以轻松实现行点击、高亮、条件样式等功能,使表格更符合项目的交互需求。
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- howto1234.com 版权所有 湘ICP备2023017662号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务