简体   繁体   English

带有 v-for 循环的 Vuetify 扩展面板?

[英]Vuetify expansion panel with v-for loop?

I am working with the Vuetify expansion panel and i am using an array Headers to loop over and get the headers and another array named elements to loop over and get the desired text.我正在使用 Vuetify 扩展面板,我正在使用一个数组Headers来循环并获取标题和另一个名为elements数组来循环并获取所需的文本。 How can i make it so that the text from elements array show up only under their respective panel.我如何才能使elements数组中的文本仅显示在它们各自的面板下。 So basically for the panel, Animals, only Cat an Dog show up etc.所以基本上对于面板,动物,只有猫和狗出现等。

Here is a link to the sample codepen这是示例代码笔的链接

 new Vue({ el: "#app", data() { return { headers: [{ text: 'Animals' }, { text: 'Food' }, { text: 'Colors' } ], elements: [{ text: 'Cat', panel: 'Animals' }, { text: 'Dog', panel: 'Animals' }, { text: 'Chocolates', panel: 'Food' }, { text: 'Pizza', panel: 'Food' }, { text: 'Red', panel: 'Colors' }, { text: 'Yellow', panel: 'Colors' } ] } } })
 <link href="https://cdn.jsdelivr.net/npm/vuetify@1.5.14/dist/vuetify.min.css" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/npm/vuetify@1.5.14/dist/vuetify.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <v-app id="inspire"> <v-expansion-panel> <v-expansion-panel-content v-for="(header,i) in headers" :key="i"> <template v-slot:header> <div>{{header.text}}</div> </template> <template v-for="element in elements" :key="element"> <v-card v-if="element.panel == 'Animals'"> <v-card-text > {{element.text}} </v-card-text> </v-card> </template> </v-expansion-panel-content> </v-expansion-panel> </v-app> </div>

So basically the Animals Content should contain Dog and Cat, Food should contain Chocolate and Pizza and Colors should contain Red and yellow.所以基本上动物内容应该包含狗和猫,食物应该包含巧克力和比萨,颜色应该包含红色和黄色。 Thank You :)谢谢你 :)

One solution, that doesn't require changing the data format, rather than hard code the v-if as in一种解决方案,不需要更改数据格式,而不是像这样对 v-if 进行硬编码

<v-card v-if="element.panel == 'Animals'">

You want to check if the element panel belongs to the current header您要检查元素面板是否属于当前标题

<v-card v-if="element.panel == header.text">

You can also change the data format (not tested, could be errors below)您还可以更改数据格式(未测试,可能是下面的错误)

new Vue({
    el: "#app",
    data() {
        return {
            headers: [{
                    text: 'Animals',
                    elements: [{
                            text: 'Cat'
                        }, {
                            text: 'Dog'
                        }
                    ]
                }, {
                    text: 'Food',
                    elements: [{
                            text: 'Chocolates'
                        }, {
                            text: 'Pizza'
                        }
                    ]
                }, {
                    text: 'Colors',
                    elements: [{
                            text: 'Red'
                        }, {
                            text: 'Yellow'
                        }
                    ]
                }
            ]
        }
    }
});

and then the markup would be然后标记将是

<div id="app">
  <v-app id="inspire">
    <v-expansion-panel>
      <v-expansion-panel-content v-for="(header,i) in headers" :key="i">
        <template v-slot:header>
          <div>{{header.text}}</div>
        </template>
        <v-card v-for="element in header.elements" :key="element">
          <v-card-text >
            {{element.text}}
          </v-card-text>
        </v-card>
      </v-expansion-panel-content>
    </v-expansion-panel>
  </v-app>
</div>

You can also use a computed property that returns a function (although I like @JaromandaX' first idea better, it's simpler).您还可以使用返回函数的计算属性(尽管我更喜欢 @JaromandaX 的第一个想法,但它更简单)。

computed: {
  elementsByPanel() {
    return (panel) => {
      return this.elements.filter(el => el.panel === panel);
  }
}

Use it like this像这样使用它

    <template v-for="element in elementsByPanel(header.text)" >
      <v-card :key="element.text">
        <v-card-text >
          {{element.text}}
        </v-card-text>
      </v-card>
    </template>      

This might be preferable if the filter criteria was more complex, as a computed property is more easily tested than an expression in the template.如果过滤条件更复杂,这可能更可取,因为计算属性比模板中的表达式更容易测试。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM