0

I have below object that I want to display as per below template. Can anyone please guide me in this? I want it to be optimized simple and quick.

Thanks in Advance.

Object:

var dataSource =  [
    {
        Id: 5,
        CriteriaName: 'First Criteria',
        CustomerName: 'Customer-1',
        Points: 350, Total: 500
    },
    {
        Id: 5,
        CriteriaName: 'First Criteria',
        CustomerName: 'Customer-2',
        Points: 250,
        Total: 500
    },
    {
        Id: 5,
        CriteriaName: 'First Criteria',
        CustomerName: 'Customer-3',
        Points:150,
        Total: 500
    },
    {
        Id: 5,
        CriteriaName: 'Second Criteria',
        CustomerName: 'Customer-1',
        Points:400,
        Total: 450
    },
    {
        Id: 5,
        CriteriaName: 'Second Criteria',
        CustomerName: 'Customer-2',
        Points: 300,
        Total: 450
    },
    {
        Id: 5,
        CriteriaName: 'Second Criteria',
        CustomerName: 'Customer-3',
        Points:200,
        Total: 450
    }
];

And desired output would be:

jsfiddle:

https://jsfiddle.net/7L79dryr/1/

4
  • so the header is static always 4 heards is it ? (total , cutomer-1 , cutomer-2 , cutomer-3 ) ? Commented Jun 30, 2016 at 2:00
  • no it will be distinct CustomerName, and Total Commented Jun 30, 2016 at 2:14
  • It seems need to group by CriteriaName. Commented Jun 30, 2016 at 2:29
  • please consider we will have customerid too Commented Jun 30, 2016 at 3:09

1 Answer 1

2

Need to map this to a different data structure as well as an array of customers

 <table class='tbl'>
   <thead>
     <tr>
       <th></th>
       <th>total</th>
       <th ng-repeat="customer in customers">{{customer}}</th>          
     </tr>
   </thead>
   <tbody>
     <tr ng-repeat='(name, data) in criteria'>
       <td>{{name}}</td>
       <td>{{data.Total}}</td>
       <td ng-repeat="customer in customers">
           {{data[customer] ? data[customer]  : 'N/A' }}
       </td>
     </tr>
     </tbody>
   </table>

JS

var customers = {},
    criteria = {};

data.forEach(function(item){
   customers[item.CustomerName]=true;
   if(!criteria[item.CriteriaName]){   
      criteria[item.CriteriaName]= {Total:item.Total}
   }
   criteria[item.CriteriaName][item.CustomerName]  = item.Points;
});

$scope.customers = Object.keys(customers).sort();
$scope.criteria = criteria;

Not sure how the Total should work , I just used the first available one

DEMO

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

4 Comments

doesn't work for me, cause I also want to post data to the server
What difference does that make?
object doest have TotalPoints column
I answered what was asked in question and also provided a demo that matched expected display results. if there is more then question needs to have more details added to it. Otherwise you should accept this and modify it to your needs

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.