Deleting an item in Vue.js with the removal feature

After deleting items from my list, they remain visible until I manually refresh the page. How can I fix this issue?

List

<tbody>
    <tr v-for="school in schools" v-bind:key="school.id">
        <td>{{ school.id }}</td>
        <td>{{ school.name }}</td>
        <td>
            <button
            class="btn btn-danger"
            v-on:click="deleteSchool(school.id)">
            Delete
            </button>
        </td>
    </tr>
</tbody>

Delete method

deleteSchool(id)
{
  let uri = `http://localhost/schools/${id}`;
  this.axios.delete(uri);
  this.schools.splice(id, 1);
}

Looking for solutions!

Answer №1

utilize the index as a key to remove an item from the list

<tbody>
    <tr v-for="(school,index) in schools" v-bind:key="school.id">
        <td>{{ school.id }}</td>
        <td>{{ school.name }}</td>
        <td>
            <button
            class="btn btn-danger"
            v-on:click="deleteSchool(school.id,index)">
            Delete
            </button>
        </td>
    </tr>
</tbody>
deleteSchool(id,index)
{
  let uri = `http://localhost/schools/${id}`;
  this.axios.delete(uri);
  this.schools.splice(index, 1);
}

Answer №2

In the realm of ES6, this task can also be accomplished using findIndex().

<tbody>
  <tr v-for="school in schools" :key="school.id">
    <td>{{ school.id }}</td>
    <td>{{ school.name }}</td>
    <td>
      <button
        class="btn btn-danger"
        @click="deleteSchool(school.id)">
        Delete
      </button>
    </td>
  </tr>
</tbody>

methods: {
  deleteSchool(id) {
    const uri = `http://localhost/schools/${id}`;

    this.axios.delete(uri);

    const schoolIndex = this.schools.findIndex(school => school.id === id);

    this.schools.splice(schoolIndex, 1);
  }
}

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

Tips on getting the most out of your watch

Within my parent component, I am passing the month_id props to a child component by clicking on a specific <li>. The code for my child component looks like this: <script> export default { props: ['mosque_id', 'month_i ...

Bazel: Utilizing the nodeJS_binary rule for executing "npm run start" - A Guide

Is there a way to utilize the nodejs_binary rule in order to execute a standard npm run start? While I can successfully run a typical node project using this rule, I am struggling to run the start script specified in package.json. Below is what I currently ...

What causes the Woocommerce checkout button to be blocked on the checkout page?

I am perplexed by WooCommerce's decision to block this section of the screen with an overlay. Under what circumstances would it do so? checkout page screenshot ...

How to show a div for small screens only using Bootstrap 4 beta?

Previously, in Bootstrap alpha 6 I was able to achieve this by writing the following code for displaying a div's contents only for sm: <div class="hidden-md-up hidden-xs-down"> This content would only be visible for sm in Bootstrap 4 alpha 6 ...

What could be causing the Express server to return an error despite everything being in order?

I am new to exploring express.js and attempting to create a basic sign-in example server. I have set up a database object with a users array containing objects, each with email and password properties. Despite using body-parser to convert the body into a j ...

A guide to retrieve data attributes in Vue.js

When my button is clicked, a modal pops up with data passed through data attributes. The button code looks like this: <button class="edit-modal btn btn-info" data-toggle="modal" data-target="#editModal" :data-id=item.id ...

What causes the disparity in the functionality of getServerSideProps between index.js and [id].js in NextJS?

Exploring NextJS and React as part of my e-commerce site development journey has been exciting. However, I encountered a roadblock when trying to connect to an external database that requires real-time updates, making getStaticProps unsuitable for my needs ...

Get a single object from an array with .single method provided by @ngrx

Seeking to retrieve an Observable containing a single object from an array of objects in my store. I aim to utilize the .single operator, which should throw an exception if there is not exactly 1 object present. However, I'm struggling with this as my ...

Highlighting JavaScript code with React Syntax Highlighter exclusively

I've been struggling to highlight Ruby code, but it seems like no matter what I do, it keeps highlighting JavaScript instead. It's frustrating because I can't seem to get it right. import React from "react"; import atom from "node_module ...

Automatic playback of the next video in a video slider

Looking to upgrade my video slider functionality - switching between videos, playing automatically when one finishes. Struggling to find a solution that combines manual control with automatic playback. My attempt: function videoUrl(hmmmmmm){ ...

Tips to prevent browser from freezing while creating a large number of HTML elements

I am currently utilizing Selection.js to develop a customizable grid on my website. To make this work effectively, I need a specific number of div elements to establish the selectable area. In my scenario, I generate all the divs using a for loop and then ...

Endless scrolling with Laravel and Vue fails to load data due to request not being sent

Welcome everyone, I am new to using vue.js. I have encountered a simple issue with infinite scroll. Even though I have configured it as shown below, when the page is reloaded, the request to fetch data from the database for page 1 is not sent. I would appr ...

Tips on retrieving the status code from a jQuery AJAX request

I am trying to figure out how to retrieve the ajax status code in jQuery. Here is the ajax block I am currently working with: $.ajax{ type: "GET", url: "keyword_mapping.html", data:"ajax=yes&sf="+status_flag, success: callback.success ...

Execute a JavaScript function daily for each new user

Can someone help me understand how to execute a JavaScript/jQuery function that triggers a PopUp once for each new user visiting a webpage? Below is the code I need assistance with. $(window).load(function() { $('#index9').fadeIn("slow"); ...

React is generating an error when attempting to use hooks without the appropriate class name prefix

I'm encountering an error when I use hooks without a class name prefix. Can someone help me troubleshoot this issue? import React, {Fragment,useState} from 'react'; function App (props) { const [x,updateX] = useState(1) /* throwing error ...

Avoid the occurrence of the parent's event on the child node

Attempting to make changes to an existing table created in react, the table is comprised of rows and cells structured as follows: <Table> <Row onClick={rowClickHandler}> <Cell onCLick={cellClickHandler} /> <Cell /> ...

Difficulty encountered while using CSS to customize a vue template

I'm currently working on a login page and experiencing difficulty styling Vue templates using CSS. Here is the code that works without Vue: HTML <body class="text-center"> <form class="form-signin"> <h1 class="h3 mb-3 fon ...

Feeling trapped during the process of setting up a intricate jQuery Image Slider

I've hit a roadblock while trying to make changes to the slider located at THIS URL. In the current setup, each thumbnail corresponds to one main display image. Clicking on a thumbnail displays a single image and then proceeds to slide to the next th ...

What is the process for creating a versatile component in Vue that can be specialized for specific models?

A dialog window Vue component has been created without a model, with slots for the title, center content, and buttons. Now the goal is to fill this dialog with a form that will have specific details unique to the form itself, such as the model data and be ...

Is there a way to replicate the functionality of $(document).ready for dynamically loaded AJAX content?

$(document).ready(handler) is triggered once the DOM has finished loading. However, if new content containing a $(document).ready(handler) function is added to the page via AJAX, this function will be executed immediately according to the jQuery API. It&ap ...