3

What is the best way to hide the progress-linear component ? Should i have it implemented as after waiting for many seconds after doing :

axios.get(this.endpoint)
        .then(response => this.data = response.data)
        .catch(error => console.log(error.response.data));

I want to hide the progress-bar after populating the array.

        <v-progress-linear style="margin : 0 auto ;" :indeterminate="isLoading" v-show="isLoading = false"></v-progress-linear>

My aim here is to show the for some x seconds so that I can show it Some cool effect

TheBAST
  • 2,680
  • 10
  • 40
  • 68
  • So you are not asking how-to, but what's-the-best-way to do it? Can you clarify? Because it seems you can hide it by setting `isLoading` to `false`. When will you set it is up to you (depends on use-case). – Traxo Jul 16 '18 at 10:16
  • @Traxo, I'm asking from you since you have a better approach in these things :) and yeah, I forgot to copy the isLoading = false – TheBAST Jul 16 '18 at 10:20
  • But I don't quite understand what's the question here. So I kindly asked you to clarify if possible. If you want to hide it some time after you populate the array, then you can use `setTimeout` inside `.then` for example. If you are asking about which approach to take then probably [UX](https://ux.stackexchange.com) is better place to ask (but you might want to include more info)? Unless I'm misunderstanding something. Perhaps someone else can confirm as well. – Traxo Jul 16 '18 at 10:26
  • initally the progress bar will show after the array is filled then hide the progress-bar :) – TheBAST Jul 16 '18 at 10:34
  • No problem, `setTimeout` still applies. so then you can `this.isLoading = true; setTimeout(() => {this.isLoading = false}, 5000);` – Traxo Jul 16 '18 at 10:37
  • @Traxo, Just like this : ? ```axios.get(this.endpoint) .then( response => this.parcels = response.data setTimeout(() => {this.isLoading = false}, 5000); ) .catch(error => console.log(error.response.data));``` > i get an error from that – TheBAST Jul 17 '18 at 03:01
  • 1
    you missed parentheses: `axios.get(this.endpoint) .then(response => { this.parcels = response.data; setTimeout(() => { this.isLoading = false; }, 5000); } );` – Traxo Jul 17 '18 at 08:26
  • @Traxo, I didn't knowthat there is a setTimeOut Property in there :) – TheBAST Jul 17 '18 at 13:46
  • 1
    @Traxo, you code is NOT working, I get error, "isLoading" is not defined on the instance but referenced during render. Please create a jsfiddle sample – hoogw Sep 14 '18 at 18:59
  • @hoogw Yes, because I used OP's example in my comment. See codepen in my answer, it works. – Traxo Sep 16 '18 at 17:17

4 Answers4

3

Just share my working code:

template:

:loading="progress_bar_loading"    <!--- ----- progressing bar indicator --------- true: show ---------- false: hide  --->

you need set :loading = "any_variable",

later in js, you will reset it to true/false as show/hide the progress bar.

the context is here:

  <v-data-table
            :headers="headers"
            :items="user"

            :loading="progress_bar_loading"    <!--- ----- progressing bar indicator --------- true: show ---------- false: hide  --->


            :search="search"
           <!---  hide-actions  --->      <!--- ----- hide/show  pagination -------------  --->
            class="elevation-1"


          >



                <v-progress-linear slot="progress"  indeterminate></v-progress-linear>

                                <template slot="items" slot-scope="props" >
                                  <td>{{ props.item.full_name }}</td>
                                  <td class="text-xs-left">{{ props.item.name }}</td>
                                  <td class="text-xs-left">{{ props.item.password }}</td>
                                  <td class="text-xs-left">{{ props.item.agency }}</td>
                                  <td class="text-xs-left">{{ props.item.level }}</td>

js:

before ajax or fetch you show progress bar by:

      // show v-data-table progress bar
             self.progress_bar_loading = true

after ajax fetch callback success, you hide progress bar by:

       // hide v-data-table progress bar
                                self.progress_bar_loading = false

Note: you must use self. Do not use this. because of scope issue.

the context is here:

