0

Hi All,

I have created a simple CRUD operation in VueJS. The values are inserting in a list , i can read it from there only and also can delete it from the list. But issue is when i am trying to update any value, its not getting update. Can any one help me out with it. Am i doing anything wrong. Please let me know.

<html>
<head>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>    
</head>
    <body>
    <div id="app">
        <form v-on:submit.prevent="addToDo">
            <input v-model="currentTodos" type=text><br><br>
            <button type="submit">Add To List</button>
        </form>
        <ul>
            <li v-for="(todo, index) in todos">
                {{todo}}<button v-on:click="deleteItemFromList(index)">X</button><button v-on:click="updateItemList(index)">#</button>
            </li>
        </ul>
    </div>
    <div v-html="htmlContent"></div>    
    </body>
    <script>
        new Vue({
            el: "#app", // el is the DOM element.
            data:{
                 todos:['push', 'pop', 'paste'],
                 currentTodos: '',
                htmlcontent : "<div><input type="text"></div>"
            },
            methods:{
                addToDo: function(){
                 this.todos.push(this.currentTodos);
                 this.currentTodos = '';   
                },

                deleteItemFromList: function(index){
                    this.todos.splice(index,1)
                },

                updateItemList: function(index){
                    this.todos.set(index,1)
                }
            }
        })
    </script>
</html>
7
  • I think array did not have set method. Commented May 3, 2019 at 10:04
  • @bcjohn can you help out with the set method, as i am a newbie in Vue, Commented May 3, 2019 at 10:10
  • 1
    this.todos[index] = newVal. Just change the value of array is fine. Commented May 3, 2019 at 10:13
  • what does this newVal do. Commented May 3, 2019 at 10:15
  • It is a new value that you want to replace the old ones. Commented May 3, 2019 at 10:17

1 Answer 1

1

Reactivity is not working here. Assign a new Array reference when changin todos array or you can use this.$set(...) to get the reactivity working.

methods:{
  addToDo: function(){
    this.todos.push(this.currentTodos);
    // assign a new Array reference in this.todos
    this.todos = [...this.todos];
    this.currentTodos = '';   
  },

  deleteItemFromList: function(index){
    this.todos.splice(index,1);
    this.todos = [...this.todos];
  },

  updateItemList: function(index){
    this.todos[index] = '1';
    this.todos = [...this.todos];
  }
}
<template>
...
<li v-for="(todo, index) in todos" :key="todo">
    {{todo}}
    <button v-on:click="deleteItemFromList(index)">X</button>
    <button v-on:click="updateItemList(index)">#</button>
</li>
...
</template>

And also add :key="todo" in v-for

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

6 Comments

hi sir, i have tried your code and added "<li v-for="(todo, index) in todos"> {{todo}}<button v-on:click="deleteItemFromList(index)">X</button v-on:click="updateItemList(index)"><button>Update</button> </li>" this in my html code but it is not working
i have added update button and passing the index of a select value
Try this.todos[index] = 1; & here the updated value = 1 (hard coded)
do you see any error in chrome console? anyway, addToDo() & deleteItemFromList() are working?
yes those methods are working but updateItemList() is not working
|

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.