【问题标题】:Without using v-model, how to set default value of v-combobox?在不使用 v-model 的情况下,如何设置 v-combobox 的默认值?
【发布时间】:2020-05-26 05:49:11
【问题描述】:

我正在创建一个带有 v-combobox 组件的表单。

:items 正在从我的数据库中的表中检索,我正在使用 v-model 将选择的任何内容输入到数据库中的不同表中,因此我使用 v-model 链接到新的数据库表.

我知道我可以使用 v-model 来设置默认值(意味着自动选择该项目),但是有没有其他方法可以做到这一点,因为我目前正在使用 v-model 连接到数据库?

表格:

<v-combobox
    outlined
    :items="allTypes"
    v-model="plates[plates.findIndex(obj => obj.id === 1)].value"
    :search-input="search"
>
</v-combobox>

【问题讨论】:

  • 您是否尝试过仅设置组件的 value 属性?另外,请停止在模板中硬编码 id,这让我很难过。
  • 我已经尝试指定值、项目值、项目文本,但到目前为止没有任何工作,哈哈抱歉我知道硬编码效率低下,但在这种情况下它是必要的(造型目的)

标签: vue.js vuetify.js


【解决方案1】:

如果我按照您在此处尝试完成的操作... 您想在组合框上设置一个默认值,而不是它与 v-model 绑定的数据的真实值?

我不确定这是个好主意。因为如果用户想要默认值,他们会留下它,认为“它已经设置为我想要的”,但它实际上并不能准确反映他们将设置的值?

无论哪种方式,听起来您可能希望将逻辑从当前的 v-model 属性移动到计算属性,这样您就可以分离获取和设置的逻辑。

已更新代码片段

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data() {
    return {
      // the set value, initially false to use as a 'has a value been set' flag
      setValue: false,
      // the default value shown before selection has happened
      defaultValue: "Pick a value (default)",
      // the options to select from
      allTypes: [
        'dogs',
        'cats',
        'hamsters',
        'something else',
      ],
      // your table data
      table: {
        value: "null (initially)"
      }
    }
  },
  computed: {
    // the value the we are binding the combo box to
    comboBoxValue: {
      // the value to output
      get() {
        // if a value has not been set
        if (!this.setValue) {
          // return the default 
          return this.defaultValue;
        } else {
          // else return whatever you would like
          return this.setValue;
          /* plates[plates.findIndex(obj => obj.id === 1)].value */
        }
      },
      // the function that happens when the combo box is set
      set(val) {
        // val is the value the combox HAS been set to, not something you define here
        console.log(val);
        // update your table, pass in "val" the combo box value
        this.updateTable(val);
        // update the set value, combo box no longer show the default value
        this.setValue = val;

      }
    }
  },
  methods: {
    // some method to update your table
    updateTable(newValue) {
      alert('update table with value: ' + newValue)
      this.table.value = newValue;
    }
  }
})
<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@4.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<div id="app">
  <v-app>
    <v-content>
      <v-container fluid>
        <v-row>
          <v-col cols="12">
            table value: {{table.value}}
          </v-col>
          <v-col cols="12">
            <v-combobox outlined :items="allTypes" v-model="comboBoxValue">
            </v-combobox>
          </v-col>
        </v-row>
      </v-container>
    </v-content>
  </v-app>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>

【讨论】:

  • 感谢您的回答! if else 语句有效,但是我认为我可能错误地编写了 set(value) 函数,因为它没有正确发布到数据库: set(newValue) { var newValue =plates[plates.findIndex(obj => obj.id == = 1)].value }
  • 是的,我想你是。使用 set(newValue){},newValue 是组合框在更改时发出的值。您无需在函数内声明或分配它。例如:set(newValue){ console.log(newValue);每次设置时都会将组合框中的值记录到控制台(如果您没有看到预期的结果,这是排除故障的良好第一步)。不看代码很难给出更多细节。我不确定您的数据中的“allTypes”或“plates”是什么样的。但我相信您正在寻找类似 set(val){ /*databaseValue = val*/ }; 的东西
  • 我认为这是正确的写法: newValue = this.plates.find(obj=>obj.id===8).data 但是,当我控制台记录 newValue 时,什么都没有返回,甚至没有一个未定义的。它告诉我,由于某种原因,我实际上并没有将 NewValue 的值(正确注册为我选择的下拉菜单)分配给数据库中的数据列。
  • 这可能是您想要使用的语句,但这不是您在计算属性中使用 set 方法的方式。括号中的变量名称 (newValue) 是组合框 被设置为 的值,而不是它 将被设置的值。因此,您无需在 set 函数中分配值,而是使用值。我已经用代码 sn-p 更新了我的答案,给你一个更好的例子。
  • 在你的语句 newValue = this.plates.find(obj=>obj.id===8).data 中,值 8 是从哪里来的?我想你可能想尝试一些数据:{ setValue:false, defaultValue:"default", allTypes:[...] },computed:{ comboBoxValue:{ get(){ if(!this.setValue) { return(this.defaultValue;) }else{ return(this.plates.find(obj=>obj.id===this.setValue).data); } },set(val){ this.setValue=val; } } 所以get方法要么返回默认值,要么返回设定值的车牌数据。但是您可能需要修改组合框用于值的字段。
猜你喜欢
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-22
  • 2019-04-29
  • 1970-01-01
相关资源
最近更新 更多