What is the best way to showcase the data retrieved from axios in a Vuejs table?

I am new to Vuejs and encountered an issue while trying to display user information retrieved from API endpoints using axios. Below is the code snippet for fetching data:

import axios from 'axios';

export default {

  data(){
        return{
            userInfo: []
        };
    },

  created(){
      this.getInfo();
  },

  methods:{
    getInfo(){
        axios.get('http://URL')
        .then(response => this.userInfo = response.data)
        .catch(error => this.userInfo = error.data);
    },      
  }  
}

I have successfully stored the data in the userInfo array, which includes username, userId, etc. How can I display these contents in tabular form within the template section of my VueJS component?

If the following schema represents my table structure in the HTML part of VueJS template, how do I populate the rows with data fetched from the API endpoint through userInfo?

<el-table>
  <el-table-column min-width="50" type="index"></el-table-column>
  <el-table-column min-width="150" label="Name"></el-table-column>
</el-table>

The code above creates a column, but I need assistance on populating it with rows based on the userInfo array, specifically displaying usernames under the "Name" column.

While I've managed to display the list of usernames as a bullet list with the following code:

<ul id="displaying">
    <li v-for="user in userInfo" :key="userInfo.id">{{user.username}}
</li></ul>

I'm now looking for guidance on how to represent these usernames in a tabular format under the mentioned "Name" column.

Your help would be highly appreciated.

Answer №1

In my personal view, Element UI can be quite challenging to navigate due to its complex documentation, but if you find it necessary...

<el-table :data="userInfo"> <!-- 👈 connect the table's "data" prop to your "userInfo" array -->

  <!-- this serves as an index column, showing row numbers starting from "1" -->
  <el-table-column type="index" min-width="50"></el-table-column>

  <!-- this column displays the "id" property of your objects -->
  <el-table-column prop="id" label="ID"></el-table-column>

  <!-- this column shows the "username" property of your objects -->
  <el-table-column prop="username" label="Username"></el-table-column>
</el-table>

Initially, no data will be shown until your userInfo array is populated after the completion of the Axios request.


If you prefer a simple HTML table to display your data, you could use something like the following:

<table>
  <thead>
    <tr>
      <th></th>
      <th>ID</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="(user, index) in userInfo" :key="user.id">
      <td>{{ index + 1 }}</td>
      <td>{{ user.id }}</td>
      <td>{{ user.username }}</td>
    </tr>
  </tbody>
</table>

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

Utilizing Axios GET Method with Authorization Header in a Vue Application

