Obtain the index of a Checkbox within a ListView using nativescript-vue

Within a ListView component, I have a checkbox that is part of a for loop

I am trying to retrieve the index of the checked item in order to make changes to it in the array. However, I am facing difficulty as there is no equivalent of the :key prop as found in Vue.

When it comes to rendering the list, I am unsure whether to use a Label alongside the checkboxes or simply utilize the text attribute for displaying text. What is the standard practice in this scenario?

Although I can obtain the index of an item using @itemTap on the ListView, the functionality ceases to work when adding @checkedChange to the CheckBox tag.

Furthermore, I have noticed a minor issue where I cannot directly tap the checkbox to change its state (in my case, click since I am utilizing an emulator). Instead, I have to tap on the associated text like :text="task.title". Removing the text removes the ability to toggle the checkbox's state altogether.

<ListView for="(task, index) in tasks" @itemTap="onLabelTap">
    <v-template>
        <GridLayout columns="auto,*">
            <!-- Displays the item label with the default style and color -->
            <CheckBox :text="task.title" :checked="task.isChecked" @checkedChange="onCheckChange" col="0" />
            <!--<Label :text="task.text" class="item" col="1" />-->
        </GridLayout>
    </v-template>
</ListView>

The JavaScript code snippet:

data() {
    return {
        tasks: [
            {'id': 0, 'title': 'One', isChecked: false},
            {'id': 1, 'title': 'Two', isChecked: true},
            {'id': 2, 'title': 'Three', isChecked: false}
        ],
    }
},
computed: {
    message() {
        return "<!-- Tasks Page -->";
    }
},
methods: {
    onDrawerButtonTap() {
        utils.showDrawer();
    },
    onLabelTap(event) {
        alert('You clicked on ' + event.index) // Index accessible here
            .then(() => {
                console.log("Alert dialog closed.");
            });
    },
    onCheckChange(event) {
        this.isChecked = event.value;
        console.log(this.isChecked);
        console.log(event.index); // Unable to access index here
    }

Answer №1

To get the index of the item tapped in a ListView event, refer to the event object.

For events on individual UI components, parameters can be passed manually as shown below:

<ListView for="(task, index) in tasks" @itemTap="onLabelTap">
    <v-template>
        <GridLayout columns="auto,*">
            <CheckBox :text="task.title" :checked="task.isChecked" @checkedChange="onCheckChange(task, index, $event)" col="0" />
            <!--<Label :text="task.text" class="item" col="1" />-->
        </GridLayout>
    </v-template>
</ListView>

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

Waiting for the response of a Javascript function

I have the code snippet below: myFunc(); bar(); The function myFunc() initiates an ajax request. I want to ensure that bar() is only executed after myFunc()'s ajax request has been completed. However, I do not wish to move the call to bar() inside ...

Steps for converting a set of data points into a format suitable for incorporation into an HTTP GET request

I'm working with a set of values: var array = [10,11,12,13,14,15] and I need to pass them as parameters in an http.get request in this format: id=10&id=11&id=12&id=13&id=14&id=15 I initially tried the following approach: var url ...

Using cytoscape.js to implement a distinct color mapping for node background colors

I am currently learning Javascript and Cytoscape.JS, and I am attempting to implement a discrete color mapping for my nodes based on a specific property in my data. This property consists of five unique values (a, b, c, d, e), and I want to assign colors t ...

jquery ajax function that returns an object when successful

Below is a brief example of an AJAX call wrapped in a function. MyNS.GetStrings = function (successCallback, errorCallback) { var url = serverUrl + "/GetStrings"; $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", dataType: ...

The header row in my table multiplies each time an AJAX call is made

HeaderRowMultiplesEachTimeAjax My web grid table in JavaScript consists of a complete HTML table. The top header, which acts like a colgroup caption table, is built through JavaScript insertion as shown below: var headerRow = "<tr><th colspan=&a ...

The attempt to compare two undisclosed inputs using basic javascript was unsuccessful

In my current project, I am facing an issue with comparing two hidden input HTML elements using JavaScript onclick submit button. Despite the seeming simplicity of the task, it is not working as expected. The function that I have implemented for this purp ...

Tips for ignoring preflight response in AngularJS

When I send a http.post request to my service, I start by sending an Option request as required by Cors. However, I've noticed that the OPTIONS (pre-flight) request does not return any response.data, whereas the POST request does. This creates an iss ...

PHP-generated AngularJs Select Element

I'm facing an issue with my PHP function in my AngularJS application. I have created a function to select a default option, but it's not displaying the desired value. Here is the function code: function qtyList($selectName, $selectQty){ $st ...

When using <body onload=foo()>, the function foo will not be executed

Having trouble with this code - it seems like initialize() is not being called. Check out the code here Any insight into why this might be happening? ...

Is there a way to transfer the functionality of openssl_seal from PHP to NodeJS 18?

I'm looking to convert the openssl_seal() function from PHP to NodeJs. The code below is from my PHP SDK and works flawlessly, which is what I want to migrate: $ivLength = openssl_cipher_iv_length('AES-256-CBC') ?: 16; $iv = openssl_random ...

If you click outside of a Div element, the Div element will close

I am looking for a way to implement a function that will hide a specific div when I click outside of its area. The div is initially set to Position: none and can be made visible using the following function: Div Element: <div id="TopBarBoxInfo1" oncli ...

What is the best way to access data from a static config.json file in TypeScript within a Vue component following the execution of a build:electron command in Vue CLI 3?

Hey there! I've been considering whether it's feasible to include a config.json file in a Vue CLI 3 project that can be dynamically read at runtime, both during development and production stages. This config.json file will hold key strings that ...

Updating the background image without having to validate the cache

I have implemented a basic image slideshow on my website using a simple Javascript function. The function runs every 5 seconds to update the CSS "background-image" property of a div element. While it is functional, I've noticed that each time the func ...

When toggling the menu in bootstrap, the functionality of <a href=""> links may be disrupted

My attempt to integrate a toggle function into each link in the menu to automatically close the menu on mobile after selecting a link has hit a snag. With the solution of adding data-toggle="collapse" data-target="#navbarSupportedContent" to the <a href ...

Waiting for the initial render before using document.getElementById() in next.js

I followed a tutorial that demonstrates how to access a canvas element by id using plain JavaScript. The tutorial can be found here. In the video, the method is explained around 5:10 and I adapted it for next.js in the code snippet below. import React, { u ...

Set your sights on a particular tab within the primefaces tabview

I've implemented a tabview feature using primefaces. <p:tabView> <p:tab id="basic" title="Login"> </p:tab> <p:tab id="personal" title="Personal"> </p:tab> <p:tab id="contact" title="Contact"> </p:tab> </p: ...

The nested elements are not triggering the jQuery on click function

When I attempt to retrieve the attributes of the parent element from a clicked element, I run into issues with dynamically added elements via jQuery. Below is the code snippet for the click event that is not functioning as expected: $("body").on('cli ...

Troubleshooting issues with the Select List component in ReactJS

Having an issue with the select list as the onChange event is not triggering. Struggling to set the selected value from the list in the this.state variable. Any assistance on this matter would be greatly appreciated. class SelectActivity extends React.C ...

Creating a Three.js shader that generates a textual output

Currently, I am in the process of building a web application using Three.js that displays a scene in ASCII art, much like the example showcased here. However, I am encountering some challenges with the frame rate. In an attempt to enhance performance, I h ...

How can I hide a div using Ajax and show success in the following div?

I'm utilizing ajax to implement search functionality onkeyup. The goal is to display all results if the textbox is empty, but when the textbox contains text, only show results that match. <input type="text" onkeyup="searchme()" id="search_id" /&g ...