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

Utilizing Jquery and Ajax to create a dynamic dropdown selection feature based on dependencies from a JSON file

Could you assist with the code snippet below? I have a form where each dropdown list depends on the selection above it. Based on the user's selection, the appropriate data should display in the subsequent dropdown list. I am trying to make dropdown l ...

What is the significance of the term "Object object"?

I am new to javascript and encountering an issue. When I use alert in my script, the output data is shown as [Object object]. The function below is called when the button (onClick) is clicked. There are [Object object] elements in the array. The last line ...

Learn the process of extracting a particular value from JSON data and displaying it in HTML by utilizing AngularJS's ng-repeat directive

As a newcomer to angularjs, I am encountering difficulties retrieving and displaying a specific value from a json file in order to showcase it on my view page using angularjs ng-repeat for image source. My goal is to repeat the json file based on a particu ...

When trying to run a jQuery function on click or load events, an error message stating that

When I have an .on(click) event triggering an ajax call, I want the same actions to occur when the page loads as well. My idea is to create a function that contains everything within the .on(click) event and trigger this function on page load. Although I ...

Adjust the size of the Div and its content to fit the dimensions of

Currently, I have a div containing elements that are aligned perfectly. However, I need to scale this div to fit the viewport size. Using CSS scale is not an option as it does not support pixel values. https://i.stack.imgur.com/uThqx.png I am looking to ...

Could we confirm if this straightforward string is considered valid JSON data?

There are numerous intricate questions on Stack Overflow about whether a complex structure is considered valid JSON. However, what about something much simpler? "12345" Would the provided code snippet be considered valid JSON? ...

Using Vue3 to conditionally display a child div based on the class of its parent div

Just starting out with Vue, I'm currently experimenting with the active classes feature from this Vue3 carousel plugin. My goal is to only show the text and link divs for the carousel__slide--active class, but at the moment, all carousel__items are di ...

The page query functionality seems to be malfunctioning when implemented within a Gridsome component

While using <page-query> within the index works fine, I encounter an error of "edges undefined" when I try to use it inside a component. Can someone provide assistance with this issue? ...

Is it possible to prompt npm to install a sub-dependency from a GitHub pull request?

Currently, I'm in the process of setting up geofirestore, which has a dependency on geofirestore-core. However, there is an existing bug in geofirestore-core that has been addressed in a pull request. How can I make sure that my geofirestore installa ...

Acquire the text within an anchor tag using JavaScript

Is it possible to invoke a search for all links on my Wordpress blog? I'm not sure if my idea is correct. Currently, I am using an Ajax call for another search on this site. How can I extract the text from a hyperlink HTML tag? For example: <a href ...

transforming a text input into unadorned plain text

Currently, I am in the process of creating a small HTML form that consists of a single textbox input. Once the user enters text into this textbox and clicks on a button located at the end of the page, I would like the textbox to transform into normal plain ...

Guide to modifying the root directory when deploying a Typescript cloud function from a monorepo using cloud build

Within my monorepo, I have a folder containing Typescript cloud functions that I want to deploy using GCP cloud build. Unfortunately, it appears that cloud build is unable to locate the package.json file within this specific folder. It seems to be expectin ...

The challenge of mocking methods/hooks remains when utilizing `jest.spyOn` in Jest

I am attempting to create mock methods and hooks in a file, then import those mock functions as needed in my test files. useMyHook.jsx const useMyHook = () => { const [val, setVal] = useState(200) return { val } } export { useMyHook } Hello.jsx: ...

Populate a form with database information to pre-fill the fields

So I have this web form built with HTML, and there are certain values within the form that can be changed by the user. To ensure these changes are saved, I'm storing them in a database. My goal is to have the form automatically filled with the data fr ...

Error during deployment on Vercel: Module 'build-rss.js' not found in workpath0 directory

One of my package.json scripts looks like this: { "export": "next export", "build": "next build && npm run export && npm run build:rss", "build:rss": "node ./.next/server/scripts/bui ...

Guide to generating a text string by utilizing the foreach loop

Is there a way to combine text strings from interfaces into a single file for display in UI? The current code is generating separate files for each interface. How can I achieve the expected result of having all interfaces in one file? Additionally, is it ...

Is checking for an email address in a form necessary?

I am currently in the process of creating a coming soon page. I have included a form on the page where users can sign up using their email addresses, which are then sent to me via email. However, I need assistance in determining how to verify that the in ...

Stop RequireJS from Storing Required Scripts in Cache

It seems that RequireJS has an internal caching mechanism for required JavaScript files. Whenever a change is made to one of the required files, renaming the file is necessary in order for the changes to take effect. The typical method of adding a version ...

Refresh Ajax/Javascripts following the loading of new data with .html() function

My website has a page that utilizes an ajax function to load data into the page. The ajax function is as follows: $(document).ready(function(){ function loadData(page){ $.ajax ...

Difficulty in toggling on and off several form elements with JavaScript

Trying to control multiple form elements on an HTML page with JavaScript has presented a challenge for me. In my form, each row contains a checkbox that should enable/disable the elements on that row. The issue I'm facing is that only the first two f ...