【问题标题】:why selected value get disappeared in the v-autocomplete?为什么选择的值在 v-autocomplete 中消失了?
【发布时间】:2021-08-29 19:55:56
【问题描述】:

选择的值在 v-autocomplete 中消失。我为此使用了 vuetify v-autocomplete 组件。一旦我选择了一个值,然后当我单击另一个组件或继续该过程时,所选值就会消失。

<v-autocomplete
            :search-input.sync="search"
            append-icon="mdi-chevron-down"
            v-model="selectedCode"
            item-text="name"
            item-value="code"
            class="my-input"
            solo
            :items="items"
            label="select"
          >
            <template v-slot:no-data>
              <v-list-item>
                <v-list-item-content>
                  <v-list-item-title> Type to filter</v-list-item-title>
                </v-list-item-content>
              </v-list-item>
            </template>
          </v-autocomplete>

data() {
    return {
      search: null
    };
  },


watch: {
    search(val) {
      this.festchData({
        search: val
      });
    }
  },

【问题讨论】:

    标签: vue.js vue-component vuetify.js


    【解决方案1】:

    在这种情况下,我可以通过检查 watch() 中的几个值来解决这个问题。

    <v-autocomplete
                      return-object
                      :loading="isloading"
                      clearable
                      append-icon="mdi-chevron-down"
                      v-model.lazy="trans.code"
                      solo
                      :search-input.sync="search"
                      class="transaction-input"
                      :items="merchantList"
                      :disabled="displayText
                      "
                      item-text="name"
                      item-value="code"
                      :label="displayText"
                      :rules="[rules.required]"
                    >
                      
                    </v-autocomplete>
    
    watch: {
        search(val) {
      // if the search value is empty
      if (val === undefined || val === null || val.length === 0) {
     // make dataList empty
        this.makeDataListEmpty();
        this.isloading = false;
        return;
      }
    
      // Items have already been loaded
      if (this.dataList.length > 0) return;
    
      // Items have already been requested
      if (this.isloading) return;
    
      this.isloading = true;
      Promise.all([this.fetchData(val)]).finally(() => {
        this.isloading = false;
      });
    }
      },
    

    手表正在监听动作,每次执行动作时都会触发它。所以我们需要检查是否必须在特定时间过滤数据。

    【讨论】:

    • 另外,我想添加更多内容。如果您从 API 动态获取 v-autocomplete 的数据,请检查您在传递所选值后是否从 API 收到任何数据。如果返回结果为空,则选择将不再存在。
    猜你喜欢
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 2013-02-25
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多