简体   繁体   English

如何处理多个选择下拉组件及其值?

[英]How do I handle multiple select dropdown component with their values?

I have created one component for select dropdown list. 我为选择下拉列表创建了一个组件。 My requirement is to add more of these component when your press add row button. 我的要求是在您按下添加行按钮时添加更多这些组件。 I am able to add this component on add row button press. 我可以在按下添加行按钮时添加此组件。

I am having trouble with their value. 我在他们的价值上遇到麻烦。 Example lets say there are four option ['audi', 'bmw', 'volvo', 'tesla']. 例如,假设有四个选项['audi','bmw','volvo','tesla']。 When the user select any value out of these like "audi" and then press Add row button. 当用户从其中选择任何值(例如“ audi”),然后按“添加行”按钮。

New drop list appear with only 3 values ['bmw', 'volvo', 'tesla'] and now they have selected "bmw". 新的下拉列表仅显示3个值['bmw','volvo','tesla'],现在它们选择了“ bmw”。 My issue is when user again select value from the first/previous dropdown component - There should be only ['audi', 'volvo', 'tesla'], excluding "bmw". 我的问题是,当用户再次从第一个/上一个下拉菜单中选择值时-应该只有['audi','volvo','tesla'],不包括“ bmw”。 How can I achieve this? 我该如何实现?

I have created an array which store all other arrays option. 我创建了一个存储所有其他数组选项的数组。 Later when I connect to the API, I am going to create array of an object. 稍后,当我连接到API时,我将创建一个对象数组。

Please let me know. 请告诉我。 Thanks 谢谢

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <button :disabled="isDisabled" @click="addRow">Add Row</button>
    <table>
      <tr>
        <td>
          <div v-for="(n, i) in defaultRow" :key="i">
            <DropDowns @dropDownSelect="onDropDownSelect" :lists="allLists[i]"></DropDowns>
          </div>
        </td>
      </tr>
    </table>
  </div>
</template>

<script>
import DropDowns from "./components/DropDowns.vue";

export default {
  name: "app",
  data() {
    return {
      defaultRow: 1,
      allLists: [["audi", "volvo", "mec", "toyota"]],
      selectedValue: ""
    };
  },
  components: {
    DropDowns
  },
  computed: {
    isDisabled() {
      return this.defaultRow === this.lists.length ? true : false;
    }
  },
  methods: {
    addRow() {
      if (this.defaultRow < 4) {
        console.log('selectedValue',this.selectedValue)
        if (this.selectedValue) {
          this.allLists.push(this.allLists[this.defaultRow - 1].filter(l => l !== this.selectedValue));
          this.allLists[this.defaultRow - 1] = this.allLists[this.defaultRow - 1].filter(l => l === this.selectedValue )
          console.log("TCL: addRow -> this.lists", this.allLists);
        }
      }
    },
    onDropDownSelect(selected) {
      this.selectedValue = selected;
      console.log(
        "TCL: onDropDownSelect -> this.selectedValue",
        this.selectedValue
      );
    }
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

Dropdown component: 下拉组件:

<template>
  <div>
    <select v-model="selected" @change="getValue">
      <option disabled value="">Please select one</option>
      <option v-for="list in lists" :value=list :key=list>{{list}}</option>
    </select>
    {{selected}}
  </div>
</template>

<script>
export default {
    props: ['lists'],
    data() {
      return {
        selected: ''
      }
    },
    methods: {
      getValue() {
        this.$emit('dropDownSelect', this.selected)
      }
    }
};
</script>

<style scoped>
</style>


Right now, once user select value from select. 现在,一旦用户从select中选择值。 They can only see that value. 他们只能看到该值。

This is a way to do it : 这是一种方法:

check the sample : https://codesandbox.io/s/vue-template-5jbmz ; 检查样本: https : //codesandbox.io/s/vue-template-5jbmz ;

I commented what i've changed above the line .... basically your main issue was that your defaultRow was not incrementing ....so you were just pushing an empty array to your allLists 我评论了我在行上方所做的更改....基本上,您的主要问题是您的defaultRow没有递增..所以您只是将空数组推入allLists

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

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