Vue2 Vuetify v-autocomplete:如何获取v-autocomplete的搜索结果/筛选项

huangapple go评论66阅读模式
英文:

Vue2 Vuetify v-autocomplete: How do I get the searched result/filtered item of v-autocomplete

问题

Currently, select all will select all from the list which include filtered/un-filtered (screenshot: 67 item(s) selected although it has been filtered to 4). I need that select all to just select those 4 items (yellow box).

问题是如何获取/访问v-autocomplete的筛选项?

看到一个帖子几乎问了和我的问题一样的问题,但2019年没有答案。有人提到要使用

> this.$refs.autoCompleteRef.filteredItems

但这并不起作用。

英文:

I implement searching/filtering within v-autocomplete with multiple selection and select all.
Currently select all will select all from the list which include filtered/un-filtered (screenshot: 67 item(s) selected although it has been filtered to 4). I need that select all to just select those 4 items (yellow box).

Vue2 Vuetify v-autocomplete:如何获取v-autocomplete的搜索结果/筛选项

Question is how do I get/access the v-autocomplete's filtered items?

Saw a post which asked almost same question as mine, but no answer from 2019. Someone mentioned to use

> this.$refs.autoCompleteRef.filteredItems

But its not working.

答案1

得分: 0

这是您提供的代码段的翻译:

它正如所述的那样运作。

请查看游乐场。最好在全屏模式下检查它。

新的 Vue({
  vuetify: new Vuetify(),
  el: '#app',
  data () {
    return {
      model: null,
      filter: '',
      searchedItems: [],
      states: [
        '阿拉巴马州', '阿拉斯加州', '美属萨摩亚', '亚利桑那州',
        '阿肯色州', '加利福尼亚州', '科罗拉多州', '康涅狄格州',
        '特拉华州', '哥伦比亚特区', '密克罗尼西亚联邦',
        '佛罗里达州', '乔治亚州', '关岛', '夏威夷州', '爱达荷州',
        '伊利诺伊州', '印第安纳州', '衣阿华州', '堪萨斯州', '肯塔基州',
        '路易斯安那州', '缅因州', '马绍尔群岛', '马里兰州',
        '麻萨诸塞州', '密歇根州', '明尼苏达州', '密西西比州',
        '密苏里州', '蒙大拿州', '内布拉斯加州', '内华达州',
        '新罕布什尔州', '新泽西州', '新墨西哥州', '纽约州',
        '北卡罗来纳州', '北达科他州', '北马里亚纳群岛', '俄亥俄州',
        '俄克拉荷马州', '俄勒冈州', '帕劳', '宾夕法尼亚州', '波多黎各',
        '罗德岛州', '南卡罗来纳州', '南达科他州', '田纳西州',
        '德克萨斯州', '犹他州', '佛蒙特州', '维尔京群岛', '弗吉尼亚州',
        '华盛顿州', '西弗吉尼亚州', '威斯康星州', '怀俄明州'
      ]
    }
  },
  watch: {
    filter() {
      this.searchedItems = this.$refs['selectExample']?.filteredItems
    }
  }
})
#app { 行高:3; }
[v-cloak] { 显示:无; }
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.x/css/materialdesignicons.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet" />
<div id="app">
  <v-app id="inspire">
    <v-card>
      <v-card-title class="headline font-weight-regular blue-grey white--text">个人资料</v-card-title>
      <v-card-text>
        搜索到的项目:{{ searchedItems }}
        <v-subheader class="pa-0">你住在哪里?</v-subheader>
        <v-autocomplete
          v-model="model"
          :items="states"
          label="州"
          :search-input.sync="filter"
          ref="selectExample"
        >
        </v-autocomplete>
      </v-card-text>
    </v-card>
  </v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>
英文:

It works exactly as mentioned.

See the playground. It's better to check it in full-page mode.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

new Vue({
  vuetify: new Vuetify(),
  el: &#39;#app&#39;,
  data () {
    return {
      model: null,
      filter: &#39;&#39;,
      searchedItems: [],
      states: [
        &#39;Alabama&#39;, &#39;Alaska&#39;, &#39;American Samoa&#39;, &#39;Arizona&#39;,
        &#39;Arkansas&#39;, &#39;California&#39;, &#39;Colorado&#39;, &#39;Connecticut&#39;,
        &#39;Delaware&#39;, &#39;District of Columbia&#39;, &#39;Federated States of Micronesia&#39;,
        &#39;Florida&#39;, &#39;Georgia&#39;, &#39;Guam&#39;, &#39;Hawaii&#39;, &#39;Idaho&#39;,
        &#39;Illinois&#39;, &#39;Indiana&#39;, &#39;Iowa&#39;, &#39;Kansas&#39;, &#39;Kentucky&#39;,
        &#39;Louisiana&#39;, &#39;Maine&#39;, &#39;Marshall Islands&#39;, &#39;Maryland&#39;,
        &#39;Massachusetts&#39;, &#39;Michigan&#39;, &#39;Minnesota&#39;, &#39;Mississippi&#39;,
        &#39;Missouri&#39;, &#39;Montana&#39;, &#39;Nebraska&#39;, &#39;Nevada&#39;,
        &#39;New Hampshire&#39;, &#39;New Jersey&#39;, &#39;New Mexico&#39;, &#39;New York&#39;,
        &#39;North Carolina&#39;, &#39;North Dakota&#39;, &#39;Northern Mariana Islands&#39;, &#39;Ohio&#39;,
        &#39;Oklahoma&#39;, &#39;Oregon&#39;, &#39;Palau&#39;, &#39;Pennsylvania&#39;, &#39;Puerto Rico&#39;,
        &#39;Rhode Island&#39;, &#39;South Carolina&#39;, &#39;South Dakota&#39;, &#39;Tennessee&#39;,
        &#39;Texas&#39;, &#39;Utah&#39;, &#39;Vermont&#39;, &#39;Virgin Island&#39;, &#39;Virginia&#39;,
        &#39;Washington&#39;, &#39;West Virginia&#39;, &#39;Wisconsin&#39;, &#39;Wyoming&#39;
      ]
    }
  },
  watch: {
    filter() {
      this.searchedItems = this.$refs[&#39;selectExample&#39;]?.filteredItems
    }
  }
})

<!-- language: lang-css -->

#app { line-height: 3; }
[v-cloak] { display: none; }

<!-- language: lang-html -->

&lt;link href=&quot;https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/@mdi/font@6.x/css/materialdesignicons.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;div id=&quot;app&quot;&gt;
&lt;v-app id=&quot;inspire&quot;&gt;
  &lt;v-card&gt;
    &lt;v-card-title class=&quot;headline font-weight-regular blue-grey white--text&quot;&gt;Profile&lt;/v-card-title&gt;
    &lt;v-card-text&gt;
      searchedItems: {{ searchedItems }}
      &lt;v-subheader class=&quot;pa-0&quot;&gt;Where do you live?&lt;/v-subheader&gt;
      &lt;v-autocomplete
        v-model=&quot;model&quot;
        :items=&quot;states&quot;
        label=&quot;State&quot;
        :search-input.sync=&quot;filter&quot;
        ref=&quot;selectExample&quot;
      &gt;
      &lt;/v-autocomplete&gt;
    &lt;/v-card-text&gt;
  &lt;/v-card&gt;
&lt;/v-app&gt;
&lt;/div&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月11日 16:03:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定