【发布时间】:2023-03-11 00:45:01
【问题描述】:
我是来自 JS/JQuery 的 Vue 新手。我有一张桌子,每行有 2 个可能的按钮和两个输入,都包含在 <td> 中。当我单击一个按钮时,我希望最近的输入添加一个类。在 JQuery 中,我会使用 closest 方法来选择相邻的 <td> 这是我的 Vue 模板语法。非常感谢!
<tbody>
<tr v-for="child in registeredChildren">
<td class="col-2"><a :href="'/getchild/'+ child.child_id">{{child.childFirstName}}</a>    {{child.childLastName}}</td>
<!--========TIME IN===========-->
<td class="col-3 pb-2"}"><input style="text-align:center;" class="form-control editChild initial" type="text" v-model="child.timeIn" ></td>
<td><button v-on:click="updateTimeIn(child)" class="btn btn-outline-success">Reset</button></td>
<!-- //========TIME Out ===========//-->
<td class="col-3 pb-2" ><input style="text-align:center;" class="form-control editChild" type="text" v-model="child.timeOut" ></td>
<td><button v-on:click="updateTimeOut(child)" class="btn btn-outline-success">Reset</button></td>
</tr>
</tbody>
方法:我在想是否可以在 UpdateTimeIn 和 TimeOut 方法中添加一些代码,这可能是一种方法吗?
methods:{
updateTimeIn(child){
this.updatedChild = child;
console.log(child.child_id,child.timeIn)
axios.post('http://kidsclub.test/upDateChildTimeIn', {
child_id: child.child_id,
timeIn: child.timeIn,
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
},
**NB** I have the same for updateTimeOut
【问题讨论】:
-
你能发布一个
registeredChildren的样例吗?child_id是唯一的吗? -
另外,我认为这个答案描述了一个很好的方法。 stackoverflow.com/a/53617104/909973 您必须创建一个名为
childTableRow的组件,然后将child对象作为道具传递给它。然后,您可以在childTableRow组件中使用局部变量来切换模板中的类。