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

Clicking on React Js reverses an array that had previously been unreversed

My issue involves an array pulled from a database that I have reversed so that the newest entry is displayed at the top when a button is clicked, opening a modal-style window. However, when the array is displayed in the modal window, it appears to be flipp ...

Why do I keep encountering invalid errors with Vuelidate and TailwindCSS?

I am struggling to comprehend why I keep encountering validation errors. The logic seems fine, so it must be something wrong with my :class bindings. Even though I know I will feel foolish once I figure it out, I have gone through numerous tutorials and st ...

Sending an email using Angular is a straightforward process that involves utilizing the built

I need help figuring out how to code a function in Angular or TypeScript that will open Gmail when a specific email address is clicked. I've tried different methods but haven't been successful so far. ...

Modify the colors of the chartist fill and stroke using JavaScript

Struggling to dynamically set colors in a chartist graph using JavaScript. How can custom colors be applied through JS? The attempted method below is not successfully changing the color for the showArea in the chartist graph. <!doctype html> <htm ...

Navigating with vue-router using guard redirections

I am currently working with vue.js and I'm looking to implement a functionality where I can redirect a user to another URL within the navigation guard called beforeRouteEnter. The main objective is to check if the current city of the user is included ...

Clear SELECT After Submission

I have a jQuery script and need a function to reset the SELECT input after it has been submitted. <script> $(document).ready(function() { //elements var progressbox = $("#progressbox"); var progressbar = $("#progressbar"); var statustxt = ...

Create a password variable while typing

Looking to avoid interference from browsers with autocomplete and password suggestions while maintaining the show/hide letters feature. The goal is to keep the password stored as a variable, regardless of whether the characters are shown or hidden. The is ...

What is the best way to choose two <li> elements with multiple classes in my WordPress navigation menu?

I am looking for a JavaScript function that will add the class "current_highlight" when an element with the class "activo2" also has the class "active". Here is my HTML code: <div class="navbar-header"> <button type="button " class="navbar-to ...

Produce a vue attribute

I am looking to display a property in my component. My template includes: <v-flex v-for="c in components"> <component :is="c.component" v-bind="c.prop"></component> </v-flex> And in the script: ... mounted(){ ...

Struggling to center with margin 0 auto

I'm having trouble aligning the middle paragraph in an HTML footer. I attempted to use floats for layout: the first paragraph is floating to the left, the third paragraph is floating to the right, and the second (middle) paragraph has a margin of 0 au ...

Struggling to position an image and text side by side within a div using CSS

Is there a way to display a message inside a div with an icon positioned to the left? The desired outcome is for the icon to always be next to the text and aligned at the bottom-center of the div. I attempted using the :after pseudo element. However, set ...

Unable to create slides with Fancybox if the content is in HTML format

I'm not sure if this issue is a bug or if it's just a restriction of fancybox. Can anyone confirm if it's possible to create a gallery using HTML elements? I've been attempting to accomplish this for hours without success. Here is the ...

Fluid imitation pillars with a decorative outer edge

Looking to create a modern, sleek 2-column HTML5 interface with a left sidebar and main content area on the right. Backwards compatibility is not an issue as all users will be using the latest versions of Chrome or FF. The goal is to give the sidebar a ba ...

The Nuxt.config.js file is responsible for rendering the noscript innerHtml element as it appears in

I've stopped JavaScript in my browser settings Incorporated inner HTML for noscript tag in nuxt.config.js Anticipated outcome is to have an iframe element added inside the body, but it's displaying text instead. How can we insert it as an eleme ...

Experimenting with the inner workings of a method by utilizing vue-test-utils alongside Jest

Is there a way to properly test the internal logic of this method? For instance: async method () { this.isLoading = true; await this.GET_OFFERS(); this.isLoading = false; this.router.push("/somewhere"); } This method toggles isLoading, ...

Consistently ensuring even horizontal CSS padding throughout all components

I'm working on a website using React and TailwindCSS. Each part of the site is its own React Component, but I want to make sure that all sections have consistent horizontal padding and a maximum width where the content stays centered and doesn't ...

Problem with full-page navigation sliding in and fading in and out

Upon the user's click on <a href="#slide-nav" class="slide-nav-trigger">, a full-page navigation smoothly slides into view. This animation is triggered by CSS and uses jQuery for event delegation. The Dilemma Instead of abruptly turning on and ...

Tips for displaying an array and iterating through its children in Angular 7

I am currently working on extracting the parent and its children to an array in order to display them using ngFor. However, I am encountering an issue where the children are not being displayed during the ngFor. I have a service that retrieves data from a ...

Display items in a not predetermined order

Despite its simplicity, I am struggling to get this working. My aim is to create a quiz program using JavaScript. Here is the basic structure: <ul> <li>option1</li> <li>option2</li> <li>option3</li> </ul> &l ...

How to style a div's height responsively behind a circle using CSS

Can a CSS pattern be created with responsive design, ensuring that the background line is always in the correct position and height regardless of window size? https://i.sstatic.net/27Tf2.jpg ...