我想用一个click事件在我的表去点击行detailpage与第一和最后一个TD元素上的异常。
例:
<tr v-for="(row, key) in $rows" :key="row.id">
<td>*Input checkbox*</td>
<td>{{ row.user.type }}</td>
<td>{{ date | moment('MMMM') }}</td>
<td>{{ row.customer.name }}</td>
<td>{{ row.user.name }}</td>
<td>{{ row.total }}</td>
<td>*Icon to archive*</td>
</tr>
我不能因为第一和最后一个元素异常的使用方法:
<tr @click="detailPage(row.user.id)" v-for="(row, key) in $rows" :key="row.id">
我不想因为重复的代码使用方法:
<tr v-for="(row, key) in $rows" :key="row.id">
<td>*Input checkbox*</td>
<td @click="detailPage(row.user.id)">{{ row.user.type }}</td>
<td @click="detailPage(row.user.id)">{{ date | moment('MMMM') }}</td>
<td @click="detailPage(row.user.id)">{{ row.customer.name }}</td>
<td @click="detailPage(row.user.id)">{{ row.user.name }}</td>
<td @click="detailPage(row.user.id)">{{ row.total }}</td>
<td>*Icon to archive*</td>
</tr>
我试过,但没有触发点击事件:
<tr v-for="(row, key) in $rows" :key="row.id">
<td>*Input checkbox*</td>
<template @click="detailPage(row.user.id)">
<td>{{ row.user.type }}</td>
<td>{{ date | moment('MMMM') }}</td>
<td>{{ row.customer.name }}</td>
<td>{{ row.user.name }}</td>
<td>{{ row.total }}</td>
<td>*Icon to archive*</td>
</template>
</tr>
我怎么能使用jQuery解决:
$('table#tbl > tbody > tr > td').not(':first').not(':last').click(function() {
// detailpage
})
从点击事件方法
methods: {
detailPage (id) {
this.$router.push({path: `/timesheets/view/${id}`})
},
有没有在Vue公司正确的方式不重复的代码?
你可以不希望与点击触发TD元素上添加此事件修饰符:
v-on:click.stop=""
并留下tr元素上@单击。
您可以查看文档的详细信息:https://vuejs.org/v2/guide/events.html
new Vue({
el: '#app',
data: {
message: 1
},
methods:{
add(){
this.message++;
}
}
})
td {
border: 2px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<table>
<tr @click="add()">
<td>Hello</td>
<td v-on:click.stop="">Visitor n° {{message}}</td>
</tr>
</table>
</div>