Home > Software engineering >  Unable to set the value to the first input field using id of the field - in vue
Unable to set the value to the first input field using id of the field - in vue

Time:11-02

I have an input field and a button (when clicked on displays a dropdown with few items) when selecting the items it has to be shown on the first input field. Similarly when clicking on the 2nd button where the dropdown is shown the selected value is shown in the 2nd input field. This entire runs in a for loop , which is where I am facing the problem.

<tr v-for="items in itemList">
  <td valign="top">&nbsp;{{items}}&nbsp;</td>
  <td align="left" nowrap>
  <input v-model="itemCode" type="text" :id="'item_code_' items" 
     @input="handleInput" 
     size="20" maxlength="27"
     autocomplete="off">
   <br/>
   </td>
   <td align="left" nowrap>
     <a id="myDropdown"  style="text-decoration:none;font- 
       size:10pt; padding-left: 10px;padding-right: 10px;"
       @click="loadFavs()"
       href="javascript:void(0)" title="Click to choose an item from your 
       favorites">
     <img hspace="3" alt="Favorites" src="/images/icons/LoadFav.png" 
         height="16" width="16"
         onm ousemove="this.style.cursor='pointer'" 
         :id="'bd_fav_image_'   items" title="Click to choose an item from 
         your favorites">
       <select  v-if="showFav" name="BOMList" 
         :id="'bd_list_' items" style="font-size:10pt;width: 100%" v- 
         model="selected" @change="selectingFav(items)">
          <option value=""></option>
          <option  v-for="(fav,index) in favList" :id="index" v- 
             bind:value="fav"  :key="fav" v-bind:index="index">{{fav}} 
             {{index}}</option>
       </select>
      </a>
      </td>
      <td valign="top" nowrap >
        <input type="Text"
           :id="'bd_qty_ '  index"
           value="" size="2"
           inputmode="numeric"
           maxlength="">
         </td>
       </tr>

favList--> this list holds a list of items , For eg:- A,B,C,D When I select A it has to be shown in the input field.

 selectingFav: function(value) {
            console.log("Inside the selectingFav..."   this.selected   "value is ."  value);
            setTheValue(value);

            
        }
function setTheValue(val){
    console.log("Inside the setThevlaue");
    if (val === 1 ){
        console.log("inside the if");
        $j('#item_code_1').val(this.selected);
        console.log("inside the if witht the value  "   $j('#item_code_1').val());
    }

Tried setting the value based on the id of the input field but nothing is showing up. If I set the v-model to the input field then all the 3 fields will be showing up the same value.

Can someone please let me know what is the issue. Hope these details are sufficient.

CodePudding user response:

a) v-model is internally implemented as:

<input v-model="myval">
<!-- is --!>
<input :model-value="myval" @update:model-value="v => myval = v">

so you can freely define your own function

<input v-for="obj, ind of whatever" @update:model-value="v => myfn(v, obj, ind)">

b) same as you have an array you v-for on you may make a parallel array

// you get the idea
data: () => ({ imputs: entries.map(e => 0) })

<div v-for="entry, ind of imputs">
  <Whatever :entry="entry"/>
  <imput v-model="imputs[ind]"> 
</div>

c) keep your imputs in objects, generally the best choice

// you get the idea
data: () => ({ imputs: entries.map(e => ({ entry: e, input: 0 })) })
// or
computed: {pairs(){ return this.entries.map(e => ({ entry: e, input: 0 })) }}

<div v-for="item of imputs">
  <Whatever :entry="item.entry"/>
  <imput v-model="item.input"> 
</div>

CodePudding user response:

Solution implemented:-
data: function(){
return{
 itemList: [
  { id: 1 , value: '' },
  { id: 2, value: '' },
  { id: 3, value: '' }]}},

methods:{
 selectingFav: function(value) {
   if (value === 1 )
      this.itemList[0].value = this.selected;
   else if(value === 2 )
      this.itemList[1].value = this.selected;
   else
      this.itemList[2].value = this.selected;
  }
}
The value holds the index

In the html:-
<tr v-for="(items,i) in itemList">
  <td valign="top">&nbsp;{{items.id}}&nbsp;</td>
  <td align="left" nowrap>
  <input v-model="items.value" type="text" :id="'item_code_' items" 
     @input="handleInput" 
     size="20" maxlength="27" autocomplete="off">
   <br/>
   </td>
  • Related