0

using Vuejs/Laravel .I use this simple javascript code to generate a chart in blade.

 <script>
  function getData(columnOrder, keyName) {
  var obj, table = $("#t4"), array = [];
  table.find('tbody tr').each(function() {
  var rows = $(this).find('td:nth-child(' + columnOrder +')');
  rows.each(function(){
    obj = {};
    obj[keyName] = $(this).text();
    array.push(obj);
  });
  });
  return array;
  }

  var categories = getData(1, 'label');
  var datas = getData(2, 'value');
  console.log(categories);
  console.log(datas);

It works when my html table contain html/php values. but when it's vuejs values, it's seems not recognizing data.. Here is my table with vuejs.

 <table class="hidden" id="t4">
 <thead>
 <tr>
   <th scope="col">a</th>
   <th scope="col">b</th>
 </tr>
 </thead>
 <tbody>
 <template v-for="person in personsWithAmount">
 <tr>
   <td scope="row" >@{{person.user.name}}</td>
   <td scope="row">@{{person.totalAmount}}</td>
 </tr> 
 </template>      
 </tbody>
 </table>

Console.log still blank Thank you in advance

3
  • 1
    I suspect your code is running before Vue does. You'd want to move your JS into the Vue component, so it only runs once the data etc. is all present and mounted. Commented Dec 28, 2018 at 21:49
  • Oh yes, u were right ! Thank you ! Commented Dec 28, 2018 at 21:59
  • Cool - I've fleshed this out into an answer a little. Commented Dec 28, 2018 at 22:01

1 Answer 1

1

You'll want to take a peek at the Vue lifecycle. In short, Vue takes a while to load and initialize, which means that JavaScript outside of Vue that relies on elements created by Vue may execute before those elements exist at all.

Generally, you're going to want to move any JS code that relies on Vue-created elements into the Vue app.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.