简体   繁体   English

如何用可能包含空单元格的JSON数据填充表(AngularJS)

[英]How to fill table with JSON data that may contain empty cells (AngularJS)

I have the following JSON data that contains the names and salaries of employees across a number of days (from 1 to 5). 我有以下JSON数据,其中包含几天(从1到5天)内员工的姓名和工资。 What I would like to do is place this data within a HTML table using AngularJS so it looks like the required table shown below. 我想做的是使用AngularJS将这些数据放在HTML表中,使其看起来像下面所示的必需表。

JSON Data: JSON数据:

[{
        name: "John",
        salaryBreakdown: [{
                day: 1,
                salary: 32
            }, {
                day: 2,
                salary: 40
            }, {
                day: 5,
                salary: 105
            }
        ]
    }, {
        name: "Nick",
        salaryBreakdown: [{
                day: 1,
                salary: 27
            }, {
                day: 4,
                salary: 82
            }
        ]
    }, {
        name: "Bob",
        salaryBreakdown: [{
                day: 2,
                salary: 55
            }, {
                day: 3,
                salary: 80
            }
        ]
    }
]

Required Table: 必填表:

Name     Day1     Day2     Day3     Day4     Day5
-------------------------------------------------
John       32       40        -        -      105
Nick       27        -        -       82        -
Bob         -       55       80        -        -

The issue I'm facing now is that there are cells within the table that will be empty as shown above (eg days 3 and 4 for John). 我现在面临的问题是表中的单元格将是空的,如上所示(例如,John的第3天和第4天)。 Now I could solve this by including an object for each day within the salaryBreakdown array for every employee, eg for John: 现在,我可以通过在每个员工(例如,对于约翰)的salaryBreakdown数组中包括每天的对象来解决此问题:

salaryBreakdown: [{day: 1, salary: 32}, {day: 2, salary: 40}, {day: 3, salary: 0}, {day: 4, salary: 0}, {day: 5, salary: 105}]

Unfortunately, this would require additional coding to create a lot of unnecessary data that would either be stored in a database or generated for each request simply for visualization purposes. 不幸的是,这将需要额外的编码来创建大量不必要的数据,这些数据要么存储在数据库中,要么仅出于可视化目的针对每个请求生成。 I am trying to find a way so that I can create the required view without altering the JSON data. 我正在尝试找到一种方法,以便可以在不更改JSON数据的情况下创建所需的视图。 The HTML code I have at the moment is shown below, although I understand how it works and why it doesn't exactly meet my requirements. 我目前拥有的HTML代码如下所示,尽管我了解它的工作方式以及为什么它不能完全满足我的要求。

Is there another way using AngularJS to achieve the required table view? 使用AngularJS还有另一种方法来实现所需的表视图吗?

Current HTML (incorrect): 当前HTML(不正确):

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Day1</th>
            <th>Day2</th>
            <th>Day3</th>
            <th>Day4</th>
            <th>Day5</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="employee in jsonData">
            <td ng-bind="employee.name"></td>
            <td ng-repeat="item in employee.salaryBreakdown | orderBy: 'day'" ng-bind="item.salary"></td>
        </tr>
    </tbody>
</table>

Current Table (incorrect): 当前表(不正确):

Name     Day1     Day2     Day3     Day4     Day5
-------------------------------------------------
John       32       40      105
Nick       27       82
Bob        55       80

Could flatten the salary arrays into single objects with days as keys and iterate over an array of day numbers and use each day to look upthat person's salary 可以将以天为键的薪水数组展平为单个对象,并遍历天数数组并每天使用该人来查询该人的薪水

 angular .module('app', []) .controller('myCtrl', function($scope) { $scope.data = angular.copy(data); $scope.data.forEach(o => o.salaryBreakdown = o.salaryBreakdown.reduce((a, c) => { a[c.day] = c; return a }, {})) $scope.days = [1,2,3,4,5] }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.min.js"></script> <table ng-app="app" ng-controller="myCtrl"> <thead> <tr> <th>Name</th> <th>Day1</th> <th>Day2</th> <th>Day3</th> <th>Day4</th> <th>Day5</th> </tr> </thead> <tbody> <tr ng-repeat="employee in data"> <td ng-bind="employee.name"></td> <td ng-repeat="day in days" ng-bind="employee.salaryBreakdown[day].salary ||0 "></td> </tr> </tbody> </table> <script> var data =[{ name: "John ", salaryBreakdown: [{ day: 1, salary: 32 }, { day: 2, salary: 40 }, { day: 5, salary: 105 } ] }, { name: "Nick ", salaryBreakdown: [{ day: 1, salary: 27 }, { day: 4, salary: 82 } ] }, { name: "Bob ", salaryBreakdown: [{ day: 2, salary: 55 }, { day: 3, salary: 80 } ] } ] </script> 

You could create a new array with blank objects inserted like this: 您可以使用以下插入的空白对象创建一个新数组:

data.forEach(person => {
  person.salaryBreakdownTransformed = [];
  for (let i = 1; i < 6; i++) {
    let bd = person.salaryBreakdown.find(b => {
      return b.day === i;
    }) || {"day": i, "salary": 0};
    person.salaryBreakdownTransformed.push(
      bd
    );
  }
});

Here's a fiddle to demonstrate it: https://jsfiddle.net/L1184yqu/16/ 这是一个小提琴来演示它: https : //jsfiddle.net/L1184yqu/16/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM