How can the table header be displayed in a Vue JS application after receiving the server response?

On my Vue.JS web page, I am making an API call using the Http GET method. Currently, the table header is displayed before receiving the API response, which doesn't look good. Can someone help me modify my code so that the table header is only displayed after the server response is received? Thank you in advance.

PackageInfo.vue

<template>

<h3> Package List </h3>;

<div>

  <br>   <br> <br>
    <button @click="fetchPackageIDs"> Get Package IDs </button>

    <br> <br>
  <table class="table-bordered table-hover table-sm" align="center"gt;
  <thead v-if="posts.packageIDs.length > 0" class="thead-light">
    <tr scope="row" align="center">

      <th> Package IDs </th>
      <th> Package Type </th>
      <th> State </th>
      <th> Sub State </th>

    </tr>
  </thead>
  <tbody>
    <tr v-for="(user) in posts.packageIDs" :key="user.packageId" align="center" scope="row">

      <td>{{ user.packageId }}</td>
        <td>{{ user.packageType }}</td>
        <td>{{ user.state }}</td>
        <td>{{ user.subState }}</td>

    </tr>
  </tbody>
</table>
 <br><br><br>
</div>

</template>

<script>

import { apiHost } from '../config';
import axios from 'axios';

export default {
    name: "PackageInfo",
    data() {
        return {
            posts: {

                packageIDs: []

            }

        }
    },
    methods: {
        fetchPackageIDs(e) {
          const url = apiHost + 'tdg/packageIds/' + localStorage.getItem('userId');
          console.log(url);
          e.preventDefault();

           axios.get(url, null, null)
          .then(response => this.posts.packageIDs = response.data)
          .catch(error => {
                console.log(error); 
          })
          console.log(this.posts.packageIDs);

         },
    }
}

</script>

<style >

.form-control:focus{
border-color:#167bff;
box-shadow:none;
}

.auth-inner{

width:450px;
margin:auto;
background:#ffffff;
box-shadow:0px 14px 80px rgba (34,35,58,0.2);
padding:40px 55px 45px 55px;
border-radius:15px;
transition:all .3s;

}

table.table-bordered{
    border:1px solid black;
    margin-top:20px;
  }
table.table-bordered > thead > tr > th{
    border:1px solid black;
    padding:7px 30px;
}
table.table-bordered > tbody > tr > td{
    border:1px solid black;
    padding:7px 30px;
}

</style>

Answer №1

To implement this functionality, make use of the boolean attribute.

<template>
   <div>
      <table v-if="displayTable">
         ...
         <tr v-if="items.packageList.length === 0">
             <td colspan={columns}> No Items Available </td>
         </tr>
      </table>
   </div>
</template>

<script>
    export default {
       data () {
          return {
              displayTable: false,
              items: {
                  packageList: []
              }
          };
       },
       methods: {
          loadPackageList(e) {
           this.displayTable = false;
           axios.get({url})   
          .then(response=>this.items.packageList=response.data)
          .catch(e => {console.log(e);})
          .finally(() => this.displayTable = true)
         }
       }
    }
</script>

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

Ensuring the modal is stored in one central location (Bootstrap)

I'm currently working on a website project using bootstrap, and I need to incorporate the same modal across all pages. Right now, I find myself duplicating the entire code in each HTML file to make it accessible on every page. However, this method is ...

extracting the identifiers and class names from an HTML document

I have successfully extracted tags from an HTML file using getElementByTagName. However, I am also interested in parsing the IDs and class names within the same HTML file. This is my current approach: $html = new DOMDocument(); $html->loadHTML ...

Unable to retrieve all form properties when using enctype='multipart/form-data'

Recently, my NodeJS server has been successfully uploading files to Amazon using a secret form. However, in an effort to enhance security measures, I decided to introduce a password field to the form. Unfortunately, upon doing so, I encountered an issue wh ...

Error occurred due to missing dependency during file import

I'm attempting to include a new dependency within the script tag of a Vue.js Component. import AuthenticationService from '@/services/AuthenticationService.js' The error message that I am encountering states - This dependency was not foun ...

