本文介绍了如何检查是否没有选定的值,然后选择第一项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Array中的每个对象内添加新值,如果没有选定的标签,它将从下拉列表中选择第一个选项.关键是还有另一个从下拉菜单中选择项目的功能.如何使它们正确协作?

I am trying to add new values inside each object in Array and if there is no selected tag then it will choose first option from drop down. The point is that there is also another function for selecting item from drop down. How can I make them work together correctly?

    <b-dropdown aria-role="list">
                    <b-button
                      icon-right="caret-down"
                      class="ToolbarButton"
                      size="is-small"
                    >
                      <span> isTagSelected(selectedProduct) </span>
                    </b-button>
    
                    <b-dropdownitem
                      v-for="selectedTag in selectedProduct.tags"
                      :key="selectedTag.id"
                      aria-role="listitem"
                      @click="selectTag(selectedProduct, selectedTag)"
                    >
                      {{ selectedTag.title }}
                    </b-dropdownItem>

js

 isTagSelected(selectedProduct) {
          if (!selectedProduct.tagTitle && selectedProduct.tags.length) {
            this.$set(selectedProduct.tags[0].id, 'tagId')
            this.$set(selectedProduct.tags[0].title, 'tagTitle')
            return selectedProduct.tagTitle
          }
    else { return selectedProduct.tagTitle}
        },

  selectTag: function(selectedProduct, selectedTag) {
      for (var i = 0; i < this.selectedProducts.length; i++) {
        if (this.selectedProducts[i].id === selectedProduct.id) {
          this.$set(this.selectedProducts[i], "tagId", selectedTag.id);
          this.$set(this.selectedProducts[i], "tagTitle", selectedTag.title);
        }
      }
    }

isTagSelected函数抛出一个错误,我们无法在此循环中使用此.$ set行.怎么解决?

isTagSelected function throws an error that can't us in for looping in this.$set lines. How can be it fixed?

推荐答案

如果我理解正确,则您正在尝试将道具添加到selectedProduct中.请让我知道这个逻辑对您来说是否正确:

If I understand correctly, you are trying to add props to selectedProduct. Please let me know if this logic looks correct to you:

 isTagSelected(selectedProduct) {
    if (!selectedProduct.tagTitle && selectedProduct.tags.length) {
        this.$set(selectedProduct, 'tagId', selectedProduct.tags[0].id)
        this.$set(selectedProduct, 'tagTitle', selectedProduct.tags[0].title)
        return selectedProduct.tagTitle
    }
    else { 
        return selectedProduct.tagTitle}
    }

这篇关于如何检查是否没有选定的值,然后选择第一项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:34