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
}