Implementing a checkbox in every row with Vue.js - a beginner's guide

Does anyone know how to implement checkboxes in each row using Vue.js? I need to pass the checked items to a function for further processing, but my attempts so far have not been successful. Below is the code snippet along with the output from an API.

The selected IDs should be passed to the "on()" function.

        <va-card style="margin-top:20px">
         <table  style="width:100%;border:2px">
                <thead>
                  <tr>
                    <th>Pole ID</th>
                    <th>Address</th>
                   <th>     
                    <label class="form-checkbox">
                      <input type="checkbox" v-model="selectPole"  >
                      <i class="form-icon"></i>
                    </label>
                  </th>
                  </tr>
                </thead>
                <tbody>
                <tr v-for="pole in streetInfo">
                  <td style="font-weight:bold">{{pole.pole}}</td>
                 <td>{{pole.address}}</td>
                  <td>       
                   <label class="form-checkbox">
                  <input type="checkbox" :value="pole.pole" v-model="selectedStreetLight">
                <i class="form-icon"></i>
                </label>
              </td>
             </tr>
           <va-button  @click="On()" class="my-0">ON</va-button>
            <va-button   @click="Off()" class="my-0">OFF</va-button>
            </tbody>
           </table>
          </va-table>
       </va-card>

//API Data Output

        [
          {
            "address": "Jayanagar 4th Block Banglore", 
            "pole": "BNG-JAY-137-003",
            "select":false
          }, 
          {
            "address": "Jayanagar 5th Block Banglore", 
            "pole": "BNG-JAY-137-004",
            "select":false
          }, 
          {
            "address": "Jayanagar 6th Block Banglore", 
            "pole": "BNG-JAY-137-005",
            "select":false
          }
        ]

Answer №1

        <tr v-for="(num,pole) in streetData">
          <td style="font-weight:bold">{{pole.pole}}</td>
         <td>{{pole.address}}</td>
          <td>       
           <label class="form-checkbox">
          <input @change="update(num)" type="checkbox" :value="pole.pole" v-model="selectedStreetLight">
        <i class="form-icon"></i>
        </label>
      </td>

Changed the variables to (num,pole) in streetData and the method call to @change="update(num)

Implemented a new method update(num) where you can customize the functionality.

update(num)
{
    this.streetData[num].isSelected = !this.streetData[num].isSelected;
}

Hope this solution is helpful for your needs

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

Converting data from a JSON-like file format into valid JSON using JavaScript

I have a unique situation where I am dealing with numerous files that have an unusual file extension. My goal is to utilize JavaScript to read these files and then convert their contents into either JSON or regular JavaScript objects. Is this task even fe ...

What steps can I take to prevent the div from exiting the screen during animations?

Hello, I am a novice in the field of web development and I am attempting to create a website where, upon loading it, a text with a typing animation will appear centered in the middle. Eventually, the remaining words in the div will be 'Nice Paws' ...

Tips for adding content to a textarea with JavaScript without disrupting the editing history

I have a specific requirement where I want the user to be able to highlight text in a textarea, then press ctrl + b to automatically surround that selected text with stars. Here is what I envision happening: 1) The initial content of the textarea is: "he ...

What does the "Undefined" group label mean in Vue-Multiselect?

I recently started learning Vue and have revamped this code based on information from different tutorials. However, I am encountering an issue with the group name showing as "Undefined" here. .html: <multiselect v-model="value" :options="op ...

What is causing the script blocks to exponentially increase in size within the VS2010 Debugger while executing an ASP.Net page with partial postbacks?

When working on an ASP.Net page that utilizes an UpdatePanel with validated controls for partial postbacks, an issue arises with the Visual Studio 2010 script debugger window. The debugger window starts displaying a continuous list of "Script Block" entrie ...

An easy way to enable mobility for BootstrapDialog on mobile devices

