When using vis.js timeline with an asynchronously loaded dataset, you'll need to take a few steps to properly update an item. Since the dataset is loaded asynchronously, you cannot update the item directly in the initial configuration. Instead, you must wait for the dataset to be loaded and then update the item.

Here's a general approach to update an item in a vis.js timeline when the dataset is loaded asynchronously:

  1. Initialize the Timeline: Create the initial vis.js timeline with an empty dataset or a placeholder dataset. This allows you to render the timeline without any items until the actual dataset is loaded.

    javascript
    // Initialize the timeline with an empty or placeholder dataset const container = document.getElementById('timeline-container'); const timeline = new vis.Timeline(container, {}, {});
  2. Load the Dataset Asynchronously: Load your dataset asynchronously using AJAX, fetch, or any other method appropriate for your application. Once the dataset is retrieved, update the timeline with the new dataset using the setItems method.

    javascript
    // Load the dataset asynchronously fetch('/your/api/endpoint') .then(response => response.json()) .then(data => { // Update the timeline with the loaded dataset timeline.setItems(data); }) .catch(error => { console.error('Error loading dataset:', error); });
  3. Update an Item: After the dataset is loaded and the timeline is updated, you can then update an individual item using the update method.

    javascript
    // Update an item in the dataset const updatedItem = { id: 1, start: new Date(2023, 7, 20), content: 'Updated Event' }; timeline.itemsData.update(updatedItem);

    In this example, we're updating an item with the ID 1 to have a new start date and updated content. The update method will apply the changes to the item in the timeline.

Remember that the timeline will automatically re-render with the updated item after using the update method.

By following this approach, you can update items in a vis.js timeline even when the dataset is being loaded asynchronously. The timeline will render with the initial dataset, and once the real dataset is available, it will be loaded into the timeline, allowing you to update items as needed.

Have questions or queries?
Get in Touch