The issue of misplaced data and nested v-for loops

I received some rather messy data from a backend API in JSON format and I am struggling to display it correctly within an HTML table. The list items are not aligned properly within the table.

For instance, as shown in the screenshot, items like 2.1, 2.2, etc. are mistakenly placed under 1.. They should be shifted down to their appropriate numbered lists. It is apparent that the data from the API is flawed. My goal is to find a way to visually resolve this issue.

https://i.sstatic.net/lmyvG.png

Do you have any suggestions on how to address this problem?

<template>
<div>

  <table>
    <!-- <tr v-if='eg[0].type == "ExREF"'> -->
      <tr>
      <th>General:</th>
      <td>
        <ol><li v-for='(item, index) in eg' v-if='item.type == "ExREF"' :key='index'>{{item["General Considerations Guidance"]}}</li></ol>
      </td>
    </tr>
    <tr>
      <th>Considerations:</th>
      <td>
        <ol>
          <li v-for='(item, index) in eg' v-if='item.type == "Extract"' :key='index'>
            {{item["General Considerations Guidance"]}}
            <ol>
              <li v-for='(a, index) in item.Nodes' :key='index' type='a'>
                <em>{{a.Node}}</em> {{a["General Considerations Guidance"]}}
                  <ol>
                    <li v-for='(i, index) in a.Nodes' :key='index' type='i'>
                      <em>{{i.Node}}</em>{{i["General Considerations Guidance"]}}
                    </li>
                  </ol>
              </li>
            </ol>
          </li>
        </ol>
      </td>
    </tr>
  </table>

</div>
</template>

Answer №1

If you want to organize the text-heading nodes effectively, you can follow these steps:

  1. Flatten Nodes - this action will rearrange all nodes to the same level, making sorting easier
  2. Sort Nodes - sort the nodes based on type and heading number
  3. Group Nodes - once sorted, group the nodes under their respective headings

To achieve this, you can create a computed property (e.g., named "egSorted") to display the sorted input (eg):

import { sortNodes } from './tree.js';

export default {
  data() {
    return {
      eg: [...]
    };
  },
  computed: {
    egSorted() {
      return sortNodes(this.eg);
    }
  }
}

In your template, make sure to use egSorted instead of eg.

Check out the demo here

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

I am looking to access a public method from a different component in Angular 2

Trying to access the headerExpand property from app.component is causing an error message in the console: metadata_resolver.js:559 Uncaught Error: Invalid providers for "Page1" - only instances of Provider and Type are allowed, got: [?undefined?] page1 ...

What is the best way to clear the content of a contenteditable element in React?

I have a scenario where I am rendering an array of items, each with a contenteditable field. MainComponent.js import { useState } from "react"; import Item from "./Item"; import "./styles.css"; export default function MainC ...

The Angular Material dialog fails to display content when triggered within an event listener in Google Maps

Within my project, I am utilizing Angular 6.0.6 and Angular Material 6.3.0. One issue I have encountered is with a dialog component that I have added to the entryComponents in the app module. Strangely, when I attempt to open this dialog within the rightcl ...

Load the page using AJAX and automatically scroll to the anchor within the loaded content

I want to achieve an automatic scroll to the beginning of a loaded page after using ajax to load it. Here is the code snippet I currently have: html <button class="btn btn-info btn-lg" type="button" id="photos"> <i class="fa fa-plus fa-5x">&l ...

Utilizing variable values in HTML and CSS to enhance a website's functionality

My current project involves modifying an HTML web resource for use in Dynamics 365. I need to replace a static URL with a dynamic value obtained via Javascript, specifically: var URL = Xrm.Page.context.getClientUrl(); There are multiple instances within ...

Minimize/Maximize Swagger Response Model Class View

After successfully integrating Swagger API documentation with my rest services, I encountered a challenge. The Swagger page appears too lengthy due to the numerous response classes in my project, requiring users to scroll extensively to find information. ...

