search

Home  >  Q&A  >  body text

Take Vuetify's v-data-table as an example to implement the function of conditionally hiding columns

<p>I have a table with columns <code>v-data-table</code> and <code>actions</code> and I want to display this column only if the user has certain permissions. I'm using a mixin to check permissions. </p> <p>I tried the following without success: </p> <pre class="brush:html;toolbar:false;"><template v-slot:[`header.actions`]="{ header }" v-if="hasPermission('update center ')"> {{ header.text }} </template> </pre> <p>This is how I use the mixin in the component file: </p> <pre class="brush:js;toolbar:false;">import BaseLayout from "../layouts/Base/Base.vue"; import hasPermission from "../../../mixins/hasPermissions"; export default { mixins: [hasPermission], ... } </pre> <p>Result: [1]: https://i.stack.imgur.com/aVSgJ.png</p>
P粉775723722P粉775723722498 days ago585

reply all(1)I'll reply

  • P粉681400307

    P粉6814003072023-08-26 09:25:31

    header.actions is a slot used to override actions column header rendering. If you don't pass it (when the condition is false), Vuetify will render the default representation.

    If you want to conditionally hide (not render) certain columns, define your table header as computed

    computed: {
      headers() {
        const headers = [
          // 其他表头定义
        ]
        if(this.hasPermission('update center')) {
          headers.push({
            // actions 表头定义
          })
        }
    
        return headers
      }
    }
    

    reply
    0
  • Cancelreply