javascript - Vue.js 表中隐藏/显示列的复选框无法正常工作
问题描述
我制作了一个 vue.js 引导表,用于从本地 JSON 文件加载一些数据。我正在尝试通过复选框实现显示/隐藏列。我想我已经解决了大部分问题,但问题是当我隐藏一列然后再次按下同一个复选框(使列再次可见)时,我失去了表的顺序(该列成为最后一列),所以在。例如,如果我隐藏“时间戳”列,它是我表中的第一个表头,然后按下再次显示它,它不再位于第一位,而是在最后一个位置创建。
https://imgur.com/BaTfgci --> 这就是应用程序现在的样子 https://codepen.io/frane_caleta/pen/KKPMKrL --> 我的代码的codepen,没有你将无法加载它JSON 文件虽然 https://imgur.com/a/23jx0lZ --> JSON 数据示例
第一次在这里提问,如果您需要更多信息来解决问题,请随时问我:)
<b-form-group label="Hide columns: ">
<b-form-checkbox-group id="checkbox-group-1" v-model="selected" :options="fields" name="flavour-1">
</b-form-checkbox-group>
</b-form-group>
//my table
<b-table id="myTable"
:small="small"
:bordered="bordered"
hover head-variant="dark"
stacked="md"
:items="cptItems"
:fields="selected"
:current-page="currentPage"
:per-page="perPage"
:filter="filter"
:sort-by.sync="sortBy"
:sort-desc.sync="sortDesc"
@filtered="onFiltered"
:tbody-tr-class="rowClass"
v-if="selected.length > 0">
</b-table>
//Javascript file
function initializeVue() {
return new Vue({
el: '#app',
data() {
return {
items: data.logdatas,
selected: [],
fields: [{
text: 'Origin',
value: {
key: 'origin',
label: 'Origin',
sortable: true,
class: 'text-center',
index: 0
}
},
{
text: 'Timestamp',
value: {
key: 'timeStamp',
label: 'Timestamp',
sortable: true,
class: 'text-center',
index: 1
}
},
{
text: 'Level',
value: {
key: 'level',
label: 'Level',
sortable: true,
class: 'text-center',
index: 2
}
}, ...there are 4 more fields here like this...
//my method for creating those checkboxes
created() {
this.selected = this.fields.map(field => field.value);
}
解决方案
数据是你的selected
罪魁祸首。b-checkbox-group :selection
按选择顺序列出项目。
b-table :fields
按项目的顺序列出列。
最好制作一个静态fields
列表并按selection
.
// make this data or property
let columnNames = ["one", "two", "three", "infinity", "pi"];
// make this data
let selected = []
//make this computed // can be optimized
let activeFields = columNames.filter(name => selected.includes(name))
// more like this
export default {
data(){
return {
selected: [],
columnNames: ['name1', 'name2']
},
computed(){
activeColumns(){
return this.columnNames.filter(this.selected.includes) || []
}
}
const app = new Vue({
data(){
return {
currentPage: 0,
perPage: 10,
fields: ['age', 'first_name', 'last_name'],
//selected: [],
selected: ['age', 'first_name', 'last_name'],
items: [
{ age: 40, first_name: 'Dickerson', last_name: 'Macdonald' },
{ age: 21, first_name: 'Larsen', last_name: 'Shaw' },
{ age: 89, first_name: 'Geneva', last_name: 'Wilson' },
{ age: 38, first_name: 'Jami', last_name: 'Carney' }
]
}
},
computed: {
activeFields(){
return this.fields.filter(name => this.selected.includes(name))
}
}
}).$mount("#app");
<!-- Add this to <head> -->
<!-- Load required Bootstrap and BootstrapVue CSS -->
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.min.css" />
<!-- Load polyfills to support older browsers -->
<script src="//polyfill.io/v3/polyfill.min.js?features=es2015%2CIntersectionObserver" crossorigin="anonymous"></script>
<!-- Load Vue followed by BootstrapVue -->
<script src="//unpkg.com/vue@latest/dist/vue.min.js"></script>
<script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.min.js"></script>
<div id="app">
<b-form-group label="Hide columns: ">
<b-form-checkbox-group id="checkbox-group-1" v-model="selected" :options="fields" name="flavour-1">
</b-form-checkbox-group>
</b-form-group>
<b-table id="myTable"
:bordered="true"
hover head-variant="dark"
stacked="md"
:items="items"
:fields="selected"
:current-page="currentPage"
:per-page="perPage"
tbody-tr-class="row-class"
v-if="selected.length > 0">
</b-table>
<b-table id="myTable"
:bordered="true"
hover head-variant="dark"
stacked="md"
:items="items"
:fields="activeFields"
:current-page="currentPage"
:per-page="perPage"
tbody-tr-class="row-class"
v-if="selected.length > 0">
</b-table>
</div>
推荐阅读
- bash - 从 awk 变量中删除最后一个字符 (bash)
- image - 带有图像和文件上传的 Nativescript Picker
- python - python类变量重置?
- qt - 如何在 QML javascript 功能块中创建 C++ QObject?
- google-cloud-platform - Google IAM 用于对最终用户进行身份验证?
- java - 枚举类方法给出空指针
- angular - Angular 6 业力测试中的“TypeError:expect(...).toBeVisible 不是函数”错误
- string - 如何在匹配模式之前和之后从文本文件中获取一串文本?
- lisp - Lisp - 如何进行随机排列
- c - C - 如何将 [600][400] 数组复制到 [4][4] 数组中,然后随机化元素位置?