new Vue({
   el: '#app',
       data: () => ({



  created () {

 // Use creation hooks if you need to set things up in your component both during client rendering and server rendering. 
 // You will not have access to the DOM or the target mounting element (this.$el) inside of creation hooks 

this.initialize()
   },


  mounted () {

//Use mounted if You need to access or modify the DOM of your component immediately before or after the initial render.
// Do not use mounted if You need to fetch some data for your component on initialization. Use created (or created + activated for keep-alive components) for this instead, especially if you need that data during server-side rendering.

//this.initialize()
   },




   methods: {


initialize () {



                 var getUser_url = url + 'cfc/sw.cfc?method=getUser&returnformat=json&queryformat=struct';

                console.log(getUser_url )

            /*
                You can use a plethora of options for doing Ajax calls such as Axios, vue-resource or better yet the browser's built in fetch API in modern browsers. 
                You can also use jQuery via $.ajax() API, which simply wraps the XHR object in a simple to use method call 
                but it's not recommended to include the whole jQuery library for the sake of using one method.
                https://www.techiediaries.com/vuejs-ajax-http/

                http://updates.html5rocks.com/2015/03/introduction-to-fetch
                The Fetch API provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline, such as requests and responses. 
                It also provides a global fetch() method that provides an easy, logical way to fetch resources asynchronously across the network.

            */


                //   **********  must use self = this ************** 
                // this reference vue-app.  must pass it to self, then pass into callback function (success call back)
                var self = this;  




                // show v-data-table progress bar
                 self.progress_bar_loading = true




                fetch(getUser_url)
                        .then(function (response) 
                               {

                                     // if js error here, likely it is coldfusion server output error message instead of valid json 
                                     // so check coldfusion server response.
                                       return response.json();

                                })

                        .then(function (result) {




                                 //------------------------ properties to lowercase ----------------------

                                 /* 
                                     result = [{"FULL_NAME":"xXx"}, {}, {}....... {}]
                                     result is upper case, must convert all properties to lowercase, 
                                     result = [{"full_name":"xXx"}, {}, {}....... {}]
                                     however, the value like password or number MUST remain no change. 
                                 */

                                 // result = [{}, {}, {}....... {}]
                                    var result_lower_properties= [];

                                    var arrayLength = result.length;

                                    for (var i = 0; i < arrayLength; i++) {

                                        var obj = result[i];
                                        var obj_lower_properties = {};

                                        for (var prop in obj) {

                                                          //console.log(prop.toLowerCase())
                                                          //console.log(obj[prop])

                                                          obj_lower_properties[prop.toLowerCase()] = obj[prop]
                                        }// for

                                        result_lower_properties.push(obj_lower_properties)

                                    }// for

                                  //----------  ENd -------------- properties to lowercase ----------------------





                                 // must use self.user,  do not use this.user, 
                                 // because here, this's scope is just the function (result).   
                                 // we need this reference to vue-app, 
                                 self.user = result_lower_properties  // [{}, {}, {}]  


                                 // hide v-data-table progress bar
                                self.progress_bar_loading = false

    }); // fetch(){}


   console.log(JSON.stringify(this.user));





},  // initialize {}

full source code is here:

template

js

hoogw
  • 4,982
  • 1
  • 37
  • 33
2

You can use watcher to keep watching on your data array so that after it is populated you can hide the progress bar.

Template:

<v-progress-linear v-show="isLoading"></v-progress-linear>

Script:

data: () => ({
   isLoading: true,
   data:[]
}),
watch() {
   data(){
      this.isLoading = false
   }
}

Check demo on Codepen

https://codepen.io/Agusto/pen/XoqLvb

Augustine
  • 21
  • 1
  • 2
2

This can all be done with another template in your <v-data-table>. You can wrap the <v-progress-linear> within a <template slot="no-data">. Then when your items array gets populated, the no-data template will be hidden, along with everything within it.

Template:

<template slot='no-data'>
    <v-progress-linear slot='progress' indeterminate></v-progress-linear>
</template>

If you want to get really fancy, you can add a <v-alert> in the template under the <v-progress-linear> as well to let your users know what they are waiting for. :-)

<v-alert :value="true" color="info" icon="warning" class="mb-4">Loading data...</v-alert>
mitty1527
  • 21
  • 1
1

Codepen <- wait 5 seconds and you see the progress-bar vanish.

Template:

<v-progress-linear v-show="isLoading"></v-progress-linear>

Script:

data: () => ({
  isLoading: true
}),
mounted() {
  setTimeout(() => {
    this.isLoading = false;
  }, 5000); // toggle after 5 seconds
}

So basically just set v-show on the component and toggle it whenever you wanna hide/show the component. Set it to false after the API call (inside then method callback for example).

I only used 5 seconds (5000ms) timeout as an example so that you can notice the change. Of course you can change it as you see fit.

Example to wait 5 seconds after the API response would look something like the following:

axios.get(this.endpoint).then(response => {
  this.parcels = response.data;
  setTimeout(() => {
    this.isLoading = false;
  }, 5000);
});
Traxo
  • 18,464
  • 4
  • 75
  • 87