简体   繁体   中英

v-select update var without v-model

Because I'm using v-select in a loop and I need to mark each of the loop iteration. I want to be able to add the selected value to an array, to check if the value exists and then add it or change it, something like this:

             <v-card-text>
                <ul>
                  <li v-for="ap in ['1','2','3']" :key="app">
                    <v-select
                      :items="findelem(appi)"
                      item-text="number"
                      item-value="number"
                     @input="check_update_array(_mark_ , _VALUE_)"
                    ></v-select>
                  </li>
                </ul>
              </v-card-text>
            </v-card>

and have check_update_array( mark , VALUE ) as a method that checks and updates the array

is this possible ?

Edit:

I'll try to be more explicit, I want to do a v-for and to iterate over an array for each selection I want to send to a function the position (value in iteration) and the selected value:

<ul>
  <li v-for="ap in ARRAY">
    <v-select
...
    @input="check_update_array(ap, SELECTED_VALUE)"
></v-select>
    </li>
  </ul>

if I do @input="check_update_array" the SELECTED_VALUE is passed as param to the function and if I do @input="check_update_array(ap)" the position in iteration is passed.

Please have a look at the following code. I took your code as a basis.

<!DOCTYPE html>
<html>
<head>
  <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">
</head>
<body>
  <div id="app">
  <v-app>
  <v-card>
    <v-card-text>
      <ul>
        <li v-for="(ap, index) in ['1','2','3']" :key="index">        
             <v-select
                    :items="findelem()"
                    item-text="number"
                    item-value="number"
                    label="My Select"
                    @input="check_update_array"
             ></v-select>
        </li>
      </ul>
      <p>
      {{ elements }}
      </p>
    </v-card-text>
    </v-card>
    </v-app>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>
  <script>
    new Vue({
      el: '#app',
      vuetify: new Vuetify(),
      data: function() {
        return {
            elements: []
        }
      },
      methods: {
        check_update_array(value) {
            this.elements.push(value);
        },
        findelem(appi) {
            return [{'number':'some element'},{'number':'another element'},{'number':'third element'}]
        }
      }
    })
  </script>
</body>

https://jsfiddle.net/ewatch/gn3eyxp2/

However some context was missing for me from your code regarding the "appi" parameter for the findelem function and the parameters " mark " and " VALUE " for the check_update_array function.

The function that is bind to the @input event receives the selected value as parameter already (see documentation here: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event ).

I hope it helps. Please let me know if you have any questions.

Best regards, ewatch

Assuming you have a data named 'selected_array'

data() {
    return {
      selected_array:[]
    }
}

Now in your component, it should be look like this.

<v-card>
   <v-card-text>
      <ul>
          <li v-for="ap in ['1','2','3']" :key="app">
                    <v-select
                      :items="findelem(appi)"
                      item-text="number"
                      item-value="number"
                     @chanage="check_update_array"
                     return-object
                    ></v-select>
                  </li>
        </ul>
    </v-card-text>
</v-card>

Then in your check_update_array function, you code should look like this.

check_update_array(data){
   let item_exist = false

   this.selected_array.forEach(item => { // check if value exists.
       if (item.value == data.value) {
          item.mark = data.mark
          item_exist = true
          return
       }
   })

  if (!item_exist) {              // if not existing then append
     this.selected_array.push(
         {mark:value, value:value}
     )
  }

 item_exist = false // reset value
}

Just make sure that your findelem(appi) is an array of objects containing mark and value elements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM