Utilize VueJS to remove a designated HTML div upon clicking the delete button

I'm having trouble with a delete button on my input fields. The goal is for the delete button to remove all related input fields when clicked, but it's not working as expected.

HTML

<div v-for="index in 10" class="form-group row" for="switch-id2" v-if="isShow2">
    <br><br>

    <div class="col-md-2">
        <b-form-fieldset>
            <label>Pincode</label>
            <div id="app">
                <treeselect placeholder="Enter the pincode(s)" :options="options" :value="value"
                    :multiple="multiple">
                    <div slot="value-label" slot-scope="{ node }">{{ customLabel }}</div>
                </treeselect>
                <p>
                    <label><input type="checkbox" v-model="multiple">Multi-select</label>
                </p>
            </div>
        </b-form-fieldset>
    </div>
    <div class="col-md-2">
        <label>Supply Chain</label>
        <b-input-group>
            <select class="form-control" id="supplyChain" name="supplyChain" v-model="supplyChain">
                <option selected value="">Select</option>
                <option value="b2bRegular">Dummy</option>
            </select>
        </b-input-group>
    </div>
    <div class="col-md-2">
        <label>ODA category</label>
        <b-input-group>
            <select class="form-control" id="odaCategory" name="odaCategory" v-model="odaCategory">
                <option selected value="">Select</option>
                <option value="nonODA">Default</option>
            </select>
        </b-input-group>
    </div>
    <div class="col-md-2">
        <label>ODA TAT</label>
        <b-input-group>
            <select class="form-control" id="odaTat" name="odaTat" v-model="odaTat">
                <option selected value="">Select</option>
                <option value="1>1</option>
                <option value="2">2</option>
        </b-input-group>
    </div>
    <div class="col-md-2">
        <label>FM Facility</label>
        <b-input-group>
            <select class="form-control" id="fmFacility" name="fmFacility" v-model="fmFacility">
                <option value=""></option>
            </select>
        </b-input-group>
    </div>
    <div class="col-md-2">
        <label>LM Facility</label>
        <b-input-group>
            <select class="form-control" id="lmFacility" name="lmFacility" v-model="lmFacility">
                <option value=""></option>
            </select>
        </b-input-group>
    </div>
    <div class="col-md-2" style=-"margin-top:-2em; margin-left:0em">
        <label>RTO Facility</label>
        <b-input-group>
            <select class="form-control" id="rtoFacility" name="rtoFacility" v-model="rtoFacility">
                <option value=""></option>
            </select>
        </b-input-group>
    </div>
    <div class="col-md-2" style="margin-top:-2em; margin-left:0em">
        <label>RVP Facility</label>
        <b-input-group>
            <select class="form-control" id="rvpFacility" name="rvpFacility" v-model="rvpFacility">
                <option value=""></option>
            </select>
        </b-input-group>
    </div>
    <b-button type="button" class="btn btn-danger" title="Delete pincode" style="font-size: 20px;"
        onClick=""><i class="fa fa-trash"></i>
    </b-button>
</div>

JS code

import CryptoJS from 'crypto-js';
import VueElementLoading from 'vue-element-loading';
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'dummy',
  components: { VueElementLoading, Treeselect },
  data() {
    return {
      isShow2: true,
      multiple: true,
      value: null,
      options: [203207, 234567, 324353, 201301, 201303, 122413].map(i => ({
        id: i,
        label: `${i}`,
        customLabel: `Custom Label ${i}`,
      })),
    }
  },
  props: {
    msg: String,
  },
  mounted() {

  },
  methods: {
    thisFileUpload() {
      document.getElementById("file").click();
    }
}
}

When clicking the red delete button in the image, the Pincode, Supply chain, ODA TAT, FM facility, LM facility, RTo facility, RVP facility input fields should be removed.

Answer №1

After the discussion in the comments above, it became clear that the goal is to manage the visibility of individual rows independently.

The solution remains the same: have the "delete" button update a state variable, and use that variable to control the display of the row you want to delete.

The only difference now is that if you're managing the visibility of multiple items, you'll need separate state variables for each one.

Typically, your state data would be organized in an array structure, especially if you plan on having different data in each row; instead of taking shortcuts like

<div v-for="index in 10">

You would set it up like this:

