Set initial vuetify v-select value

后端 未结 2 848
南笙
南笙 2020-12-28 16:04

Can someone help me set the default value for a v-select? The caveat is that my v-select is populated with objects, which I think might be the reas

2条回答
  •  死守一世寂寞
    2020-12-28 17:08

    Alternative answer for those finding this question from a search...

    How to select default value using an attribute of an object

    
    
    
    

    Fiddle example: https://jsfiddle.net/4c3tbj6m/

    Explain usage:

    v-model is a special interfacing method for the value attribute of an field.

    item-value="id" tells the input field what attribute of a selected object item row to set as the value of input.user_id

    item-text="name" tells the field what attribute of a selected object item row to use to display as the selected text.

    See Official Documentation on v-model.

    Example above is a select field so v-model is representing value of what would be a selected attribute of an option element like the following:

    The value of input.user_id is the selected item (value set by the v-model bind)

    You can then POST the entirety of input (if more input fields are added) but in this case there is only user_id in there:

    
    methods: {
    
      save() {
    
        axios.post('/my/api/example', this.input).then(response => {
    
          console.log(response);
    
        })
    
      }
    
    }
    
    
    SAVE
    

    This will POST a JSON object to your servers end point /my/api/example formatted like this:

    {
      "user_id": 1
    }
    

提交回复
热议问题