【问题标题】:Organizing objects hierarchically by two levels in angularjs在angularjs中按两个级别分层组织对象
【发布时间】:2016-07-05 00:54:16
【问题描述】:

我正在开发一个 Angular 应用程序,该应用程序在 Angular Bootstrap 日历中使用 cell modifier 内的 custom cell template。在每个单元格内,而不是标准事件,我放置了一组表格,用于注册当天在车站的轮班。这些表分为两组; am 和 pm,并且在每个 am/pm 组内,每个站点都有一个表,每个表中包含三行。

上午

| 1 |职位|姓名|
| |职位 1 |姓名 1|
| |职位 2 |姓名 2|
| |职位 3 |姓名 3|

| 2 |职位|姓名|
| |职位 1 |姓名 1|
| |职位 2 |姓名 2|
| |职位 3 |姓名 3|

下午

| 1 |职位|姓名|
| |职位 1 |姓名 1|
| |职位 2 |姓名 2|
| |职位 3 |姓名 3|

| 2 |职位|姓名|
| |职位 1 |姓名 1|
| |职位 2 |姓名 2|
| |职位 3 |姓名 3|

在我的单元格修改器函数中,我得到了当天的班次对象数组,每个班次对象都包含 ampm 值和 station 值:

 {
    "_id": "57776537ac0a88010063b9b9",
    "modified": "2016-07-02T06:54:47.518Z",
    "data": {
      "station": "1",
      "date": "2016-07-01T07:00:00.000Z",
      "ampm": "pm",
      "slots": [
        {
          "position": "AO",
          "name": ""
        },
        {
          "position": "FF",
          "name": {
            "_id": "57776507ac0a88010063b9b8",
            "modified": "2016-07-02T06:53:59.661Z",
            "data": {
              "group": "suppression",
              "driving": {
                "n": false,
                "d": true,
                "ao": false,
                "wt": false
              },
              "emtLevel": "b",
              "secondaryPhoneNumber": "",
              "primaryPhoneNumber": "5556781234",
              "emailAddress": "person.one@mysite.com",
              "fullName": "Person One",
              "userName": "person.one",
              "assignedStation": "18",
              "probationary": false
            },
            "form": "57427ba554ec330100dad645",
            "created": "2016-07-02T06:53:59.644Z",
            "externalIds": [],
            "access": [],
            "roles": [
              "573511a8ffaa7a0100a5718a"
            ],
            "owner": "57776507ac0a88010063b9b8"
          }
        },
        {
          "position": "FF",
          "name": {
            "_id": "57439d856e67b40100d4c420",
            "modified": "2016-05-24T00:17:09.493Z",
            "data": {
              "userName": "person.two",
              "fullName": "Person Two",
              "emailAddress": "person.two@mysite.com",
              "primaryPhoneNumber": "5555556666",
              "secondaryPhoneNumber": "",
              "assignedStation": "",
              "emtLevel": "b",
              "driving": {
                "d": true
              },
              "group": "suppression"
            },
            "form": "57427ba554ec330100dad645",
            "created": "2016-05-24T00:17:09.474Z",
            "externalIds": [],
            "access": [],
            "roles": [
              "573511a8ffaa7a0100a5718a"
            ],
            "owner": "5734bba2ffaa7a0100a57029"
          }
        }
      ]
    },

所以问题是如何获取这些对象并将它们组织到上面提到的两个分组中,以便我可以在我的模板中使用 ngRepeat 循环它们。我到目前为止是这样的:

vm.cellModifier = function(cell) {
  cell.text = 'Test Text';
  var shifts = vm.events;
  // Get the date for the cell.
  this.cellDate = moment(cell.date).format('YYYY-MM-DD');
 // Iterate over shifts to get ones for this day.
  this.cell = cell;
  this.todayShifts = {};
  shifts.forEach(function(shift, index, allShifts) {
    var shiftDate = moment(shift.data.date).format('YYYY-MM-DD');
    // Now we need to see if this shift belongs to this day.
    if (moment(vm.cellDate).isSame(moment(shiftDate))) {
      // Shift is today, so let's put it into the appropriate array.
      if (typeof vm.todayShifts[shift.data.ampm] == 'undefined') {
        vm.todayShifts[shift.data.ampm] = shift;
      } else {
        vm.todayShifts[shift.data.ampm].push(shift);
      }
    }
  });
  // Add arrays to cell object.
  cell.todayShifts = vm.todayShifts;
};

这给了vm.todayShifts[am]vm.todayShifts[pm],但我也想获得第二级,这样我就有vm.todayShifts[am][1]vm.todayShifts[am][2]等。有没有更简单的方法来做我想做的事情(我相当肯定有)而不是添加另一段语句?我想知道自定义指令或组件是否更干净,因为这样我就可以将我的数据传递到该控制器中,但即便如此,我仍然需要正确安排我的数据,以便它可以按正确的顺序显示。

希望这一切都有意义。

谢谢。

【问题讨论】:

    标签: javascript arrays angularjs multidimensional-array


    【解决方案1】:

    我认为您实际上非常接近...试试这个,它将始终以您要求的格式放置数据。

    shifts.forEach(function(shift, index, allShifts) {
      var shiftDate = moment(shift.data.date).format('YYYY-MM-DD');
      // Now we need to see if this shift belongs to this day.
      if (moment(vm.cellDate).isSame(moment(shiftDate))) {
        // Shift is today, so let's put it into the appropriate array.
        if (typeof vm.todayShifts[shift.data.ampm] == 'undefined') {
    
          // Initialize the shifts as an array.
          vm.todayShifts[shift.data.ampm] = [];
        }
    
        // Push the shift onto the array.
        vm.todayShifts[shift.data.ampm].push(shift);
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多