javascript - 引导程序Vue,带有基于表的绑定项目数据的复选框输入
问题描述
我有一个充满数据的表。selected
我对要绑定到 b 表中的复选框的数据有一个属性。我似乎无法弄清楚如何做到这一点。
我的数据:
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 并使用 Vuev-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>
解决方案
您可以在作用域插槽内按如下方式访问行项目并将嵌套复选框绑定到selected
属性:
<template v-slot:cell(selected)="row">
<b-form-group>
<input type="checkbox" v-model="row.item.selected" />
</b-form-group>
</template>
有关详细信息,请参阅表格 - 自定义渲染文档。
推荐阅读
- python - 为什么 Pandas Dataframe(和 Python)有一个奇怪的浮点精度十进制值赋值会影响舍入?
- javascript - Ref 在浅渲染 Jest/Enzyme 期间返回 undefined
- google-cloud-platform - 带有 Kaniko 和 gcloud sdk 的 Gitlab 运行器
- mongodb - Azure Cosmos DB Mongo - 为客户存储库选择分区键
- python - 我正在尝试使用 Django 在 html 表中显示我的 DataFrame,但它只显示列名而不是行中的值?
- javascript - 迭代对象数组时使用扩展运算符的好处?
- rest - Azure DevOps Server REST API:如何为构建设置“标记源”选项?
- github - 我无法在没有收到此错误的情况下推送到 github
- excel - MS Excel 中的最大值和最小值在正负值范围内
- sql - 我是否正确实施了 SCD 类型 1 和 7