Currently, I am attempting to make a GET request using axios to an API that requires an Authorization header. Below is the code I am currently working with: My Code: data () { return { listings: [], AuthStr : 'Bearer ' + JSON.pa ...

What is the best method for dynamically increasing the data in a shopping cart?

My goal is to stack cart items dynamically every time the addProduct() function is called. I've successfully captured the data, but I'm facing an issue where the quantity always remains at 1 on each function call. Here's the logic I've ...

Guide on accessing a nested array within a JSON object using JavaScript

I'm currently faced with a json object that contains nested arrays and names with spaces like Account ID. My goal is to only display the Account ID's within my Vue.js application. While I can access the entire response.data json object, I'm ...

What are the steps to successfully integrate Vuetify 2.3 or any other packages into a Vue 3 Project?

How can I properly register Vuetify in my main.js file without using Vue alias? After importing Vuetify, all of my components are hidden. Dependencies: "vue": "^3.0.0-rc.7", "vue-router": "^4.0.0-0", &quo ...

I am unable to load any HTML files in my VueJS iframe, except for the index.html located in the public

Within the template of my vue component, I am utilizing the following code: <iframe width="1000vw" height="600vh" src="../../public/myHtmlFile.html"></iframe> Unfortunately, the file specified in the src attribut ...

Adding properties with strings as identifiers to classes in TypeScript: A step-by-step guide

I am working with a list of string values that represent the identifiers of fields I need to add to a class. For instance: Consider the following string array: let stringArr = ['player1score', 'player2score', 'player3score' ...

Effortlessly Retrieve Initial Data from Ajax in Vue3 using Composition API

After extensively researching different approaches, I am leaning towards using the Vue3 composition API in its "setup" form for future compatibility. However, I am encountering a significant amount of variability across these approaches. My current form i ...

The thread_id_key value must not be 0x7777 in order to execute the function find_thread_id_key, as specified in the ../src/coroutine.cc

I am currently working on a Vue project and encountered an issue after running npm run serve. The error message I received is as follows: 66% building 670/715 modules 45 active /Users/bytedance/go/src/code.byted.org/ad/omega_fe/node_modules/echarts/lib/cha ...

Retrieving a single document using Firebase with VueJS

My goal is to retrieve a specific document from firebase using the auto-generated ID as my route parameter. However, I am not getting any data in return. I want to use the when() method to fetch a document with an auto-generated ID that matches my query, ...

In Vue, reactivity does not extend to nested child objects

I am dealing with JSON data structured like this- items: { id: 1, children: [{ id: 2, parentId: 1, children: [{ id: 3, parentId: 2 }] }] } level-1 children represent parent items that all possess a parent_id of 1 (the roo ...

What could be causing my unit test to fail when trying to retrieve the text from a single-file component that was set using $el.innerText within the mounted hook?

Description of the Issue There is an editable div element that receives a default value of an empty string for its content property. Instead of using mustache syntax to display the content like this: {{ content }}, I set the innerText of the element using ...

What is the preferred approach: modifying the Pinia state within actions or directly within the component?

One challenge I've encountered involves a function within my Pinia store: async function getCurrentUser(){ let user: User | undefined = undefined; if (auth.currentUser?.uid) { user = await getUserById(auth.currentUser.uid) state.va ...

Images not showing in Vue.js

I have been working on setting up a carousel using bootstrap-vue. It is being generated dynamically through an array containing three objects with keys such as id, caption, text, and image path. The issue I am facing now is that while the caption and text ...

What is the best way to apply variables from a SCSS file to functions within a .vue file?

After following the steps provided in this link, I have successfully configured sass for my project. First, I installed sass using npm install -D sass-loader sass. Next, I created a file named variables.scss under the src directory: src/variables.scss $ ...

The instance is referencing the property or method "sendResetMail" during render, but it is not defined

I'm pretty new to Vue and struggling with an error message while trying to get a reset email modal working in my Vue project: The error says that the property or method "sendResetMail" is not defined on the instance but referenced during render. I ...

What steps should I take to modify this Vue.js function?

<script lang="js"> import { ref } from 'vue'; export default { setup(){ const dateInput = ref(new Date()); function handleDateSelection(payload : Date): void { console.log(payload); } return ...

Vue function that inserts <br> tags for addresses

My Vue filter retrieves and combines address details with a , Vue.filter('address', (address, countryNames = []) => { const formattedAddress = [ address?.name, address?.company, address?.add1, address?.add2, address?.town ...

The pagination functionality in Vue.js does not behave as described in the documentation

I've been working on implementing this paginator in vue and come across an issue with the layout. Here is what my current paginator setup looks like: https://i.stack.imgur.com/6ovhU.png I followed the instructions to import and use it as shown below ...

Issue with Vue 3 binding when select option is not displaying in updated values, causing select to remain blank instead of changing to an empty value

Having two components - an Input and a Select, where the options displayed in the select depend on what is inputted in the Input. If the selected option is not present in the new set of options, either choose the default option or the first option "select ...

Is the screen size detection malfunctioning when using Vue 3's computed property?

How do I accurately detect screen size in Nuxt 3? I have tried using the code below, but the screen size always shows as 1200. It seems that the computed property is being executed before the onMounted() hook. How can I resolve this issue? <template> ...