在angularjs

时间:2016-07-05 00:54:17

标签: javascript arrays angularjs multidimensional-array

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

AM

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

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

PM

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

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

在我的单元格修改器函数中,我得到当天的一组移位对象,每个移位对象包含ampm值和工作站值:

 {
    "_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]等。有更简单的方法吗?做我正在尝试做的事情(我相当确定),而不是添加另一部分语句?我想知道自定义指令或组件是否更干净,因为那时我可以将我的数据传递到该控制器,但即便如此,我仍然需要正确安排我的数据,以便它能以正确的顺序显示。

希望这一切都有道理。

感谢。

1 个答案:

答案 0 :(得分: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);
  }
});