BootstrapVue conditional column in b-table

后端 未结 1 1838
[愿得一人]
[愿得一人] 2021-01-07 15:07

I\'d like to only show one of my columns if the current user is an admin. I\'m not sure how to do this with bootstrapVue. Any ideas?

1条回答
  •  Happy的楠姐
    2021-01-07 15:31

    Here's a snippet based on Troy's comment.

    I've added a custom property to the field object called requiresAdmin. This is not part of standard Bootstrap-Vue.

    You can use this to filter out all the fields that require's the user to be an admin in a computed property. Based on whether the user is an admin or not. This makes it easy to add and remove fields that require's the user to be an admin.

    new Vue({
      el: '#app',
      computed: {
        computedFields() {
          // If the user isn't an admin, filter out fields that require auth.
          if(!this.isUserAdmin)
            return this.fields.filter(field => !field.requiresAdmin);
            
          // If the user IS an admin, return all fields.
          else
            return this.fields;
        }
      },
      data() {
          return {
            isUserAdmin: false,
            fields: [
              { key: 'first', label: 'First Name' },
              { key: 'last', label: 'Last Name' },
              { key: 'age', label: 'Age' },
              { key: 'sex', label: 'Sex' },
              { key: 'secretActions', label: 'Secret Actions', requiresAdmin: true },
            ],
            items: [
              { first: 'John', last: 'Doe', sex: 'Male', age: 42 },
              { first: 'Jane', last: 'Doe', sex: 'Female', age: 36 },
              { first: 'Rubin', last: 'Kincade', sex: 'Male', age: 73 },
              { first: 'Shirley', last: 'Partridge', sex: 'Female', age: 62 }
            ]
          }
        }
    })
    
    
    
    
    
    
    
    Toggle admin user ({{ isUserAdmin }})

    0 讨论(0)
提交回复
热议问题