Tips on displaying Vue objects row by row

I am currently working on a Vue application where I want to display each 'object' in its own table row. However, I'm facing an issue where the objects are being displayed in one column or as separate rows. Is there a way to ensure that '0 BTC AUD 14,745.3' appears in the first row and the next object '1 ETH AUD 312.14' is shown in the second row? As someone new to Vue, I would appreciate any assistance. Thank you!

Below is an image link and the code snippet:

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

<template>
<div class="main">
  <div id="container" v-for="(index) in coin" :key="index">
    <table class="coins">
      <thead>
        <tr class="header">
          <th>Rank</th>
          <th>Name</th>
          <th>Price</th>
        </tr>
      </thead>
      <tbody>
        <td>{{ index }}</td>
      </tbody>
    </table>
  </div>
</div>
</template>

<script>
import axios from 'axios'
var limit = 20

export default {
  name: 'ex',
  data: () => ({
    coin: []
  }),

  created () {
    axios.get('https://min-api.cryptocompare.com/data/top/totalvolfull?limit=' + limit + '&tsym=AUD')
      .then(response => {
        for (var i = 0; i < limit; i++) {
          this.coin.push(i, response.data.Data[i].CoinInfo.Name, response.data.Data[i].DISPLAY.AUD.PRICE)
          // console.log(this.coin[i])
        }
      })
      .catch(e => {
        this.errors.push(e)
      })
  }
}
</script>

Answer №1

To improve the way data is added to your array of coins, consider pushing a single item (array or object) that contains all necessary information in each iteration instead of three separate items. Additionally, rename the array from coin to coins for better code clarity. Here's an example of how you can make these changes:

this.coins.push([i, response.data.Data[i].CoinInfo.Name, response.data.Data[i].DISPLAY.AUD.PRICE])

Next, update the iteration in your template like so. Modify the v-for directive as shown below:

<div id="container" v-for="(coin, index) in coins" :key="index">

Then, adjust the content display in the template:

<tbody>
     <td>{{ coin[0] }}</td>
     <td>{{ coin[1] }}</td>
     <td>{{ coin[2] }}</td>
</tbody>

While I haven't tested this specific code, I believe these suggestions will help steer you in the right direction.

Answer №2

Perhaps consider updating how the object was created

Instead, try this approach:

this.coin.push(i, response.data.Data[i].CoinInfo.Name, response.data.Data[i].DISPLAY.AUD.PRICE)

Try something similar to this:

const coinObject = {
  index: i,
  name:response.data.Data[i].CoinInfo.Name,
  price: response.data.Data[i].DISPLAY.AUD.PRICE
}
this.coin.push(coinObject);

Then you can iterate through it in your template with a loop like this:

 <div id="container" v-for="(coinItem, index) in coin" :key="index">
    <table class="coins">
      <thead>
        <tr class="header">
          <th>Rank</th>
          <th>Name</th>
          <th>Price</th>
        </tr>
      </thead>
      <tbody>
        <td>{{ coinItem.index }}</td>
        <td>{{ coinItem.name }}</td>
        <td>{{ coinItem.price }}</td>
      </tbody>
    </table>
  </div>

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

Handling errors in XMLHttpRequest using Axios in React JS

