javascript - 将对象绑定到 Vue 多选
问题描述
我有一个包含两列(位置和用户)的表。位置是静态信息,但用户是使用Vue-Select进行的多选选。我需要显示当前为页面加载位置选择的用户。我从数据库中获取这些信息。我还需要能够通过使用显示所有用户的多选来更改某个位置的选定用户。示例模拟
Vue 大纲
<table>
<thead>
<tr>
<th class="col-xs-2">Locations</th>
<th class="col-xs-8">Users</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, index) in rows" v-bind:key="index">
<td>
<span>{{ row.location }}</span>
</td>
<td>
<v-select multiple v-model="row.users">
<option v-for="user in allUsers" :key="user.id" :value="user.id">{{ user.name }}</option>
</v-select>
</td>
</tr>
</tbody>
</table>
Vue
var app = new Vue({
el: '#el',
data() {
return {
errors: [],
loading: false,
rows: this.assignments,
allUsers: this.users
}
},
props: {
assignments: Array,
users: Array
},
})
如何从数据库返回行的示例
[{
"locations":[
{ "id":1,
"name":"SomePlace, CA",
"users": [
{"id":1, "name":"Person One"},
{"id":2, "name":"Person Two"}
]
},
{ "id":2,
"name":"AnotherPlace, CA",
"users": [
{"id":3, "name":"Person Three"}
]
}
]
},
{
"locations":[
{ "id":1,
"name":"SomePlace, CA",
"users": [
{"id":1, "name":"Person One"},
{"id":2, "name":"Person Two"}
]
},
{ "id":2,
"name":"AnotherPlace, CA",
"users": [
{"id":3, "name":"Person Three"}
]
}
]
}]
如何从数据库返回所有用户的示例
[
["id":1, "name":"Person One"],
["id":2, "name":"Person Two"],
["id":3,"name":"Person Three"],
]
解决方案
我已将通过props
直接传入的数据移动到数据对象,因为您的rows
属性有一个包含locations
数组的项目,我循环通过第一项rows[0]
并将row
作为选择选项:options="row"
,对于第二列,我循环通过用户selectedLocation
:
Vue.component('v-select', VueSelect.VueSelect)
var app = new Vue({
el: '#app',
data() {
return {
errors: [],
loading: false,
rows: [{
"locations": [{
"id": 1,
"name": "SomePlace, CA",
"users": [{
"id": 1,
"name": "Person One"
},
{
"id": 2,
"name": "Person Two"
}
]
},
{
"id": 2,
"name": "AnotherPlace, CA",
"users": [{
"id": 3,
"name": "Person Three"
}]
}
]
},
{
"locations": [{
"id": 1,
"name": "SomePlace, CA",
"users": [{
"id": 1,
"name": "Person One"
},
{
"id": 2,
"name": "Person Two"
}
]
},
{
"id": 2,
"name": "AnotherPlace, CA",
"users": [{
"id": 3,
"name": "Person Three"
}]
}
]
}
],
allUsers: this.users
}
},
props: {
assignments: Array,
users: Array
},
})
<table>
<thead>
<tr>
<th class="col-xs-2">Locations</th>
<th class="col-xs-8">Users</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, index) in rows[0].locations" v-bind:key="index">
<td class="lead-locations">
{{row.name}}
</td>
<td class="lead-users">
<v-select multiple v-model="row.users" label="name">
</v-select>
</td>
</tr>
</tbody>
</table>
对于演示检查此代码
推荐阅读
- javascript - Discord.js 问候消息已停止工作
- azure-active-directory - WPA Enterprise 与 Azure ADDS for Aruba IAP
- interrupt - Guru Meditation 错误:Core 0 出现恐慌(LoadProhibited),(CPU1 上的中断 wdt 超时)
- r - 如何在 R data.table 中按组进行子集化时获取不同行数的切片
- javascript - 为什么 Firebase 的实时数据库的 child_removed 事件处理程序不执行?
- javascript - 我有一个循环,其条件取决于循环本身调用的函数的布尔值
- laravel - 缺少路线 bus.edit 所需的参数
- node.js - 将nodejs程序的输出重定向到文件时编码错误(windows 10 powershell可能的问题)
- scala - 如果组的任何行具有 0 值,我将如何使用 ANY 条件进行过滤?
- java - "GeorgeNotFound" 的 MinecraftShocker 插件如果你有这个错误:jSSC-2.8_x86_64.dll+0xb5db,如何修复