【问题标题】:How to set initial value in v-select when using infinite scrolling使用无限滚动时如何在 v-select 中设置初始值
【发布时间】:2021-09-23 19:09:59
【问题描述】:

我已经像在the official example 中一样设置了它...但是用户的选择存储在数据库中,下次他们访问该页面时,我希望将其预设为他们之前的选择。在这种情况下,我似乎不知道该怎么做,因为 :options 是一个计算属性...

编辑:我没有提供足够的代码...我用更多信息更新了它...我确实找到了问题,这是两件事(1)@987654323 中的数据@ 与paginated 中的数据格式不同,因此无法识别初始值,并且(2)我没有使用v-model,而是使用@input,因为我需要将选择发送到数据库.通过将v-model 设置为一个虚拟数据变量并使用来自prefs 的数据以与选项列表相同的格式填充该变量,然后它就起作用了。我也会发布我的答案。

HTML

  <v-select
    :options="paginated"
    :filterable="false"
    @input="setValue"
    @open="onOpen"
    @close="onClose"
    @search="(query) => (search = query)"
  ></v-select>

JS

...
  props: ['prefs'],
...
  data: function() {
...
    countries: []
...
  computed: {
    filtered() {
      return countries.filter((country) => country.includes(this.search))
    },
    paginated() {
      return this.filtered.slice(0, this.limit)
    },
    hasNextPage() {
      return this.paginated.length < this.filtered.length
    },
  },
...
  methods: {
    setValue: function(v) {
      this.prefs = v.id;
      (... ajax call to update database ...)
    }
...
  created: {
    (... ajax call to populate this.countries from database ...)
  }

【问题讨论】:

  • 我觉得很好,有什么问题?
  • prefs 不会继续 props 但在 data:function() { return {prefs} } 当您从 ajax 获取时更新 this.prefs = [What user choice]

标签: javascript vue.js vuetify.js


【解决方案1】:

https://codepen.io/chauriya/pen/BaZOxby

请看附件pen,这里我用v-model为v-select设置了一个默认值,我看到你的prefs是一个道具,如果你将默认值传递给prefs然后它应该在 v-select 中设置。如果您在 prop 中发送一些默认值,那么请更新您的问题,它会比它更清楚。

【讨论】:

    【解决方案2】:

    我的问题是双重的:

    1. 我没有使用v-model,而是使用@input,因为我想在 AJAX 调用中将选择发送到数据库,因此没有什么可以挂钩初始值。
    2. prefs 中的数据与paginated 中的数据格式不同(字符串与对象),因此它无法在选项列表中找到初始选择。

    这里我贴出固定代码:

    HTML

    <v-select
        :options="paginated"
        :filterable="false"
        v-model="dummy"
        @input="setValue"
        @open="onOpen"
        @close="onClose"
        @search="(query) => (search = query)"
      ></v-select>
    

    JS

    ...
      props: ['prefs'],
    ...
      data: function() {
    ...
        countries: [],
        dummy: {}
    ...
      computed: {
        filtered() {
          return countries.filter((country) => country.includes(this.search))
        },
        paginated() {
          return this.filtered.slice(0, this.limit)
        },
        hasNextPage() {
          return this.paginated.length < this.filtered.length
        },
      },
    ...
      methods: {
        setValue: function(v) {
          this.prefs = v.id;
          (... ajax call to update database ...)
        }
    ...
      created: {
         let me = this;
        (... ajax call to populate this.countries from database ...)
        .then(function(resp) {
            me.countries = resp.data;
            me.dummy = me.countries.find(c => c.id == me.prefs);
        })
      }
    

    【讨论】:

    • Now that I have it working it occurs to me it is worthless when the options list is huge - is there a way to do it like SELECT2 where it gets the list a page at a time and search text发送到服务器过滤分页列表?
    猜你喜欢
    • 2018-12-25
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 2021-08-04
    • 1970-01-01
    • 2017-06-17
    • 2021-05-25
    相关资源
    最近更新 更多