Updating the parent's reference from a child component in Vue 3

In one of my child components, I have a component named Navbar that includes an option for logging out. <a @click="(event) => {event.preventDefault();}"> Logout </a> This Navbar component has been imported into my parent compon ...

What is the method for generating a popover in HTML when a user hovers over a button?

In this scenario, I have implemented two status buttons with different colors. The green button corresponds to "RUNNING" and the red button indicates "TERMINATED", which are fetched from JASON data. Upon hovering over the green status button, the text "RU ...

Fixing a menu hover appearance

I recently encountered a small issue with the menu on my website. When hovering over a menu item, a sub-menu should appear. However, there seems to be a slight misalignment where the submenu appears a few pixels below the actual menu item. Check out the w ...

What is the best way to automatically show scrollbars when a page loads using JavaScript?

How can I make the vertical scrollbar appear as soon as the page loads using javascript? I am currently using a jquery slide toggle animation that causes the vertical scrollbar to show up because it makes the page longer. However, when the scrollbar appear ...

Instructions on submitting a form containing multiple select lists using the enter key

After researching various threads, I have come across solutions using the JS onkeypress function to trigger actions with input fields. However, in my case, I need to implement this functionality with a form that consists of multiple select lists instead of ...

Unable to align text in the middle of the header

Seeking help with my design flaws, I have created a fiddle to showcase the issues that arise when attempting to make it responsive. The main problem is that the HEADING IN CENTER text is not aligned properly in the center. If I remove the position attribut ...

Issue encountered while attempting to register child component within vue.js

Encountering the following error: Unknown custom element: - have you registered the component correctly? For recursive components, ensure to include the "name" option. I am unsure of what's causing the issue in the code. I've referred to this ...

NUXT: ERROR Unexpected export token encountered while running npm dev command

I am relatively new to using nuxt and currently working on my first website with it. I encountered an error that I am struggling to resolve. My WSL Ubuntu development environment was functioning properly until I mistakenly ran npm run build instead of npm ...

Leveraging HTML tables for input purposes

Just starting out with php, HTML, and mysql. Using HTML tables for the first time here. Created an HTML table filled with data from a MySQL table. Planning to use this table as a menu where users can click on a cell with a specific date. The clicked date ...

Guide to adding a Json file in a PHP file with PHP

I have a PHP file with an embedded JSON file, and I want to update the JSON file with new information from a form. The form looks like this: <form action="process.php" method="POST"> First name:<br> <input type="text" name="firstName"> ...

Overlay text on top of image with fading transparency when hovering over it

Is there a way to make the transparent layer that appears when hovering over an image on a card only cover the image itself and not extend onto the footer of the card? Currently, the image on the card covers most of it, and I want the hover overlay to beh ...

Whiskey Angostura and Straight Row Set to 8, not 12

Recently, I incorporated Bourbon, Bitters, and Neat into my rails application. However, I am encountering an issue where the number of grid-columns is stuck at 8 instead of the desired 12. To address this problem, I have taken the following steps: Ensur ...

Make sure to load the gif image before any other images are loaded from the css

Hey there, I'm attempting to display a GIF image before the actual image loads. I found a great example to accomplish this: Example: Java Script function loadimage(imgsrc, change){ var loadimage = new Image(); loadimage.onload = changesrc(imgsrc, c ...

Limit the number input to only allow values between 0 and 100

I am utilizing the Number input component from MUI, which includes Increment and Decrement buttons for adjusting numbers. Is there a method to limit the input to only accept values ranging from 0 to 100 ? Additionally, how can I decrease the width of the ...

Minimal Space Separating Footer Division and Body Element

While there are numerous discussions online about fixing footer alignment issues, I've encountered a unique problem that needs addressing. The issue at hand is a 29px gap between the bottom of the footer and the <body> tag on my webpage, which ...

What is the best way to limit the dimension of uploaded images to a specific height and width?

function validateImageDimensions(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function(e) { $('#uploadForm + img').remove(); var img = $('<img> ...