With this code, how to expand and collapse..toggle all el-collapse-items of ElementPlus Vue3 library with one button?
<template>
<div class="demo-collapse">
<el-collapse v-model="activeName" accordion>
<el-collapse-item title="Consistency" name="1">
<script lang="ts" setup>
import { ref } from 'vue'
const activeName = ref('1')
</script>
https://element-plus.org/en-US/component/collapse.html#accordion
Please take a look at the following code snippet:
const { ref } = Vue const app = Vue.createApp({ setup() { const items = ref([{id: 1, title: "first", text: "aaaaaaaaaaaa"}, {id: 2, title: "second", text: "bbbbbbbbbbbb"}, {id: 3, title: "third", text: "ccccccccccc"}]) const activeName = ref([1]); const toggleAll = () => { activeName.value = activeName.value.length === items.value.length ? [] : items.value.map(i => i.id) } return { items, activeName, toggleAll }; }, }) app.use(ElementPlus); app.mount('#demo')You cannot perform this operation in Accordion mode. As the documentation states:
To do this, you have to remove the accordion attribute and change the activeName value to an array, like in the documentation:
To expand/collapse all items, you can create a function that will change the value of activeNames to include all names of el-collapse-item/ em> component or empty, for example
toggleElements() { if(activeName.value.length) { activeName.value = []; } else { activeName.value = ['1', '2', '3', ...]; } }