Implementing a dual-level hierarchical organization of objects in AngularJS

I am currently developing an Angular application that integrates a cell modifier within a custom cell template in Angular Bootstrap Calendar. Instead of traditional event displays within each cell, I am incorporating sets of tables used for shift sign-ups at specific stations for the day. These tables are categorized into AM and PM groups, each containing multiple station-specific tables with three rows in each.

AM

| 1 |Position|Name |
| |Pos 1 |Name 1|
| |Pos 2 |Name 2|
| |Pos 3 |Name 3|

| 2 |Position|Name |
| |Pos 1 |Name 1|
| |Pos 2 |Name 2|
| |Pos 3 |Name 3|

PM

| 1 |Position|Name |
| |Pos 1 |Name 1|
| |Pos 2 |Name 2|
| |Pos 3 |Name 3|

| 2 |Position|Name |
| |Pos 1 |Name 1|
| |Pos 2 |Name 2|
| |Pos 3 |Name 3|

Within my cell modifier function, I receive an array of shift objects for the day, where each object contains the ampm value and station value:

 {
    "_id": "57776537ac0a88010063b9b9",
    "modified": "2016-07-02T06:54:47.518Z",
    "data": {
      "station": "1",
      "date": "2016-07-01T07:00:00.000Z",
      "ampm": "pm",
      "slots": [
        // Shifts data here
      ]
    },

The challenge lies in organizing these objects into the two mentioned groupings so they can be easily looped through with ngRepeat in the template. Currently, my approach involves:

vm.cellModifier = function(cell) {
  cell.text = 'Test Text';
  var shifts = vm.events;
  this.cellDate = moment(cell.date).format('YYYY-MM-DD');
  
  // Iterating over shifts to get those for the specific date.
  this.cell = cell;
  this.todayShifts = {};
  
  shifts.forEach(function(shift, index, allShifts) {
    var shiftDate = moment(shift.data.date).format('YYYY-MM-DD');
    
    if (moment(vm.cellDate).isSame(moment(shiftDate))) {
     
      if (typeof vm.todayShifts[shift.data.ampm] == 'undefined') {
        vm.todayShifts[shift.data.ampm] = shift;
      } else {
        vm.todayShifts[shift.data.ampm].push(shift);
      }
    }
  });
  
  cell.todayShifts = vm.todayShifts;
};

This method generates vm.todayShifts[am] and vm.todayShifts[pm], but I am also looking to implement a secondary level like vm.todayShifts[am][1], vm.todayShifts[am][2], etc. Is there a more efficient way to accomplish this rather than adding more statements? Perhaps utilizing a custom directive or component could offer a cleaner solution, enabling easy data-passing to the controller while ensuring correct arrangement for display.

I hope this explanation clarifies the scenario. Thank you.

Answer №1

It seems like you're getting pretty close to the solution... Consider trying this approach to always format the data as requested.

shifts.forEach(function(shift, index, allShifts) {
  var shiftDate = moment(shift.data.date).format('YYYY-MM-DD');
  // Check if the shift belongs to the specified day.
  if (moment(vm.cellDate).isSame(moment(shiftDate))) {
    // Put the shift into the appropriate array if it is for today.
    if (typeof vm.todayShifts[shift.data.ampm] == 'undefined') {

      // Initialize an array for shifts.
      vm.todayShifts[shift.data.ampm] = [];
    }

    // Add the shift to the array.
    vm.todayShifts[shift.data.ampm].push(shift);
  }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Setting up webpack for React to utilize multiple entry points and outputs

Having some trouble configuring the server to handle multiple entries and outputs. The application utilizes Zurb Foundation, jQuery, and React. I'm aiming to exclude jQuery and foundation from the bundle.js file, while also creating a separate bundle ...

The callback function in AngularJS filters

I'm currently using an AngularJS filter to sort through a list of items. Here is the Jade markup I am using: li(ng-repeat="parcel in parcels | filter : filterActiveAreaParcels") After the filter function runs and the elements are displayed in the DO ...

Personalize your material-ui popover

Seeking assistance in customizing the shape of a material-ui popover similar to the one depicted in the image. https://i.sstatic.net/l5uNL.png I have created a working demo of the popover using React and provided a link for editing purposes. Any help? =& ...

Troubleshooting issue with Bootstrap collapse functionality failing with dynamic IDs

Having trouble creating dynamic ids for bootstrap collapsing functionality. I want each topic in an ng-repeat to collapse and display its respective question list when clicked. The issue is that when I click on a second topic, the question list data from ...

Locate a string containing a series of words separated by a character, with the last word being able to end with any combination of characters through the use of regex

Here are some words to consider: const words = ["apple", "orange", "tomato"] const str = "apple.orange.tomato.$COULD_$_BE_ANY_STRING_HERE" I am in search of a regular expression to verify the format of this string. ...

The HTML iframe is displaying blank content

I'm trying to embed a webpage within another webpage using an iframe. I attempted to do so with this simple code: <iframe src="http://edition.cnn.com/" id="i_frame"></iframe> JSFIDDLE However, nothing is displaying. Any thoughts on why ...

Typehead.js on Twitter is displaying the full query instead of just the value

The Problem This is the issue I am facing with my code. My goal is to retrieve only the value, but instead of that, the entire query value is being returned. var engine; engine = new Bloodhound({ local: [{value: 'red'}, {value: 'blue&apo ...

Storing data in a table created through a belongsToMany relationship in Sequelize and retrieving it. (Solution provided below)

My backend setup includes Node.js, Express.js, and Sequelize for database connections. I have established a many-to-many relationship between Tasks and Keys. I defined the connection between Tasks and Keys using Sequelize as follows: Backend // Task ...

What is the best way to include a context in a JavaScript promise?

Figuring out JS Promises has always been a challenge for me. I'm aware this might be a basic question, but please bear with me. =) Looking at the code snippet below, my goal is to modify a property within the class containing this function (essentiall ...

Looking to understand how to effectively utilize the ContentProtectionCallback in SHAKA PLAYER?

Could someone assist me in understanding how to pass the ContentProtectionCallback in order to manage the preProcessor of a drm license url for shaka player? [ var manifestUri = '<mpd url>'; function initApp() { // Including nece ...

"Exploring the versatility of NextJS with dynamic route parameters

Can NextJS be configured to handle dynamic routes that match both /country and /country/city, while excluding matches like /country/city/whatever_content_here? The goal is to display the same content for either of the specified routes, regardless of whethe ...

Leveraging jQuery Ajax and MySQL to generate dynamic HTML content

I'm in the process of creating a unique photo gallery that utilizes dynamic features. Instead of relying on constant HTML/PHP refreshing for updates, I am incorporating jQuery to handle dynamic MYSQL queries. As a beginner, I've managed to creat ...

The Angular.js UIBootstarp Timepicker is displaying the "ng-invalid" class when used with an input type of "number"

Currently, I am working on incorporating a time picker using UIBootstrap and angular.js. By default, the timepicker utilizes {{input type="text}} for capturing hours and minutes. However, since I intend to use this feature on mobile devices, I need to di ...

Encountered an unhandled promise rejection: TypeError - The Form is undefined in Angular 6 error

Whenever I attempt to call one .ts file from another using .Form, I encounter the following error: Uncaught (in promise): TypeError: this.Form is undefined The file that contains the error has imported the .ts file which I intend to pass values to. ...

Contrasting characteristics of indexing and slicing within numpy structured arrays

Imagine you have a structured array a: import numpy as np a = np.array([1, 2, 3, 4, 5, 6], dtype=[('val', 'i4')]) print(a) [(1,) (2,) (3,) (4,) (5,) (6,)] Now, when it comes to changing one of the entries to a different value, there ...

What is the best way to store a personalized configuration for a user within a Node module?

For my CLI project in Node.js utilizing commander.js, I am interested in implementing a way to store user-specific configuration settings. This will allow users to input their preferences only once during the initial usage. What would be the best approac ...

Implementing translation functionality within an AngularJs controller

Utilizing angular translate for translating certain words in the controller, with translation stored in json files within the html. // LABELS var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "Septe ...

Tips for setting up the information in a Bootstrap popover

I am currently working on a Google web app that involves Google Sheets data. My objective is to have the data displayed when hovering over a button, but instead, the data appears directly on the button without the hover display. What might I have done in ...

I possess an array with a specific structure that I wish to substitute the keys as demonstrated below

The data array is currently structured in this way. I have attempted various methods but have not been able to find a suitable solution. I need to send the JSON below via an AJAX request, however, I do not want the keys 0 and 1 in both child arrays. [sch ...

What strategies can I use to reduce duplication in my HTML and JavaScript code?

Struggling with messy HTML and JS code? If you're using Bootstrap-3 and jQuery-1.11.0, dealing with dropdowns might be tricky. Especially when it comes to switching icons based on the dropdown state and ensuring only one dropdown is open at a time. Is ...