2

Using Laravel 5.4 and Vuejs 2.1

In code I have a select field with two input fields (quantity and stock) in tr (Table Row). Table Row with the fields can be dynamically added as much as user is needed, this is the code:

<tbody id="loads-content">
  <tr v-for="(item, index) in items">
     <td>
         <select v-model="item.load_id" name="load_id[]" class="loads" v-on:change="getLoadStock()">
             <option v-for="load in loads" :value="load.id" :id="load.id">{{load.name}}</option>
         </select>
     </td>
     <td><input type="text" v-model="item.quantity" name="quantity[]" class="input is-hovered qty"></td>
     <td><input type="text" v-model="item.stock" class="input is-hovered stock" disabled readonly="true"></td>
     <td>
         <button type="button" class="button is-danger remove" @click="items.splice(index, 1)"><i class="fa fa-minus-square-o" aria-hidden="true"></i></button>
     </td>
     </tr>
     <a @click="addLine" class="button is-success"><i class="fa fa-plus-square-o" aria-hidden="true"></i></a>
</tbody>

When you choose some value from select field I need to populate the STOCK input field with stock data from the database. For that I used an API call. I made something like this:

methods: {
        addLine() {
            this.items.push({
                load_id: '',
                quantity: '',
                stock: ''
            })
        },

        getLoadStock(){
            var loadsContent = $('#loads-content');
            var tr = loadsContent.parent().parent();
            var id = tr.find('.loads').val();

            axios.get('/api/load/' + id)
                    .then(function(response) {
                        tr.find('.stock').val(response.data.stock);
                    })
                    .catch(function(error) {
                        console.log(error)
                    });

        },

This code is not working as expected.

The goal is to fetch actual stock for current choosen load, to see how much quantity can you enter in the input field for quantity.

I am open for any suggestions, if anyone has a better approach and solution please help.

Thanks in advance! :)

1 Answer 1

3

You are mixing two different and incompatible ways of building a web app: Vue.js and jQuery.

When using Vue, you should not be manipulating the DOM directly. You should instead be binding the DOM elements to Vue model attributes. Then, if you need a DOM element to reflect a change, you change the model – not the DOM.

So for instance, you would want something like this (note adding index as an argument to getLoadStock):

     <select v-model="item.load_id" name="load_id[]" class="loads" v-on:change="getLoadStock(index)">
         <option v-for="load in loads" :value="load.id" :id="load.id">{{load.name}}</option>
     </select>

and

    getLoadStock(index){
        axios.get('/api/load/' + this.items[index].load_id)
                .then(function(response) {
                    this.items[index].stock = response.data.stock;
                })
                .catch(function(error) {
                    console.log(error)
                });

    },
Sign up to request clarification or add additional context in comments.

1 Comment

you nailed it! Thank you very much (Also thanks for explanation in the second sentence). Have a nice weekend :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.