【发布时间】:2019-07-08 19:05:48
【问题描述】:
我有一个充满数据的表格。我在要绑定到 b 表中的复选框的数据上有一个 selected 属性。我似乎无法弄清楚如何做到这一点。
我的数据:
data: () => ({
tableData: [
{
title: "title01",
desc: "desc01",
selected: false
},
{
title: "title02",
desc: "desc02",
selected: false
},
],
tableColumns: [
{ key: "selected", label: "", sortable: false }
{ key: "title", label: "Title", sortable: false },
{ key: "desc", label: "Description", sortable: false }
})
html:
<b-table id="myTabel"
hover
striped
:items="tableData"
:fields="tableColumns">
<template slot="selected" slot-scope="row">
<b-form-group>
<input type="checkbox" v-model="How_To_Bind_To_Object_Prop?">
</b-form-group>
</template>
</b-table>
在我的一生中,我无法将v-model 设置为实际绑定到表数据。
v-model="tableData.selected" 将所有复选框绑定到所有数据对象。所以,如果你检查一个,你检查所有,反之亦然。我只是不知道如何将它绑定到该行的数据。
我可以通过使用更传统的 HTML 并使用 Vue 的 v-for 循环遍历 tableData 来绑定每个表格行来做到这一点。但是,我们正在尝试将大部分(如果不是全部)表单转移到使用 bootstrap-vue。
所以,这很好用:
<table>
<thead>
<tr>
<th :key="key" v-for="(tableColumn, key) in tableColumns">
{{ tableColumn.label }}
</th>
</tr>
</thead>
<tbody>
<tr :key="key" v-for="(tableRow, key) in tableData">
<td>
<input type="checkbox"
v-model="tableRow.selected">
</td>
<td>
{{ tableRow.title }}
</td>
<td>
{{ tableRow.desc }}
</td>
</tr>
</tbody>
</table>
【问题讨论】:
标签: javascript vue.js vuejs2 vue-component bootstrap-vue