Sharing data from parent to child components in Vue.js: A guide to passing references

I need some help with this code snippet

HTML Code:

<div class="col-xs-4">
    <h3 class="text-center">Incomplete task</h3>
        <div class="well" style="max-height: 300px;overflow: auto;">
            <ul id="check-list-box" class="list-group checked-list-box">

                    <li v-for="task in incompleteTasks" class="list-group-item">
                        <input type="checkbox" name="" @click="addToDone">
                        {{task.description}}
                    </li>
              <!-- <li v-for="task in incompleteTasks" v-text="task.description" class="list-group-item">
              <input type="checkbox" class="" /></li> -->
            </ul>
            <br />
        </div>
</div>

Here's the JavaScript (JS) Code:

let data = {
                heading:'Task List',
                tasks :[
                        {description: 'GO to store', completed : false},
                        {description: 'SignUp Page', completed :false },
                        {description: 'Create New team', completed : false},
                        {description: 'Add Entity', completed : false},
                        {description: 'Add WorkFlow', completed : false}
                    ]
            };

new Vue({

  el: '#root',

  data: data,

  methods: {
      addToDone() {

        this.completed = true;
        console.log(this);
      },
  },

  computed: {

      heading() {

        return this.heading;
      },

      incompleteTasks() {

        return this.tasks.filter(task => !task.completed);
      },

      completeTasks() {

        return this.tasks.filter(task => task.completed);
      }
  }

})

When a checkbox is clicked, I want to toggle the 'completed' property of that specific list item to True.

Could someone please guide me on how to achieve this?

Thank you!

Answer №1

Is it necessary to pass the task that needs modification in the method, as shown below:

<li v-for="task in incompleteTasks" class="list-group-item">
   <input type="checkbox" name="" @click="addToDone(task)">
                    {{task.description}}
</li>

Then, within the method, mark it as completed:

methods: {
    addToDone(task) {
      task.completed = true;
      console.log(task);
    },
},

Answer №2

If you are looking to modify an element based on its index, consider implementing the following approach

<li v-for="(item, index) in itemsList" class="list-item">
   <input type="checkbox" @click="updateItem(index)">
     {{item.name}}
 </li>

Inside the method:

updateItem(index) {
  this.$set(this.items[index], 'modified', true);
},

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A tool that showcases outcomes determined by the interaction of two variables

I have two select inputs: <select id="inputA"> <option>1</option> <option>2</option> <option>3</option> </select> <select id="inputB"> <option>1</option> <option>2</opti ...

Guide on fetching data from a database using Node Js in a hierarchical structure

I am currently developing a NodeJs backend code to retrieve data from the database. The desired structure of the data should look like this: data = [{ name: "Admin", id: '1', children: [ { name: "Admin", id: "1& ...

It appears that Yarn is having trouble properly retrieving all the necessary files for a package

Recently, I encountered a strange issue in our project involving 3 microservices, all using the exceljs library. Two of the microservices successfully download all necessary files for this package through yarn. However, the third microservice is missing ...

I am able to access the JSON file from the URL without any issues, but for some reason, I am unable to fetch it using $

(function(){ $(document).ready(function(){ $.getJSON('http://dev.markitondemand.com/MODApis/Api/v2/Quote/json?symbol=AAPL&callback=?', function(){console.log("function was run");}); }); }()) I recently delved into working with APIs. Ho ...

Utilize a map image as a texture on a plane within a personalized shader in THREE.js

I'm currently facing an issue where I need to load two images as textures, blend between them in the fragment shader, and apply the resulting color to a plane. However, I am struggling to even display a single texture properly. My process for creatin ...

Can you tell if there are any distinctions between the two code snippets?

Initial Code console.log('Begin'); // output 1 await axios({ method: 'post', url: '<HTTP_URL>' data: <SOME_DATA>, }).then ((response) => { // Performing some action... This may take a few seconds. con ...

I'm having some trouble with this search filter in Vue 2 - is it failing to display the items as expected

After struggling with this issue for over a week, I've hit a roadblock and need some assistance. I'm currently working on implementing a search filter in Vue 2 with Vuetify, but something isn't quite right. Here's a snippet of the sea ...

What sets apart $document from $window.document in Angular?

After experimenting with the code on JSBin, I noticed that the first snippet successfully retrieved the canvas object, while the second one did not. JSBin: http://jsbin.com/natavejasa/1/edit?html,js,output var canvas = $window.document.getElementById(&ap ...

Utilize AngularJS to Access Global JavaScript Variables

I recently encountered a situation where I needed to incorporate some dynamic functionality into a website I was building. This led me to AngularJS, which I decided to integrate into certain parts of the site rather than the entire thing. Within my JavaSc ...

Can the issue of mangling be avoided during .NET minification?

Currently, I am utilizing the .NET Bundling and Minification feature to bundle and minify the files for my application. While this method works well, I am interested in exploring alternative solutions due to its complexity. Specifically, I am attempting t ...

I am uncertain about whether it would be better to utilize URL path parameters or query parameters for filtering on the back-end of the application

Suppose I am trying to retrieve all car models from a specific brand. This can be achieved by utilizing URL path parameters or query parameters as shown below: router.get('/modelsByBrand', modelController.getModelsByBrand); To make the GET reque ...

Establish a connection to a regular socket by utilizing WebSocket technology

I am attempting to connect a client interface to a hardware device using WebSocket in a browser like Chrome. The code snippet I have been using for connection is as follows: var ws = new WebSocket("ws://127.0.0.1:13854"); ws.onopen = function() ... Howev ...

Unable to modify app.message to 'something' in the console

While experimenting with vue.js at https://v2.vuejs.org/v2/guide/, I added the file to my view. The First App example successfully displays "Hello Vue!" when the app loads. However, upon trying to change app.message from the console, the title does not up ...

Running "vue ui" with Node.js v17.2.0 - A step-by-step guide

After updating to Node.js v17.2.0, I am facing issues with running "vue ui" in my project. The error message I receive indicates a problem with node modules: at Object.readdirSync (node:fs:1390:3) at exports.readdir (/usr/local/lib/node_modules/@vu ...

Having Difficulty Applying a Background Color to a Class in Bulk

I am working on a unique HTML canvas for pixel art, which is created using a table made up of various divs all sharing the "pixel" class. I want to add a clear button that can reset the entire canvas, but changing the background color of each individual di ...

Issue with Vue Apollo 4 fetchin wrong data from GraphQL API

We are currently facing an issue while trying to utilize version 4 of Vue apollo along with the composition API following the steps outlined in this particular tutorial. Despite our efforts, we are unable to retrieve any results from a standard graphql que ...

Acquire the key name of an object during iteration in Jade

const information = { attribute1: 'value1', attribute2: 'value2', attribute3: 'value3' }; for each value, key in information li= value.keyname?? The desired result should be: <li>attribute1</li> <li ...

The response body from the HTTP request is consistently empty or undefined

While working with two containers managed by Docker Compose, I encountered an issue where the response body from a GET call made by a Node.js service in one container to an API in another container using the request module was consistently empty or undefin ...

Guide on sending a JSONP POST request using jQuery and setting the contentType

I'm struggling to figure out how to make a jsonp POST request with the correct content type of 'application/json'. Initially, I was able to send the POST request to the server using the jQuery.ajax method: jQuery.ajax({ type: ...

Issues with Javascript code syntax

I have encountered an issue while creating a new literal control from code behind. I am able to add standard HTML into it successfully, however, when I try to insert <%= SomeDiv.ClientID %> into the literal control, it simply prints exactly what is i ...