Tips for refreshing a page in Vue.js

I am facing an issue with updating a table on my page after deleting a row. Each row in the table has a delete button and I have tried using window.location.reload() but it didn't work.


    </va-card>
    <br/>
      <va-card >
        <va-table :hover="hover" :size="size" v-if="approveduserData.length"  >
          <table style="width:100%;" >
            <thead >
              <tr style="font-weight:bold;font-size:19px">
                <th>Username</th>
                <th>Email</th>
                <th>Mobile</th>
                <th>Delete User</th>
               </tr>
            </thead>
            </tbody>
            <tr v-for="user in approveduserData">
              <td style="font-weight:bold">{{user.username}}</td>
              <td>{{user.email}}</td>
              <td>{{user.phone}}</td>
              <td><va-button color="danger" v-on:click="Deactivate(user.email,user.username)"> Deactivate</va-button></td>
             </tr>
            </tbody>
          </table>
        </va-table>
      </va-card>

// Here is the deactivate function


    Deactivate:function(email,name){
       const rejectUser = {
         "email":email
       }
       if(confirm("Do you want to Remove "+ name +" Account??"))
       {
         axios.post('http://34.67.88.0:3000/api/userrejected',rejectUser)
           .then(function(res){
             self.outputRejected = res.data;
             if(self.outputRejected){
               alert(  name+" Account Successfully Deleted!!")
             }
           })
           .catch(function(error){
             console.log("Error",error)
           })
       }
     }

Answer №1

After successfully deleting a record on the server, you can update the local copy in the callback function:

          .then(function(res){
            self.outputRejected = res.data;
            if(self.outputRejected){
              this.approveduserData = this.approveduserData.filter(x => x.name !== name) <--
              alert(  name+" Account Successfully Deleted!!")

            }

There are other ways to remove the entry from the local array, but the underlying logic remains the same.

Answer №2

When making updates to an object in Vue, the framework may not automatically detect the changes on a data property. To ensure that the updates are recognized, you can add a key to the elements that require reloading. Then, when updating the data property, also update the key on those elements to force the reload. Learn more about forcing re-renders here.

//template
<va-card :key="reloadKey">
...
data(){
  return {
    reloadKey:0,
    approveduserData: []
  }
}

.then(...update local data property, increment reloadKey)

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

Transmitting a client-side JavaScript function to the server for processing the database response and generating a downloadable CSV file

I need to create CSV reports for a series of queries in my project. In the backend, I have a general POST request handler in nodejs/express that accepts a JSON object structured like this: { "converter": <converter function>, "fields": < ...

Choose a specific element using jQuery based on a class that is shared by several elements

I am looking to target an element with the class 'submenu-expand' and apply an additional class to it. $('.menu-item').on('click',function(){ $('.submenu-expand').toggleClass('expanded') }) While this cod ...

Is there a way to implement a scrollbar that only scrolls through one specific column in an HTML table?

I need help adding a scrollbar to a specific column in an HTML table. Take a look at this scenario https://jsfiddle.net/6wpdc4tL/: https://i.stack.imgur.com/svzIg.png This table should have two scrollbars, one for the light blue SCROLL column and another ...

Finding the initial occurrence of duplicates within an array of objects using JavaScript

I am working with a data structure retrieved from an API, and it looks like this: [ { id: '10000844', text_id: '10000844-01', }, { id: '10000844', text_id: '10000844-02', }, { id: ' ...

Passport - Pairing Plan "Error|The username provided for sign_request() is not recognized"

Currently experimenting with duo in node.js using passport to test its implementation in an application....Utilizing a passport-duo strategy and encountering issues when trying to apply the example provided in my own project. The GitHub repository for pass ...

Uh oh! AngularJS just threw an error: Uncaught TypeError - angular.service is not a function. It happened in taskService.js

I've run into some issues with AngularJS, even though I have imported all the necessary libraries. Specifically, I am encountering the following errors: Uncaught TypeError: angular.service is not a function at taskService.js:2 taskFactory. ...

How can I run a TypeScript function within a JavaScript file?

I am working with a typescript file named file1.ts export function Hello(str: string) { console.log(str); } In addition, I have another file called index.js { require('./some.js'); } Furthermore, there is a script defined in the pack ...

Photos failing to load in the image slider

Although it may seem intimidating, a large portion of the code is repetitive. Experiment by clicking on the red buttons. <body> <ul id="carousel" class="carousel"> <button id="moveSlideLeft" class="moveSlide moveSlideLeft"></button& ...

What is the best way to stop the browser from automatically redirecting to another page after submitting a form?

I am using an AJAX call to a method that returns JSON data. How can I retrieve and read the JSON response without being redirected to a new empty page? [HttpPost] public JsonResult Test() { return Json("JSON return test", JsonRequestBehavior.AllowGe ...

Conflicting Transformation Properties Causing CSS Issues Within a Single Element

I'm currently working on a user interface where users can drag and drop a box with a red outline to position it on the screen within a black box. See the UI here Alternatively, users can also move the box by adjusting the inputs on the right side. ...

The search filter in react material-table is not functioning properly upon initialization

Search functionality is functioning correctly in this code snippet: <MaterialTable columns={[ { title: 'Name', field: 'firstname', type: 'string' } ]} /> Unfortunately, the Search filte ...

Express JS is experiencing difficulties with the functionality of the iframe

When I'm using iframe in HTML, it works perfectly fine: <iframe src="main.html" height="100px"></iframe> But when I use Iframe in Express, it shows an error: <iframe src="main.html" height="100px&qu ...

Using a React component to trigger an action when the Enter key

I have a react component that needs to respond to the "Enter" key press event. class MyComponent extends Component { componentDidMount() { console.log('componentDidMount'); document.removeEventListener('keypress', t ...

An object rotating in a loop with Three.js will not cast a shadow over the entire scene

Why are some cubes in my loop not casting shadows? Despite using a directional light that should cast shadows on all cubes, the shadowing stops after around 5 columns. let dirLight = new THREE.DirectionalLight(0xFFFFFF, 1.5); dirLight.position.set(300, - ...

Unique Javascript Library Focused on AJAX

Looking for a specific JavaScript library that focuses solely on AJAX functionality, such as a basic XMLHttp wrapper. ...

Updates to class variable values in jQuery are failing to take effect

Attempting to create my first JavaScript class has presented some challenges, specifically when it comes to updating a class variable. Despite hours of testing different approaches, I still can't seem to get it right! function ClassName(productId) { ...

Some models showcase crisp textures with Thee.js, while others appear hazy (especially custom designs)

As a beginner in the realm of Three.js, I have been following a tutorial on texturing custom geometric shapes. The tutorial used a classic head OBJ file as a test case, which worked perfectly fine. However, when I tried to tweak the script to render my own ...

Automatically, the "ng-hide" class gets added despite using the correct syntax for ng-show

I am trying to dynamically show/hide a tr tag within a table based on the value of a scope variable in the controller. Despite writing the code correctly, I am facing an issue where the "ng-hide" class is automatically added to the tr tag every time it is ...

Callback after completion of a for loop in Angular TypeScript

During the execution of my javascript async function, I encountered a situation where my printing code was running before the div creation. I needed to ensure that my print code would run only after the completion of the for loop, but I struggled to find a ...

What is a more efficient way to differentiate a group of interfaces using an object map instead of using a switch statement?

As someone still getting the hang of advanced typescript concepts, I appreciate your patience. In my various projects, I utilize objects to organize react components based on a shared prop (e.g _type). My goal is to automatically determine the correct com ...