Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically open each expansion panel in Vuetify

I am trying to programmatically open and close the expansion panels with Vuetify 2.3.5

<v-expansion-panels accordion>
    <v-expansion-panel v-for="(item,i) in faqs" :key="i">
        <div class="expansion-panel-header-container">
            <div class="handle"><v-icon>$drag_icon</v-icon></div>
            <v-expansion-panel-header hide-actions expand-icon="$edit">
                <span class="font-weight-bold">{{item.question}}</span>
            </v-expansion-panel-header>
            <div class="action-icons">
                <v-icon @click.native="doSomething">$edit</v-icon>
                <v-icon>$delete</v-icon>
            </div>
        </div>
    <v-expansion-panel-content>
        CONTENT GOES HERE
    </v-expansion-panel-content>
<v-expansion-panels accordion>

Currently the v-expansion-panel-header becomes the entire button that triggers the open and close of the panel but I would like to disable that function and trigger the open and close with this <v-icon @click.native="doSomething">$edit</v-icon> instead.

I can not find any documentation on how to do this.

Does anyone know how I could go about achieving this functionality?

like image 565
Jason Avatar asked Jul 21 '20 17:07

Jason


1 Answers

All you need to do is add a v-model. When you set the value to null all panels will get closed:

<v-expansion-panels v-model="openedPanel" accordion>
  ...
data () {
  return {
    openedPanel: null
  }
},
methods: {
  closeAllPanels () {
    this.openedPanel = null
  },
  openPanel (index) {
    this.openedPanel = index
  }
}

If you want to be able to open multiple panels at the same time, use an array instead:

<v-expansion-panels v-model="openedPanel" multiple accordion>
  ...
data () {
  return {
    openedPanel: []
  }
},
methods: {
  closeAllPanels () {
    this.openedPanel = []
  },
  openPanel (index) {
    this.openedPanel.push(index)
  },
  closePanel (index) {
    this.openedPanel.splice(index, 1)
  }
}
like image 91
AlekseyHoffman Avatar answered Nov 16 '22 10:11

AlekseyHoffman