在引导 vue 的元素中传递带有@change的参数 * 和 * 单击的值<b-form-radio-group>



试图在引导程序vue表单中传递行记录id所选单选按钮选项。Beow是具有正常b-form-input(有效(和b-form-radio-group(无效(的表单片段:

<b-table
ref="selectableTable"
:select-mode="selectMode"
:items="dataDevices"
>
<template #row-details="row">
<b-card>  <!--  drops down when selecting a table row to show the editable form -->
<b-form>
<b-form-input 
v-model="row.item.serialNumber"
@change="sendUpdate( row.item.id, { 'serno': row.item.serialNumber} )"
/>
<b-form-radio-group  
:options="deviceOptions"
@change="updateRadioSettings"
/>
...

这是我正在使用的(简化的(函数:

updateRadioSettings( value ) {
// use value to pick the right row in the array
let jsonData = { "access": this.deviceSetupArray[value].access } 
// send this data off to the microService
this.sendUpdate( id, jsonData )   
},

我从Booststrap vue文档中了解到,在没有括号的单选按钮的@change中调用函数会将单击的值传递给该函数。这本身就非常有效但是如果我尝试添加另一个参数-在本例中是表行记录中唯一的row.item.id,我将丢失该值(变为"undefined"(。

我尝试过模板中的updateRadioSettings( row.item.id, value )updateRadioSettings( value, row.item.id ),以及函数中相应的updateRadioSettings( id, value )updateRadioSettings( value, id )

我已经尝试过假设不管怎样都会传递该值,并在函数中尝试了updateRadioSettings( row.item.id )updateRadioSettings( value, id )updateRadioSettings( id, value )

在每种情况下,如果我试图将任何内容传递给函数,value都将未定义。

有什么想法吗?是否有我丢失或忘记的Vue或bootstrap属性也会包含单击的值?我想我可以应付Vue的这件事$refs并深入对象,从函数本身内部找到行id,但这感觉有些脏,并且不能保证在从表中选择该行时在该对象中设置了记录id。

谷歌似乎非常缺乏这样的例子。我发现要么是"如何向函数发送参数",要么是"如果你想要点击的值,什么都不发送",但没有建议如何同时执行这两种操作。

非常感谢对正确方向的戳、戳或建议。

您应该为此使用$event,并告诉vue将事件属性发送给方法。查看下面的片段以获得一个简单的示例:

new Vue({
el: '#app',
data() {
return {
deviceOptions: [{
text: 'Toggle this custom radio',
value: 'first'
},
{
text: 'Or toggle this other custom radio',
value: 'second'
},
{
text: 'This is the 4th radio',
value: {
fourth: 4
}
}
]
}
},
methods: {
updateRadioSettings(e, your_value) {
console.log(e, your_value)
}
}
})
<link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap@4.5.3/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap-vue@2.21.2/dist/bootstrap-vue.css" />
<script src="https://unpkg.com/vue@2.6.12/dist/vue.min.js"></script>
<script src="https://unpkg.com/bootstrap-vue@2.21.2/dist/bootstrap-vue.min.js"></script>
<div id="app">
<b-form-radio-group :options="deviceOptions" @change="updateRadioSettings($event, 'CUSTOM_VARIABLE')" />
</div>

最新更新