Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm trying to sum a computed property (effectively sum a column in a table), but it returns 0 (Total Net - bottom of last column). See fiddle: https://jsfiddle.net/2djreyds/ I think it has to do with how I am referencing the computed property. Sure its something simple! Any ideas? or if there's a smarter way to do this let me know! :) Thanks!

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Vue.js Tutorial | More Computed Properties</title>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  </head>

  <body>
    <div id="app" class="container">
      <div class="row">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Phase</th>
                        <th>Labour Budget</th>
                        <th>Labour Hours</th>
                        <th>Labour Cost Rate</th>
                        <th>Labour Cost</th>
                        <th>Overhead Cost</th>
                        <th>Net</th>
                    </tr>   
                </thead>
                <tbody>
                    <tr is="data-row" v-for="record in records" :record="record"></tr>
                    <tr>
                        <td></td>
                        <td></td>
                        <td>Total Hours: {{totalHours}}</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td class="alert alert-info">Total Net: {{totalNet}}</td>
                    <tr>
                </tbody>
            </table>
      </div>
    </div>
  </body>

  <script src="https://unpkg.com/[email protected]/dist/vue.js"></script>

  <script type="text/x-template" id ="data-row">
    <tr>
        <td>{{record.phase}}</td>
        <td>{{record.labourBudget}}</td>
        <td><input type="number" v-model="record.labourHours"></td>
        <td><input type="number" v-model="record.labourCostRate"></td>
        <td>{{labourCost}}</td>
        <td>{{overheadCost}}</td>
        <td>{{net}}</td>
    <tr>
  </script>

  <script>
    var app = new Vue({
      el: '#app',
      data: {
        records: [
            {phase:"Phase1", labourBudget: 100, labourHours: 4, labourCostRate: 45},
            {phase:"Phase2", labourBudget: 100, labourHours: 2, labourCostRate: 42}
        ]
      },
      components:{
        'data-row':{
            template: "#data-row",
            props: ["record"],
            data:function(){
                return this.record;
            },
            computed:{
                 labourCost: function(){
                    return this.record.labourHours * this.record.labourCostRate;
                },
                overheadCost: function(){
                    return this.labourCost * 1.36;
                },
                net: function(){
                    return this.record.labourBudget-this.labourCost-this.overheadCost;
                }
            }
        }
      },
      computed:{
        totalHours: function(){
            var sum = 0;
            var items = this.records;
            for (var i in items){
                sum += items[i].labourHours;
            }
            return sum;
          },
        totalNet: function(){
            var sum = 0;
            var items = this.$children;
            for (var i in items){
                sum += items[i].overheadCost;
            }
            return sum;
          }
      }
    })
  </script>
</html>

share|improve this question
    
Your problem is that $children isn't reactive. So it's running before it gets populated with data / mounted. – Adam Dec 9 '16 at 20:59
    
Thanks Adam - that points me in the right direction! how would reference/loop through the reactive computed properties of children components? – FirstRedPepper Dec 9 '16 at 21:04
    
NP! :) hhhmmmm... maybe this will help? – Adam Dec 9 '16 at 21:31
1  
Thank you sir! Got it working with the help of that link! – FirstRedPepper Dec 10 '16 at 0:08
1  
For anyone interested - here's the fiddle: jsfiddle.net/mbdz7d4j – FirstRedPepper Dec 10 '16 at 0:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.