data() {
    return {
      rowData: [
         {isShown: true, /* other row data here */},
         {isShown: true, /* other row data here */},
         {isShown: true, /* other row data here */},
         /* ...and so forth */
      ],

Then, your rendering loop would iterate over the rowData array (keeping in mind not to use v-for and v-if on the same element):

<template v-for="(row, index) in rowData">
  <div v-if="row.isShown">
    /* content here */
    <button v-click="hideRow(index)">Delete</button>
  </div>
</template>

Each delete button within a row can pass its index to the click handler, which then knows which element to update the isShown property for (by replacing rowData with a new array where rowData[index].isShown becomes false.)

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

Replace the bullet icon with a double quote symbol

My HTML List needs a different look - I want to change the bullet icon into a double quote icon. A website that I found interesting is this one. Here's the CSS code I have: .listStyle li:before { list-style: none; content: "\00BB"; } < ...

Incorporating a polygon into vue2-leaflet framework

I have been struggling to incorporate a MultiPolygon onto a leaflet map using vue2-leaflet without any success. The polygon coordinates are being generated from PostGIS. Is there a way to add a polygon to a vue2leaflet map? Sample code: fiddle: https:// ...

Ways to programmatically increase the names of variables in JavaScript?

I am currently working on developing a user-friendly recipe app with Node/Express. Users are able to access a 'new recipe' HTML form where they can easily add as many ingredients as they desire by clicking the 'add ingredient' button. U ...

The properties of the box model applied to unidentified inline boxes

Reference: CSS Specification Text directly contained inside a block container element (not within an inline element) is considered as an anonymous inline element. For example, in an HTML document like this: <p>Some <em>emphasized</em> ...

Implementing jQuery UI in ASP.NET Web Forms

When you click on New > Web Forms in an ASP.NET Web Forms project, a page is generated with the following code: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DuckbilledPlatypus.aspx.cs" Inherits="DuckbilledPlatypus" %> <!DOCTYPE ht ...

Unusual problem arises when working with exclusions and inclusions in tsconfig.json

I've encountered a peculiar issue. When I specify the following in my configuration: "exclude": [ "node_modules", "**/__tests__", "**/*.test.*", "**/server-handlers/**/*", "**/ ...

How come my web page is not displaying the guages from guage.js?

I am utilizing guage.js to create a graph based on this Fiddle. However, upon adding the same code to my website, the rendering does not appear as expected: https://i.sstatic.net/fIkQr.png Upon inspecting in Chrome developer tools, I noticed that the ele ...

center items after aligning floats

Currently, I am in the process of creating a website using a mobile-first approach with media queries. As I progressed to a larger device, specifically a tablet, I had to utilize the float property on my .textcontainer element to ensure it aligned properly ...

Spin a three-dimensional cube on a platform using three.js with a unique functionality

I am attempting to create an animation featuring a cube on a flat surface. The cube can be rotated along the x/y axis only, with no need to show its underside. I want to be able to tip the cube over any edge - when a side of the cube touches the surface, i ...

Generating pages dynamically based on the number of divs

So, I've been using template literals to create and append new divs, but now I want to take it a step further. Is there a way to automatically generate a 'next page' when a certain number of divs have been appended in HTML & JS? Appreciate ...

Trouble hindering mouseup event on Android despite using preventDefault()

Seeking a solution to prevent a mouseup or touchend event from firing twice. See the complete example below: <!DOCTYPE html> <html> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script& ...

Vue developer proxy rule getting overlooked

How can I successfully proxy a link from an unauthenticated landing page to localhost:8080 in my development environment? It seems that my current proxy rule is not being recognized. static site: localhost:80 webapp: localhost:8080 On the static site, I h ...

Issue with Heroku: Unable to serve images through NodeJS Express server

I have an app deployed on Heroku that displays an image. app.js package.json package-lock.json public |__ image1.png |__ image2.png This is the code within app.js const express = require('express'); const app = express(); const path = re ...

Instructions on making one script activate another [jQuery] for resizing and enlarging a search box

Hi there, I'm facing a small dilemma and would appreciate some guidance. I've been working on creating a menu bar that mimics the functionality of apple.com (where the search bar expands when clicked to show options). Currently, I have a script ...

Leverage the power of Next.js Dynamic routing query object with an SWR fetch request

While working with Next.js dynamic routing, I am facing an issue where my SWR fetch request is being called before the query object is properly set. This occurs specifically when using the routing query object. Let's take the example of a dynamic rou ...

Error message: "Cookie not found upon initial visit"

Upon the initial visit to my website, there are no cookies set which leads to a NaN error in my calculation. To address this issue, how can I modify it to return 0 if no cookie is present? This is the current code snippet used for retrieving the cookies: ...

Incorporating hCaptcha into Laravel Jetstream with Inertia.js

Currently utilizing Laravel 8 together with Jetstream 2.0 and the Inertia stack. Successfully added the Vue hCaptcha component from here to my login form. The Vue component is functioning perfectly. Followed the instructions provided in this guide to se ...

Performance problems with Kinetic JS in Chrome and Opera

My Kinetic JS app is experiencing significant performance issues when using Chrome or Opera browsers, but runs smoothly on IE or Firefox. You can check out the app here and view the JavaScript code here. I am currently using free hosting - could this be t ...

Using Nativescript Vue to dynamically change the image src.decode in order to show a font icon

I have developed a custom component that is intended to display a font icon : <template> <Image :src.decode="code" /> </template> <script> const mapping = {wi_owm_200:"&#xf01e;",wi_owm_201:"& ...

Is it possible to deploy Vue.js from a content delivery network (CDN) for production

My decision to use Vue.js for a new project was influenced by its ability to run natively in the browser, unlike React which requires compilation/transpilation via Node. I'm considering linking directly to a CDN like this in my production code: <s ...