0

I'm creating a tree view of the amount of forms employees either haven't filled out, have started or completed. There can be multiple nested levels.

For a simple example the rows are:

Year 2014: missing:3, started=7, completed=7

John Doe: missing=2, started=3, completed=5

Phil Smith: missing=1, started=4, completed=2

What if I filter on employee? Then I want the missing, started and completed count to change for the year row. How do I have those variables be dynamically calculated with an ng-repeat?

1
  • 1
    I think your question is going to need a bit more work. I read it a couple of times and was unable to understand it. I think you will have to describe your thinking in more concrete terms of the data structures in play, what your tree looks like and ideally some examples of what you are trying to achieve. Also, because this site is about programming questions ... you'll probably want to describe what you tried and in that context, what is challenging you. Commented Nov 11, 2014 at 4:14

1 Answer 1

1

You would want to calculate the subtotals in the controller. Your problem is that you want the subtotals to update according to the filters that are applied on view. You can achieve this by injecting $filter into your controller.

Have a look at the plunk here: http://plnkr.co/edit/JwUOzm5u3G7379I1W6hk?p=preview

I created a filter in an input box. Try changing the text, the subtotals update accordingly. In this case I use the 'filter' filter both in the view as well as the controller so that the view and the subtotal update simultaneously.

Code below:

<!DOCTYPE html>
<html>
 <head>
    <script data-require="angular.js@*" data-semver="1.3.1" src="//code.angularjs.org/1.3.1/angular.js"></script>
    <link rel="stylesheet" href="style.css" />

    <script src="script.js"></script>
  </head>

  <body ng-app="app">


    <div ng-controller="mainCtrl as main">
      <input type="text" ng-model="main.empName" placeholder="name filter">
      <div ng-repeat="year in main.data">
      {{year.yearNum}}:{{main.calculateTotal(year.yearNum)}}
        <div ng-repeat="employee in year.employees|filter:main.empName">
          {{employee.name}} : {{employee.cost}}
        </div>
      </div>
    </div>



    <script>
    angular.module('app',[])
      .controller('mainCtrl',function($filter){
      var main=this;

      main.calculateTotal=function(yearNum){
        var data=main.data;
        var total=0;
        for(var i=0;i<data.length;i++){
          if(data[i].yearNum==yearNum)
          {
            var employees=$filter('filter')( main.data[i].employees, main.empName );
            for(var j=0;j<employees.length;j++){
              total+=employees[j].cost; 
            }
          }
        }
        return total;
      }


      main.data=[
        {
          yearNum:2012,
          employees:[
          {
            name:"jane",
            cost:200
          },
          {
            name:"jow",
            cost:400
          }
          ]
        },
        {
          yearNum:2013,
          employees:[
          {
            name:"jane",
            cost:250
          },
          {
            name:"jow",
            cost:450
          }
          ]
        }

        ];
    });
  </script>
  </body>

</html>
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.