Currently, I am utilizing the library available at https://github.com/nakupanda/bootstrap3-dialog in order to create a dialog box. However, my issue arises when viewing the dialog on mobile devices where it exceeds the screen size. On desktops, adjusting t ...

sending data from an AngularJS application to an MVC controller in JSON format containing multiple arrays

Currently, I am working on a project that involves using AngularJS and MVC. I am transferring data from an AngularJS controller to my MVC controller using $http.post(). At the moment, I am using a single object or JSON array to retrieve data as follows: pu ...

This guideline never refreshes the information it contains

I'm currently working on a directive that needs to reload its content automatically. Unfortunately, I haven't had much success with it so far. Any assistance would be greatly appreciated. UPDATE: I attempted the suggested solution but encountere ...

Encountered an error while trying to retrieve data from

Having trouble with file uploads to the S3 bucket using @aws-sdk/client-s3 library and encountering errors when uploading files larger than 70kbps: `TypeError: Failed to fetch at FetchHttpHandler.handle (fetch-http-handler.js:56:13) at PutObjectCommand ...

What is the best way to link a class to a specific row cell in an Element UI table depending on a condition

<el-table :data="confirmedAppointments" highlight-current-row style="width: 100%"> <el-table-column type="index" width="50"> </el-table-column> <el-table-column prop='token&apo ...

Issues with link behavior when using Angular UI-Router

I am just starting to learn AngularJS and I'm trying to figure out how to pass a parameter using anchor tags within the $stateProvider code. I've been attempting to do so, but it doesn't seem to be working. Can someone guide me on the correc ...

Access a PDF document through an AJAX POST request

I have an ajax POST method that looks like this: $.ajax({ type: 'POST', url: rootUrl("Home/PrintInvoice/12"), success: function (result) { $("#TestInvoicePrint").empty(); $("#TestInvoicePrint").html(result); w ...

JQuery for Seamless Zoom Functionality

I am working with some divs that have images as backgrounds, and I have added a zooming effect on hover. However, the zoom effect is not smooth. Is there a way to modify the script to make the zoom effect smoother? Here is an example of my HTML structure: ...

What is the method for customizing the background color in a .vue file using Bootstrap?

Can anyone assist me in updating the background color of a div class from grey to white? The code is within a .vue file using Bootstrap and includes an enclosed SVG file. Here's the snippet: <div class="text-left my-3 bg-white"> <button var ...

Is there a way to simulate a virtual keyboard event (specifically the tab key) using Javascript in Internet Explorer 8

I'm looking to create a virtual keyboard event for tab using JavaScript. I've researched this topic and found some helpful answers, but I haven't been able to get it working properly. I know that JavaScript is an event-driven language and ty ...

Utilizing Visuals from a Vue Component Collection

As I attempt to publish a Vue library to npmjs, I encounter an issue with the images within it. After publishing the app to NPM and importing it into my main app, everything appears to be working correctly. However, the paths to the images in the library ...

d3.on event firing prior to mouseover event

This page showcases a dynamic d3 bar chart. On line 162, we have an interesting event listener that logs a message when the user hovers over a bar on the chart. However, the log is displayed as soon as the page loads. You can see the running code in actio ...

Is there a way in JS to wipe out all methods, classes, setTimouts, and setIntervals in one swift action?

I am working on creating an animation with a class that involves calling other classes, methods of other classes, setTimeouts, and more. My current task is to implement an exit button for this animation. When this button is clicked, I need all the classes, ...

The mounted property is causing an error because the method being called does not exist in the type

Whenever I attempt to call the function load from mounted(), I encounter an error stating "Property 'load' does not exist on type '{ name: string; components: { IonHeader:" which appears in the console message. <script lang="ts" ...

Similar to the .Contains method in ASP

Similar Post: JavaScript: checking if a string contains another string Jquery: How to determine if a string contains a specific substring Currently, in my ASP .NET C# code I utilize the following method: string aa = "aa bb"; if (aa.Contains("aa") ...