vue⽤antdesign中table表格,点击某⾏时触发的事件操作使⽤customRow 设置⾏属性,写对应事件
:customRow="rowClick"
然后在data⾥⾯写
rowClick: record => ({
// 事件
on: {
click: () => {
// 点击改⾏时要做的事情
// ......
console.log(record, 'record')
}
}
})
在官⽅⽂档中也写的很清楚
补充知识:Ant-Design-Vue table 合并单元格,并且添加点击事件
点击⾏,有⼀个。可以配置点击事件。
单元格的⾃定义分为两种⽅式。
⼀种是:通过template标签。
html部分
// text为dataIndex中的值,data为⾏数据,index为索引值
<template slot="xxx" slot-scope="text,data,index">
{{text|xxxFormat}}
</template>
js部分
//table的columns设定,customRender对应着html中的slot值
columns = [
{ title: "列名", dataIndex: "aaa", scopedSlots: { customRender: 'xxx' }},
]
⼀种是:customRender。下⾯给出来的是合并单元格的⼀段代码。
{
title: "列名", dataIndex: "aaa",
customRender: (text, row, index) => {
var obj = {
children: text,
attrs: {}
}
if (index % 2 == 0) {
wSpan = 2;
} else {
wSpan = 0;
}
return obj;
}
},
];
在合并单元格的代码中可以看出。obj实际上操作的是td的相关属性。children中的内容是放在td中的。
这个内容就类似于上⾯的template。因为能操作td以及内部的内容,所以这种⽅法的灵活性更加⾼。对于单元格合并这种操作来说,只能通过customRender来了。
虽然官⽅给了很多在table中添加a标签的例⼦,不过都没有对点击事件填写相应的⽅法调⽤。
如果只是简单的点击事件,可以通过简单地template调⽤点击事件。也可以⽅便的传参数。
<template slot="xxx" slot-scope="text,data,index">
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="clickMe">你点我呀</a>
</template>
antdesignvue 配置外部文件知识点,来了,如果是合并单元格⾥⾯添加点击事件呢?
第⼀次尝试
customRender: (text, row, index) => {
var obj = {
children: <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" click='cityClick(text)'>{text}</a>, attrs: {}
};
if (index % 2 == 0) {
wSpan = 2;
} else {
wSpan = 0;
}
return obj;
},
⾃⼰写的时候,⼼⾥就觉得别扭,click='cityClick(text)'这个地⽅值能传进去么?
试了下,呵呵哒,⽅法都不好使,也不报错。。
第⼆次尝试,借鉴下customRow
customRender: (text, row, index) => {
var obj = {
children: <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{text}</a>,
attrs: {},
on: {
click: () => {
this.$message.info(text);
}
}
};
if (index % 2 == 0) {
wSpan = 2;
} else {
wSpan = 0;
}
return obj;
},
写完之后,⾃我感觉还是不错的,试⼀下。
不动如⼭。。。
第三次尝试,祭出⼤杀器 vue-jsx
children不能简简单单的写个<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{text}</a>,得给他翻译翻译
虚拟DOM不错的样⼦
var vm = this;
const columns = [
{ title: 'Name', dataIndex: 'name' },
{
title: 'City',
dataIndex: 'city',
customRender: (text, row, index) => {
var child = vm.$createElement("a", {
domProps: {
innerHTML: text
},
on: {
click: function () {
vm.cityClick(text);
}
}
});
var obj = {
children: child,
attrs: {},
};
if (index % 2 == 0) {
wSpan = 2;
} else {
wSpan = 0;
}
return obj;
},
},
再单独把实现⽅法拿出来
var child = vm.$createElement("a", {
domProps: {
innerHTML: text
},
on: {
click: function () {
vm.cityClick(text);
}
}
});
看看效果
完美~~~
以上这篇vue⽤ant design中table表格,点击某⾏时触发的事件操作就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论