What is the best way to reset a package with every route change in Vue?

Recently, I stumbled upon a fantastic tool for managing tables and it has been a game-changer for me. However, there is one issue - the tool requires initialization as indicated below, which means it only runs once when the page loads.

mounted: () => {
  $(document).ready(function () { 
    $("table").DataTable();
  }
}

I attempted to place the code in both the mounted and created sections. Due to what I suspect is the rendering order, I had to stick with the ready method for it to work properly.

This presents two challenges for me. First, I use the same component as a matrix view in various components (it's dynamically configured based on the store). Second, upon navigating away from the page and returning, the table does not reinitialize.

What is the best approach to ensure that the code within the ready method runs each time the component comes into view?

I have searched online but haven't found much information on this specific issue. The most relevant resource I came across was the lifecycle of the component, although it didn't provide a breakthrough solution. I also discovered that the data table instance needs to be destroyed, but that seems futile if I can't trigger the process.

Answer №1

To solve this issue, simply follow these steps:

mounted () {
    $("table").DataTable();
}

In jQuery, $(document).ready is used to detect when the document is ready. However, in Vue, mounted is called after the instance has been mounted, with el being replaced by the newly created vm.$el, which is equivalent to document.ready.

I have verified in Vue 2.x.x that mounted is triggered even if you navigate away from the page and then return.


If your code depends on data loading and component re-rendering, you can utilize updated instead of mounted. This hook is executed after a change in data triggers the virtual DOM to be re-rendered and patched.

updated () {
    $("table").DataTable();
}

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

Express JS failing to detect the current environment

I have organized my application folder structure in the following way. app/ config/ app.js env.js server.js Every time I try to run my app.js file, it displays "server started at undefined". Here is a link to the code snippet: Gist Cod ...

Codeigniter 3 and Ajax: Troubles with receiving post data

I'm struggling to successfully send some data via Ajax to save it inside a controller. Unfortunately, I am unable to retrieve the posted data inside my controller as it always appears empty. Here is my basic Ajax code that just doesn't seem to w ...

What is the reason behind Collection.bulkwrite() only writing a single document to the MongoDB database?

I have utilized ag-grid to create this grid, and my objective is to bulkwrite its data into my MongoDB database by clicking on the saveInformation button. https://i.sstatic.net/bbMN4.png app.component.ts saveInformation() { console.log('actio ...

Troubleshoot: Json causing issue with displaying markers on Google Maps API (v3)

I developed a custom Google Maps application using JSON data and implemented PostgreSQL database integration. Here is the code snippet: <script type="text/javascript"> var map; var national = [{"lng":"-6.173319","city":"JAKARTA","lat":"106.818 ...

Accessing the Vuex store from external JavaScript files is not allowed

The structure of my application can be found in the following link Architecture of my app For my specific use case, I am looking to utilize getters in the Axios interceptor (/../src/app/shared/services/http-client/http-client.js) to include the authoriza ...

Stack two divs together

It may seem silly, but I just can't get it to work. I'm attempting to enclose two divs with different classes in another div, but my current code is automatically closing the divs. There are multiple sets of divs with classes .panel-heading and ...

What is the best way to perform two separate actions on a single element within an array?

In my MongoDB database, I have a collection called users with the following document structure (simplified for clarity): { _id: ObjectId, name: 'John Doe', sportsList: [ // array of objects { name: 'football', leve ...

Interactions of selecting dates in datepicker widget

Currently, I am working on developing my personal work website. One issue I have encountered is with the calendar feature. The "From" date can be selected before today's date, which is not ideal. Additionally, if a date 5 days from now is chosen as th ...

Creating a custom JavaScript clock based on stored database values

I am looking to create an analog clock using JavaScript. I have both working hours and off hours stored in a database. My goal is to display working hours in one color and off hours in another color on the clock. How can I achieve this? The database prov ...

Problem with using Twitter Bootstrap in IE11 when Browser Profile Enterprise is enabled

I am facing an issue with a Bootstrap 3 web page. Some machines have IE configured to load intranet pages in Enterprise profile mode, while others have the default Desktop profile set in IE11. This configuration has caused the layout to break completely. ...

If the search term entered does not match any options, v-autocomplete will not display a drop-down list

Currently, I am facing an issue with a search box utilizing v-autocomplete where users can search based on institution name or city. The dropdown only displays institution names, but when a user searches by city, the data is present in the debugger, howeve ...

Using CORS with Spring Boot and React Admin in React applications

For the admin interface, I am utilizing a tool called "React Admin" along with Spring Boot for my REST API. The React app's URL is: "http://localhost:3000", while the Spring Boot API's URL is: "http://localhost:8080". In my project, I have a sep ...

What is the correct method for formatting a JavaScript tag?

As someone who has been developing web apps since 1996, I am constantly exploring new approaches to familiar tasks. This has led me to ponder on the best JavaScript tag to use for my latest projects. Currently, I tend to utilize something like the followi ...

Timeout for jQuery animations

My jQuery animation script is causing an issue where it does not finish the animation before returning to the parent function. Specifically, the JavaScript code changes a background color, calls the animate function, the animation starts but doesn't c ...

Vue computed property does not reflect updated value

I'm attempting to modify a Computed Property that receives its data from the vuex state. The data is retrieved from the store. I aim to update the value allowed in the store. But, when I click on my update button, the value does not get updated. Her ...

Creating SVG Lines with Google Maps JavaScript API v3

I have a project that requires a dashed line between two points on Google Maps using JavaScript v3. The specification states that each dash should be 100px long. I have attempted to achieve this using SVG, but the dashes are not displaying correctly. Here ...

Exploring Bootstrap 4 Grid - Understanding the Difference Between .col-breakpoint and .col-number

I've recently decided to make the move from Bootstrap 3 to Bootstrap 4 and I'm taking a closer look at the changes in the grid system. https://getbootstrap.com/docs/4.0/layout/grid/ It explains: Thanks to flexbox, grid columns without a spe ...

Alert all simultaneous elements of modifications

I am attempting to update parallel components (which are rendered with v-for) when changes occur. Here is the code snippet I am using: <div v-for="item in items" :key="item.ID"> <my-component :item="item"></ ...

Using PHP and AJAX to retrieve an array stored in a javascript file using the $_FILES variable in PHP

My current setup includes HTML input: <input type="file" id="foo" multiple/> Accompanied by this snippet of javascript: $("#foo").change(function(){ var files = $(this)[0].files; $.ajax({ // ??? }); }); In my PHP file, named uploa ...

Aggregate the divided values from an HTML form to generate data for a Google scatter chart

I am in the process of developing an online quiz and am interested in displaying the scores on a chart. My goal is to calculate the sum of comma-separated values from checked radio buttons in a form, ensuring that the result remains in the same format. I ...