Deleting specific arrays by selecting checkboxes and removing them in Vue.js

<script>
import { datalist } from "./datalist";
export default {
  name: "HelloWorld",
  components: {},
  data() {
    return {
      items: datalist,
    };
  },
  methods: {
    deleteEvent(id) {
      this.items = this.items.filter((e) => e.id !== id);
    },
  },
};
</script>
My data...
export const datalist = [
  { id: 1, val: "11", kk: "potter" },
  { id: 2, val: "22", kk: "james" },
  { id: 3, val: "55", kk: "limda" },
  { id: 4, val: "77", kk: "stepen" }
];
 
  <div>
    <div v-for="item in items" :key="item.id">
      <b>{{ item.id }}.</b> &nbsp;&nbsp;&nbsp;
      <router-link :to="{ name: 'UserWithID', params: { id: item.id } }">
        {{ item.kk }}
      </router-link>
      <input type="checkbox" :value="item.id" />
      <button @click="deleteEvent(item.id)">Delete</button>
    </div>
  </div>

My complete code;- https://codesandbox.io/s/wild-flower-rssg0?file=/src/components/datalist.js

I aim to remove entries from the array when the user checks the checkbox next to each entry and then clicks the delete button.

Currently, I am able to delete an array record by clicking directly on the delete button without first checking the checkbox. However, the deletion should only occur after both actions - checkbox selection followed by the delete button click.

In my code, I have set up a method to handle the deletion process upon clicking the button, but it is not functioning as intended. Can anyone provide assistance in identifying what may be causing the issue with the code?

Answer №1

One way to efficiently manage deletion of items is by creating an array specifically for holding items to delete. Connect the checkboxes to this array so that when they are checked, the corresponding item ID gets added. Then, include a delete button in your interface to remove all selected items at once.

For a practical demonstration, check out this example: https://codesandbox.io/s/elated-lake-ws2wn

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

Guide on setting up a MEAN stack application to run on port 8080

I am brand new to the mean stack development environment. I'm attempting to configure my root domain name to display the app directory once I enter the command grunt, but the only way it currently works is at website.com:8080/!#/. How can I get it to ...

Switching Symbols with JQuery

Hello, I am brand new to utilizing javascript and I'm currently experimenting with the toggle function. My goal is to create show/hide functionality while also toggling arrow icons. Specifically, I want a right arrow next to the link "More -->" and ...

Using recursive setTimeout in Javascript may not always utilize the entire final JSON object that is returned

My current approach involves making recursive calls to a URL until it returns either a success or reaches the maximum limit of tries. Below is a compact version of the relevant code: function doSomething(numRetries) { $.post('someURL', {retr ...

What is the best way to target changing elements displayed by *ngIf?

Trying to access a dynamic element generated by ngIf in the code below has proven to be challenging. I attempted two different methods: Using ElementRef and querySelector Component template: `<div class="test" *ngIf="expr"> <a id="b ...

Making Changes to Data Objects in NativeScript-Vue?

Is there a way to add a new property to a data object? The data is sourced externally and currently lacks a flag indicating whether or not to display the content of the object. <RadListView for="item in list" @itemTap="toggleAccordion"> <v-t ...

Creating a dynamic CSS height for a div in Angular CLI V12 with variables

Exploring Angular development is a new venture for me, and I could use some guidance on how to achieve a variable CSS height in Angular CLI V12. Let me simplify my query by presenting it as follows: I have three boxes displayed below. Visual representatio ...

Is there a way to append the current path to a link that leads to another URL?

I am currently in the process of separating a website, with the English version being on the subdomain en. and the French version residing on the www. Before making this change, I have a drop-down menu that allows users to select their preferred language ...

numerous yields within a calculated attribute

I am facing an issue with setting up multiple returns in a computed property. I am unsure why it is not functioning as expected. I also attempted to repurpose the arrow function, but without success. My objective here is to trigger the 'nearby' m ...

Implementing data binding with JSON objects in Vue.js

I recently used the Vuetify component to create a todo app using Vue.js with Firebase as the database. I am currently struggling with binding the title from a JSON object. Can anyone help me with how to bind a specific value or attribute from an object? ...

Generate an array of checked inputs to be used when posting to a REST API

I have been using .push() to create a checked array of "List" inputs for posting to a REST API. However, it doesn't seem to be working correctly. When unchecking an item, it is not automatically removed from the array. Does anyone have a better solut ...

Utilize JavaScript to append elements to an array in each loop iteration, gradually building a single, unified

I find myself in a situation where an async request is sending data to a child react component. Upon logging the "data" in the component, I receive multiple arrays sequentially. Ideally, I would prefer not to rewrite the request in the parent component and ...

Android browser experiences a sudden surge of unexpected data influx

I am facing an issue with my application where it maps an array from the state. The array should ideally only contain 6 sets of data, as limited by the backend. However, sometimes it spikes and displays data that is not supposed to be there or shows old da ...

What could be the reason behind the validation failure of this Javascript code?

After implementing your recommendation, this is my current status: <script> function tick() { const React.element = ( '<div><marquee behavior="scroll" bgcolor="lightyellow" loop="-1" width="100%"> <i> <font color ...

The cookie set in express.js is not being successfully set in React.js, even after setting up CORS and using credentials: 'include'

I have been struggling to set a cookie from my express backend using a React frontend. The cookie shows up in the response header, but for some reason, the browser refuses to set it. I've tested this on Chromium and Firefox, with no success. Interesti ...

Node.js application encountering crashes while attempting to download a sizable file

I'm currently working on a project that involves converting HTTP URLs to torrents. The program is designed for downloading smaller files, typically between 1-500Mb. However, I've encountered an issue where the application crashes or times out whe ...

The function Sequelize.create() does not exist

My attempts to push my DB with sequelize are not working, even though I have set up this schema for the DB: module.exports = (sequelize, DataTypes) => { const Problems = sequelize.define("Posts", { theme: { type: DataTypes.ST ...

Angular implementing a carousel feature using the ngFor directive

Currently, I am working on implementing a carousel feature. The images in the carousel are sourced from a string array and I want to be able to cycle through them when clicking on the left or right arrows. How can I achieve this functionality using only ...

Protecting client-side game logic operations with web application security

I've been developing a web-based game that utilizes the Canvas feature of HTML5. However, I've come to realize that there is a significant vulnerability in my system. The scoring and gameplay statistics are currently being calculated on the clien ...

Troubleshooting the issue of VSCode's CTRL+Click feature not navigating to definitions within HTML tags in Vue files

Currently, I am utilizing VSCode along with all the necessary Vue extensions (including Vetur, which is supposed to handle going to definitions), but I have encountered an issue. When working in a .vue file type, I am unable to use CTRL+Click to navigate t ...

When using Angularjs and UIRouter, calling event.preventDefault() will prevent the default browser behavior and

A custom JavaScript prompt is triggered when a user clicks the back button on their browser by monitoring the $stateChangeStart event. Let's explore this scenario: Imagine users moving through pages 1, 2, and finally reaching page 3. Upon trying to g ...