javascript - Bootstrap Vue 动态表模板
问题描述
我正在使用 Bootstrap Vue JS 表组件来创建数据表: https ://bootstrap-vue.js.org/docs/components/table
我是 VueJS 的新手,不确定如何解决这个问题,这使得寻找解决方案变得更加复杂。
我使用 API 端点返回 JSON 数据:
{
"options":{
"filter":false
},
"fields":[
{
"key":"id",
"label":"Id",
"editLink":false,
"display":true,
"sortable":true,
"class":"shrink"
},
{
"key":"name",
"label":"Name",
"editLink":true,
"display":true,
"sortable":true
}
],
"data":[ ]
}
这是我的表格模板:
<b-table striped hover bordered foot-clone class="table-sm"
:items="users" :fields="displayedFields" :per-page="perPage" :current-page="currentPage" :filter="filter"
@filtered="onFiltered"
>
<template v-for="(field, index) in fields">
<template slot="{{field.key}}" slot-scope="row" v-if="field.editLink">
<router-link :to="'/user/' + row.item.id" v-bind:key="index"></router-link>
</template>
</template>
<template slot="status" slot-scope="row">
<toggle-button :width="36" :height="18" v-model="row.status" :labels="false" :colors="{checked: '#00FF00', unchecked: '#FF0000', disabled: '#CCCCCC'}"/>
</template>
</b-table>
第一个模板标签是来自疯狂猜测的尝试。我希望能够有条件地从字段配置中为列选择一个表。您可以在我的尝试中看到,当字段的配置 editLink 为 true 时,我想放置一个 RouterLink。
我怎样才能完成这项工作?
解决方案
如果您使用的是 2.0.0 或更高版本,则bootstrap-vue
需要更改插槽名称,因为它们已更改,并且旧的 vue 插槽也已被弃用,而支持v-slot
.
我更改了接受的答案小提琴以使用新的命名和 v-slot
new Vue({
el: "#app",
data: {
fields: [{
key: "id",
label: "Id",
colType: "span"
}, {
key: "name",
label: "Name",
colType: "button"
}, {
key: "uhh",
label: "uhh..",
colType: "idk"
}],
items: [{
id: 0,
name: "Test 0"
}, {
id: 1,
name: "Test 1"
}, {
id: 2,
name: "Test 2"
}]
}
});
<link href="https://unpkg.com/bootstrap@4.3.1/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://unpkg.com/bootstrap-vue@2.0.0/dist/bootstrap-vue.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.js"></script>
<script src="https://unpkg.com/bootstrap-vue@2.0.0/dist/bootstrap-vue.js"></script>
<div id="app">
<b-table :items="items" :fields="fields">
<template v-for="(field, index) in fields" v-slot:[`cell(${field.key})`]="data">
<div v-if="field.colType === 'button'">
<h5>{{data.item.name}}</h5>
<b-button>Am Button</b-button>
</div>
<div v-else-if="field.colType === 'span'">
<h5>{{data.item.name}}</h5>
<span>Am Span</span>
</div>
<div v-else>
<h5>{{data.item.name}}</h5>
Am Confused
</div>
</template>
</b-table>
</div>
推荐阅读
- android - 检测运行 MDM(移动设备管理)的设备
- c++ - 逐个字符读取文件并使用测试
- java - 终端的 POST 请求中不支持 Spring Boot 内容类型“文本/纯文本”
- python - Python将GTFS时间转换为日期时间
- android - 使用 GestureDetectorCompat 的奇怪行为
- python - 从队列中获取消息时更新标签
- android - Android、Kotlin、ContentResolver:图像查看器无法识别我的应用发送的内容 uri
- c# - 为什么我不能在两个事件中调用表单对象?
- c# - 合并多个内容中的所有内容
div内的标签,变成单个字符串
- java - 如果我初始化一个已经在 JAVA 中初始化的 Class 对象会发生什么?