简体   繁体   English

Bootstrap-vue - 如何以编程方式显示/隐藏 b 表列

[英]Bootstrap-vue - How to show/hide a b-table column programmatically

How could I show/hide a column in the BootstrapVue b-table example below based on some event that changes the data model.我如何根据更改数据模型的某些事件在下面的 BootstrapVue b-table例中显示/隐藏列。

<template>
  <button @click="showHideAge=!showHideAge">Show/Hide Age</button>
  <b-table striped hover :items="items"></b-table>
</template>

<script>
const items = [
  { isActive: true, age: 40, first_name: 'Dickerson', last_name: 'Macdonald' },
  { isActive: false, age: 21, first_name: 'Larsen', last_name: 'Shaw' },
  { isActive: false, age: 89, first_name: 'Geneva', last_name: 'Wilson' },
  { isActive: true, age: 38, first_name: 'Jami', last_name: 'Carney' }
]

export default {
  data () {
    return {
      items: items,
      showHideAge: true
    }
  }
}
</script>

You could use computed property to get person details according to the state given by show/hide age button您可以使用computed属性根据show/hide age按钮给出的状态获取人员详细信息

<template>
    <div>
        <button @click="showHideAge=!showHideAge">Show/Hide Age</button>
        <b-table striped hover :items="persons"></b-table>
    </div>
</template>

<script>
    const items = [
        { isActive: true, age: 40, first_name: 'Dickerson', last_name: 'Macdonald' },
        { isActive: false, age: 21, first_name: 'Larsen', last_name: 'Shaw' },
        { isActive: false, age: 89, first_name: 'Geneva', last_name: 'Wilson' },
        { isActive: true, age: 38, first_name: 'Jami', last_name: 'Carney' }
    ]

    export default {
        data () {
            return {
                items: items,
                showHideAge: true
            }
        },
        computed: {
            persons() {
                if(this.showHideAge) {
                     return this.items
                } else {
                    return items.map(x => ({
                        isActive: x.isActive,
                        first_name: x.first_name,
                        last_name: x.last_name
                   }))
                }
            }
        }
    }
</script>

I realise this is old, but the question is still valid.我意识到这是旧的,但问题仍然有效。 There are v-slots for headers and cells.标题和单元格有 v 槽。 You can use v-show on a div in the slot to show/hide both header and cells that the whole column is hidden.您可以在插槽中的 div 上使用 v-show 来显示/隐藏整列隐藏的标题和单元格。

`<template>


<div>
      <button @click="showHideAge=!showHideAge">Show/Hide Age</button>
      <b-table striped hover :items="items">
       <template v-slot:cell(age)="row">
        <div v-show="showHideAge">{{ row.item.age }}</div>
       </template>
       <template v-slot:head(age)="field">
        <div v-show="showHideAge">{{ field.label }}</div>
       </template>
      </b-table>
   </div>
</template>

<script>
  const items = [
    { isActive: true, age: 40, first_name: 'Dickerson', last_name: 'Macdonald' },
    { isActive: false, age: 21, first_name: 'Larsen', last_name: 'Shaw' },
    { isActive: false, age: 89, first_name: 'Geneva', last_name: 'Wilson' },
    { isActive: true, age: 38, first_name: 'Jami', last_name: 'Carney' }
    ]

     export default {
           data () {
             return {
               items: items,
               showHideAge: true
              }
          },
         
       }
 </script>`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在bootstrap-vue中的“ b-table”组件中使用插槽“ bottom-row”? - How to use the slot “bottom-row” in “b-table” component in bootstrap-vue? 如何使用“_showDetails”动态加载Bootstrap-vue“b-table”行数据? - How to load Bootstrap-vue "b-table" row data dynamically using "_showDetails"? 如何使用项目提供程序 function 从 Bootstrap-Vue 异步更新 b 表中的项目? - How do I update the items async in a b-table from Bootstrap-Vue reusing the items provider function? bootstrap-vue b-table:在表重新加载时保持扩展行扩展 - bootstrap-vue b-table: keep expanded rows expanded on table reload 在bootstrap-vue中在没有jquery的情况下在b表中创建新的可编辑行 - Creating a new editable row in a b-table without jquery in bootstrap-vue 仅在应用过滤器时显示 bootstrap-vue b-table 中的项目 - Only display items in bootstrap-vue b-table when filter is applied 如何改变b表Vue JS中列数据的颜色 - How to change the color of column data in b-table Vue JS bootstrap vue b-table上模板内的Vue&#39;this&#39;对象 - Vue 'this' object inside template on bootstrap vue b-table Bootstrap Vue更改b表中每个单元格的背景 - Bootstrap Vue change background for each cell in b-table Bootstrap-Vue 如何创建自定义列? - Bootstrap-Vue how to create a custom column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM