1、表格整行添加点击事件
方法一:在data中设置对应得点击事件
<a-table
:loading="loading"
:columns="columns"
:dataSource="tableData"
:pagination="false"
:rowSelection="rowSelection"
:customRow="rowClick"
/>
<script>
export default {
data(){
return {
rowClick: record => ({
on: { // 事件
click: () => {
// 点击改行时要做的事情
console.log('record', record)
},
}
})
}
}
}
</script>
方法二:在methods中设置
<script>
export default {
methods:{
rowClick(record, index){
return {
on: {
click: () => {
console.log(record,index)
}
}
}
}
}
}
</script>
2、设置点击该行选中多选框,已选则取消多选
<template>
<a-table
:loading="loading"
:columns="columns"
:dataSource="tableData"
:pagination="false"
:rowSelection="rowSelection"
:rowClassName="setRowClassName"
:customRow="rowClick"
:rowKey="rowKey"
/>
</template>
<script>
export default {
data(){
return {
selectedRowKeys: [],
options: {
selectedRowKeys: this.selectedRowKeys,
onChange: this.onSelectChange
},
}
},
computed: {
rowSelection() {
return this.options
}
},
methods:{
onSelectChange(selectedRowKeys, selectedRows) {
this.options = {
selectedRowKeys: selectedRowKeys,
onChange: this.onSelectChange
}
}
}
}
</script>
1.antd表格设置:https://blog.csdn.net/weixin_44051815/article/details/90049091
2.单选高亮设置:https://blog.csdn.net/zm_miner/article/details/83026968
3.多选框点击行设置:https://blog.csdn.net/qq_35331167/article/details/90174428
3、利用slot方法自定义单元格显示内容
单个
<template>
<a-table :columns="columns" :dataSource="data">
<a slot="action" slot-scope="text" href="javascript:;">Delete</a>
</a-table>
</template>
<script>
const columns = [
{ title: 'Name', dataIndex: 'name', key: 'name' },
{ title: 'Age', dataIndex: 'age', key: 'age' },
{ title: 'Address', dataIndex: 'address', key: 'address' },
{ title: 'Action', dataIndex: '', key: 'x', scopedSlots: { customRender: 'action' } },
];
</script>
多个
<template>
<a-table :columns="columns" :dataSource="data">
<template v-for="col in ['name','age','address']" :slot="col" slot-scope="text,record">
<div :key='col'></div>
</template>
</a-table>
</template>
<script>
const columns = [
{ title: 'Name', dataIndex: 'name', key: 'name' , scopedSlots: { customRender: 'name' } },
{ title: 'Age', dataIndex: 'age', key: 'age' , scopedSlots: { customRender: 'age' } },
{ title: 'Address', dataIndex: 'address', key: 'address', scopedSlots: { customRender: 'address' } },
];
</script>
4、使用render方法自定义单元格显示内容
const columns = [
{
title: '操作',
dataIndex: 'operation',
fixed: 'right',
align: 'center',
customRender: (text, record) => {
const h = this.$createElement
let ele = [
h('a-menu-item', {key: 1,}, '重新分配账号'),
h('a-menu-item', {key: 2}, '手动同步主数据'),
h('a-menu-item', {key: 3}, '重置密码')
]
return h('a-dropdown', {
props: {
placement: 'bottomCenter'
}
}, [
h('a-menu', {
slot: 'overlay',
on: {
'click': (e) => {
this.handleMenuClick(e.key, record)
}
}
}, ele),
h('a-button', ['操作', h('a-icon', {props: {type: 'down'}})])
])
}
}
]