I am currently utilizing the REACT-JS framework for my FRONT-END development: Below is the action I am calling from REDUX-REACT export function UserLogin(values) { var headers = { 'Access-Control-Allow-Origin': '*', ...

Send data using only Javascript

Hey there, I'm a beginner in javascript and I'm having some trouble submitting a form using pure javascript. Here is my code: var myform = document.getElementById('js-post-form'); myform.addEventListener('submit', function(e ...

Physically moving the window to a specified location using window.scrollTo()

Whenever I use the window.scrollTo(); method upon clicking a button, it simply jumps to the destination without displaying the scrolling motion. How can I ensure that the window physically scrolls to the designated position instead of instantly appearing ...

Capture the onclick attribute with jQuery and then reapply it

I am facing a challenge with a page that has several calendars composed of HTML tables where each day is represented by a td. The td elements have an onClick attribute which I need to manipulate using jQuery. Specifically, I need to remove the onClick attr ...

Challenge yourself with the act of dragging and dropping multiple times using only Javascript

I'm currently working on implementing a drag and drop functionality from one column to another. The goal is to allow multiple copies of an element from the left list to be placed in the right list. Each copy will have a unique ID assigned before being ...

Obtain the position of a value within an array that has been grouped together

In my JavaScript code, I am dealing with two arrays. One array contains Ids: [1,1,1,2,2,2,3,3,...] The other array holds values: [0,1,0,0,0,1,1,0,...] What I need to do is determine the zero-based index of the group of Ids where the corresponding value ...

When attempting to refresh the page, an error occurred stating: 'Unhandled TypeError: Cannot access properties of undefined (reading FetchApi.js:23 'data')'

Upon the initial run of the code, the mappings data appear as desired. However, upon refreshing the page, an error is displayed. Can someone please advise on how to rectify this error and what could be causing it? The provided code: import React, { useE ...

A more efficient method for creating node.js functions to read and write files

Is there a more efficient way to optimize this code? var fs = require('fs'); var file = '/test.txt'; fs.readFile(file, 'utf8', function (err, txt) { if (err) return console.log(err); txt = txt + '\nApp ...

What is the best way to send props from page.js to layout.js in the Next.js app directory?

Is there a way to effectively pass props to layouts in Next.js 13? Can we optimize the approach? Here's an example: // layout.js export default Layout({children}) { return ( <> {/* Display different `text` based on the page.js being ...

Include the root prefix in angular.json

Within my angular.json file, I am utilizing the following code snippet: "root": "admin/", "sourceRoot": "src", "prefix": "app", "architect": { "build": { " ...

Using wildcards in the path of a route is a great way to

I'm trying to implement a back button in my application toolbar that should only show up when the user is on a page within /manage/*. If the user is on either /manage or /other, the back button shouldn't be displayed. Below is the computed prope ...

Adjust the height of a div containing variable elements

I'm having trouble adjusting the height of a div so that it can be scrolled all the way through. This div is positioned next to another one, so I set the overflow-y to scroll. Initially, I thought setting the height to auto would solve the issue, but ...

Personalized Design Incorporating Sections and Sidebars

I need the aside to be positioned on the right side and the section on the left side, both centered in the space. Feel free to check out this link <!DOCTYPE html> <html> <head> <style> #main { width: 800px; margin: 0 auto; } ...

Elements are unresponsive to scrolling inputs

My Ionic 2 input elements are not scrolling to the top when the keyboard is shown. I've tried everything I could find on Google, making sure the keyboard disable scroll is set to false. However, I still can't figure out what's causing the sc ...

Choosing sub-elements in CSS

I am working with HTML code that dynamically creates div elements inside a main div using JavaScript. To easily change the styles of these inner divs after they are created, I have created a new class called "main-light" and utilize jQuery's addClass ...

jQuery's addClass() method behaving unexpectedly when used within an if statement

As a newcomer to jQuery, I am facing an issue with my code. My goal is for jQuery to detect when the user clicks on an li element. If the element already has the class active, the function should stop. However, if the element does not have the class acti ...

NgMap Angular Pins

While working on implementing Ng-Map for Angular in my application, I encountered an issue. I am retrieving entries from the database and attempting to drop markers on the map. However, even though I have 40 entries, only the first 11 are showing up as m ...

Managing API calls through Next.js routing

Here is a next.js handler function for handling api requests on our server. I am looking for assistance on how to access a variable between request methods. Any help would be greatly appreciated. export default function handler(req, res) { if(req.met ...

Replicate styling while copying CodeMirror code

Is there a way to maintain the styling and formatting of javascript code when copying from CodeMirror? Whenever I try to copy and paste from the CodeMirror editor, the coloring and style are lost, with the content pasted as text/plain. How can I preserve ...

Give precedence to several concurrent rules

Setting up a babel server using express for configuration. To serve static files, the following rules have been added: app.use('/', express.static('public')); app.get(/.*\.(png|jpg|js|css)$/, express.static('public')); ...