sort the array based on its data type

Recently diving into typescript... I have an array that is a union of typeA[] | typeB[] but I am looking to filter based on the object's type interface TypeA { attribute1: string attribute2: string } interface TypeB { attribute3: string attri ...

In JavaScript, Identify the filename selected to be attached to the form and provide an alert message if the user chooses the incorrect file

I have a form in HTML that includes an input field for file upload. I am looking to ensure that the selected file matches the desired file name (mcust.csv). If a different file is chosen, I want to trigger a JS error. Below is the form: <form name="up ...

Getting child components within a Vue.js application can be achieved by using methods such as

I have developed an app that consists of multiple child components, functioning similar to a spreadsheet. My goal is to calculate the sum of all components whenever any cell undergoes a change. I have managed to store the values of the cells by caching th ...

Improving the method of accomplishing a task using AngularJS

Clicking on an item in the tobeclicked list will transfer the data to the find empty list. The goal is to locate an empty list and update it by cloning the image there. The actual values and lists will include images. When an image is clicked, the system s ...

When employing useEffect, clearInterval cannot halt the execution of SetInterval

My attempt to create a function that initiates a countdown when the isPlaying variable is true and stops when it's false has not been successful. Instead of working as intended, it starts multiple intervals concurrently. The value of isPlaying changes ...

Enhancing multiple documents in mongoDB with additional properties

I am working with a data structure that includes user information, decks, and cards. I want to update all the cards within the deck named "Planeten" by adding a new property. How can I achieve this using a mongoose query? { "_id": "5ebd08794bcc8d2fd893f ...

Ways to avoid triggering the re-render of a dynamic component

I am working on a component that has reactive properties. I am looking for a way to have the properties updated without triggering a DOM rerender. Also, I would like to be notified when the properties are changing through any available event. Does anyone ...

Is it possible to append an "index" to a database field using Express, Loopback, or NodeJS?

Currently, we are utilizing the Express/Loopback Framework for our backend. I am currently working on ensuring that indexes on certain fields in models are properly created in MongoDB. Is there a way to utilize meta-tags within the models so that DataJuggl ...

Modifying the attribute of an element inside an array

Presented below is an object: { "_id" : ObjectId("5a8d83d5d5048f1c9ae877a8"), "websites" : [ "", "", "" ], "keys" : [ { "_id" : ObjectId("5a8d83d5d5048f1c9ae877af"), "name ...

Struggling with getting the JavaScript, scss, and CSS television animation to turn on and off properly? Seeking assistance to troubleshoot this

After finding this javascript code on Codepen and seeing that it worked perfectly in the console there, I tried to run it on my computer with jQuery but it didn't work outside of Codepen. Even when attempting to use it on JSfiddle or compile the SCSS ...

Laravel Mix causing issues with Vue loading when extract() is used

I currently have a simple webpack.mix.js and resources/js/app.js setup in my Laravel project. Here is the code from webpack.mix.js: const mix = require('laravel-mix'); mix.js('resources/js/app.js', 'public/js') .sass(&ap ...

Can you provide some insight on how to store XMLHttpRequest response in Javascript for future

I have a function in my codebase that is responsible for loading HTML templates asynchronously: loadTemplate: function(url) { return new Promise(function(resolve, reject) { var xhr = new XMLHttpRequest(); xhr.open("GET" ...

Unable to show an image within the <div> element when hovering the mouse

Is it necessary to use the background style image in order to display an image on mouseover of a name? I have implemented a controller and would like the image to be replaced by text within a div tag. Below is my HTML code: <!DOCTYPE html> <html& ...

Publishing Your App on the Android Market with PhoneGap

Seeking a comprehensive PhoneGap tutorial that covers app publishing, especially struggling with successful app deployment. Currently experienced in HTML, CSS, and JavaScript. Any tips or advice would be greatly appreciated, thank you! I have a good gras ...