3

I have a table that could potentially be populated with any number of rows (e.g. 3, 3800, etc.), so before the data is populated, I want to apply an overlay (I already have a function applyOverlay() for that). Here's my HTML:

<table id="table" class="datatable" style="width:100%">
    <thead>
        /* table header */
    </thead>
    <tbody>
        <tr v-for="(item, index) in items" item="item">
            <td v-text="item.Name"></td>
            /* more <td> */
        </tr>
    </tbody>
</table>

And here's my JavaScript:

var tblItems = [ /* items */ ];

var app = new Vue({ 
    el: '#table',
    data: {
        items: tblItems
    }
});

I tried using jQuery's $(document).ready, but I see that the page gets loaded (table not populated yet), and my overlay isn't applied until seconds later (and the table is populated). I want to have the overlay applied as soon as the html is done loading. I read about their lifecycle but I'm not sure which hook I should use. My applyOverlay() is vanilla JavaScript and doesn't depend on Vue.

EDIT: I've tried using mounted, beforeMount, created, and beforeCreate, and nothing has worked. I wonder if it has anything to do with jQuery. But, my jQuery does load first before loading Vue.

tony19
  • 125,647
  • 18
  • 229
  • 307
Darren
  • 223
  • 2
  • 17
  • 1
    Possible duplicate of [How to call a vue.js function on page load](http://stackoverflow.com/questions/40714319/how-to-call-a-vue-js-function-on-page-load) – Saurabh Mar 22 '17 at 15:21
  • Why would you not simply use DOMContentLoaded? – connexo Jul 14 '22 at 00:20

3 Answers3

1

The function applyOverlay() can be added to the methods property of the Vue object. Then use a lifecycle hook like beforeMount to call that method. For a more thorough explanation of the available hooks, Saurabh has a good list here.

var app = new Vue({ 
    el: '#table',
    data: {
        items: tblItems
    },
    methods: {
        applyOverlay: function() {
             //code for applying overlay
        }
    }
    beforeMount: function() {
        this.applyOverlay();
    }
});

See this demonstrated below. The example uses HTML class bindings to add the overlay.

var tblItems = [ /* items */ ];

var app = new Vue({
  el: '#table',
  data: {
    items: tblItems,
    status: '',
    overlay: false
  },
  beforeMount() {
    this.applyOverlay();
    setTimeout(this.setUnits, 1500);
  },
  methods: {
    applyOverlay: function() {
      this.overlay = true;
      this.status = "overlay applied";
    },
    setUnits: function() {
      this.overlay = false;
      this.items = [{
        Name: "A",
        status: "done"
      }, {
        Name: "B",
        status: "in transit"
      }];
      this.status = 'set items, overlay removed';
    }
  }
});
.datatable {
  text-align: center;
}

tfoot {
  font-size: small;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  /*dim the background*/
  background-color: rgba(0, 0, 0, 0.5);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.4/vue.min.js"></script>
<table id="table" class="datatable" style="width:100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Status</th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="(item, index) in items" item="item">
      <td v-text="item.Name"></td>
      <td v-text="item.status"></td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td colspan="2">Status: {{ status }}
        <div v-bind:class="{overlay: overlay}"></div>
      </td>
    </tr>
  </tfoot>
</table>
tony19
  • 125,647
  • 18
  • 229
  • 307
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
0

You can use created, mounted life-cycle hook or beforeMount hook, like following:

var app = new Vue({ 
    el: '#table',
    data: {
        items: tblItems
    },
    mounted () {
      // Your code needed to be executed on page load
    }
});
tony19
  • 125,647
  • 18
  • 229
  • 307
Saurabh
  • 71,488
  • 40
  • 181
  • 244
0

I think you are looking for "mounted" :

var tblItems = [ /* items */ ];

var app = new Vue({ 
    el: '#table',
    data: {
        items: tblItems
    },
    mounted: function() {
        //init table here because table template is available
    }
});

enter image description here

Happyriri
  • 4,295
  • 1
  • 17
  • 28