简体   繁体   English

Vue JS - 基于下拉选择添加动态组件

[英]Vue JS - Add dynamic component based on dropdown selection

I am new to Vue JS and I have below requirement我是 Vue JS 的新手,我有以下要求

Requirement:要求:

I have dropdown with below values我有以下值的下拉列表

在此处输入图像描述

On selection of each value, I want to add dropdown component on page, which are already defined.在选择每个值时,我想在页面上添加已经定义的下拉组件。 For example:例如:

  • On Selection of 'Closed', dropdown component (which is searchable dropdown)will be added在选择“关闭”时,将添加下拉组件(可搜索的下拉菜单)
  • On Selection of 'Reviewed', another dropdown component where user can select values from dropdown.在选择“已审核”时,用户可以从下拉列表中选择 select 值的另一个下拉组件。 (not searchable one). (不可搜索的)。 Likewise..同样地..

What I have:我有的:

  • I already have all the different types of dropdowns as a component.我已经将所有不同类型的下拉菜单作为一个组件。

What I tried:我尝试了什么:

  • I have loaded all the four types of dropdown on page load, and I am hiding and showing them based on dropdown value selection.我已经在页面加载时加载了所有四种类型的下拉列表,并且我根据下拉值选择隐藏和显示它们。
  • Example: I am showing only searchable dropdown component when user select 'Closed' option and hiding other components.示例:当用户 select“关闭”选项并隐藏其他组件时,我只显示可搜索的下拉组件。 I am showing selectable dropdown component when user select 'Reviewed' option and hiding other components.当用户 select 'Reviewed' 选项并隐藏其他组件时,我正在显示可选的下拉组件。

Problem:问题:

  • Now the requirement is, user can select any option N number of times and on each selection, respective dropdown component should get added.现在的要求是,用户可以 select 任意选项 N 次,并且在每次选择时,应添加相应的下拉组件。
  • This screen also have edit functionality.此屏幕还具有编辑功能。

Note:笔记:

  • Consider this as a filter functionality with below screen将此视为具有以下屏幕的过滤器功能在此处输入图像描述

Any help / pointers would be appreciated.任何帮助/指针将不胜感激。 Thanks in advance.提前致谢。

First you should create Closed and Reviewed components首先,您应该创建 Closed 和 Reviewed 组件

输入图片描述

Then, you should have a array of filters:然后,你应该有一个过滤器数组:

data() { return {
  filters: [],
} }

When Add filter is clicked you should push corresponding component to filters, something like:单击添加过滤器时,您应该将相应的组件推送到过滤器,例如:

methods: {
  addFilter() {
    const component = Created /* */;
    this.filters.push({
      value: null,
      component: component
    })
  }
}

And finally render them in template this way:最后以这种方式在模板中渲染它们:

<div v-for="(filter, index) in filters" :key="index">
  <component :is="filter.component" />
</div>

Demo演示


For And/Or dropdowns, you can use some hacks but I'm not sure how to implement them (you can check if index is zero to only display them between filters)对于And/Or下拉菜单,您可以使用一些技巧,但我不确定如何实现它们(您可以检查索引是否为零以仅在过滤器之间显示它们)

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

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