Generating a container DIV with loops and ng-class in AngularJS

Currently, I am working on developing a dynamic timeline using AngularJS. To populate my timeline, I have successfully configured the data fetching from a JSON file. You can see what I have accomplished so far on PLNKR: http://plnkr.co/edit/avRkVJNJMs4Ig5mDrwJ8

The navigation structure I am currently using is:

<nav id="sticky-navigation">
      <ul>
          <li><a href="#decade-1960s">1960</a></li>
          <li><a href="#decade-1970s">1970</a></li>
          <li><a href="#decade-1980s">1980</a></li>
          <li><a href="#decade-1990s">1990</a></li>
          <li><a href="#decade-2000s">2000</a></li>
          <li class="active"><a href="#decade-2010s">2010-Now</a></li>
      </ul>
  </nav>

I aim to create wrapper divs for each unique ID like "decade-1960s", "decade-1970s", etc., allowing users to easily navigate to their desired decade using the provided navigation links. As the date information is extracted from the JSON file, I am exploring ways to extract the year from the JSON, categorize events by decades, and dynamically add a wrapper for each decade.

Thank you for your assistance! I understand it may seem complex, but as a front-end developer and designer, I prefer focusing on the visual aspects rather than backend programming.

Answer №1

Let's take a look at the main concept:

This process is akin to organizing data into pages based on specific criteria, much like paging. By extracting the decade from all your records (events) and grouping them accordingly (one page per decade), you can streamline the functionality of your repeater using the eventsByDecade array in AngularJS.

Below are some important pointers to follow:

Once you have populated your events variable with data from a JSON file, create an array and populate it based on the event dates. To achieve this, you will need to extract the year information using event.theDate.getFullYear() for each event entry.

Following that, determine the respective decade based on the year by possibly utilizing year.substring(0,2) (consider only the first 3 digits).

Subsequently, organize the events into groups by decade within an array and then allocate these decade arrays into a primary eventsByDecade array.

To finalize, modify your repeater setup from:

ng-repeat="event in events | filter:query | orderBy:orderProp:true"

to utilize your "paged" array as shown below:

ng-repeat="event in eventsByDecade[currentIndex] | filter:query | orderBy:orderProp:true"

In this case, ensure to set the value of currentIndex whenever the corresponding links are clicked. For instance:

<li><a href="#decade-1960s" ng-click="currentIndex = 1">1960</a></li>

Update: Working with JSON objects

Remember that JSON evaluates to JavaScript Objects. Hence, you can iterate through your events in the following manner:

for(var event in events){
 event.theDate; //this object should contain properties like the date
}

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

Angular not recognizing draggable true functionality

I am encountering an issue with a div element: <div draggable = "true" ng-show="showPullDown" class="topPull stretch" draggable > </div> The draggable=true attribute is not functioning as expected. I have attempted to set it through the co ...

Click events are unresponsive when used within ng-repeat

Having trouble with ng-click inside of ng-repeat. It seems to work fine outside of it. Check out this JSFiddle link <div ng-controller="MyCtrl"> <a ng-click="triggerTitle='This works!'">test</a> <h5>Please select tri ...

Attempting to minimize the number of jQuery calls within a Domino XPage

Trying to incorporate the 'DataTables' table plug-in for jQuery into a basic Domino XPage. Successfully loaded required libraries from CDN sources... JQuery: ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js DataTables: cdn.datatables. ...

Utilize the identical ReactJS hook across multiple components

I've created a hook that pulls data from an API and stores the result in my component's state using setAllCommunitiesFromSponsor. Now I want to use this same hook in another component. Instead of copying and pasting, what is the best way to imp ...

Struggling to iterate through the children of a variable with the help of express-handlebars?

Currently, I am in the process of developing an application using Javascript along with express, express-handlebars, and MySQL. One of my main tasks is to establish a route that follows the pattern '/viewowner/:ID/scoreboards/:year' where ID sig ...

Looking for non-case-sensitive letters in a text input field

Having trouble with case-insensitive search using jquery? The search option seems to be working fine, but there's an issue with uppercase and lowercase letters in the content. Check out my full code on jsfiddle where if I search for "senthil" it doesn ...

Tips for presenting retrieved HTML unchanged within an Angular 2 template

Currently, I am fetching post data from a remote server. The content of the posts includes HTML with style and class attributes which are generated by a WYSIWYG editor. My goal is to display the HTML data as it is, without any filtering or sanitization. ...

navigation bar directing to the wrong destination

My landing page has attached pages like landing/gothere and /another. The issue arises when I try to navigate from /another to landing/gothere. I fetch landing information using an API, but there's a delay when I click on the navbar link. The page loa ...

Stop Ajax requests when there are blank spaces in Typeahead.js

I've been experimenting with typeahead.js and utilizing the BloodHound remote feature to load data. Everything is functioning properly, except that when I input only spaces in the textbox, typeahead still makes an ajax call. I'm wondering if th ...

I have the ability to see HTTP-only cookies within my web browser

So, I always believed that httpOnly cookies could only be accessed during a http request. But the other day, while inspecting Firefox dev tools, I noticed that I could actually view the cookies' values. Is this standard behavior? ...

Is there a way to modify my function expression without using any state variables?

Currently working on a jQuery game where the player is generated upon clicking a button. Below is the function expression I'm using to store the player's information: var getPlayerData = function() { return { name: $("#name").val(),/ ...

Execute the function numerous times that is associated with an asynchronous function in JavaScript

I am currently working on two functions: one is asynchronous as it fetches data from a CSV file, and the other function renders a list of cities. The CSV file contains information about shops located in various cities. My goal is to display a list of cit ...

"Obtaining a three.js sprite within a Verold script - the ultimate guide

Greetings fellow users of stack overflow! I've recently been experimenting with the world editor known as verold, based on three.js. The features it offers are quite impressive, but I've encountered an issue with the scripting aspect. My curren ...

Creating a Paytm payment link using API in a React Native app without the need for a server

Suppose a user enters all their details and the total cost of their order amounts to 15000 rupees. In that case, the app should generate a Paytm payment link for this amount and automatically open it in a web view for easy payment processing. Any suggesti ...

Having trouble displaying the API response data on the screen in Next.js

I am currently experiencing an issue with my OCR API that is supposed to return text from a given image. The data is being received on the client side and can be seen in the console. However, for some reason, the element is not updating with the data. Bel ...

Navigating to a different page in the app following a document update: a step-by-step guide

I am facing an issue with a page where I am trying to print a specific DIV using the script below... function printReceiptDiv() { var divElements; if (vm.isDLR) { divElements = document.getElementById("DLRreportCont ...

In Next.js, a peculiar issue arises when getServerSideProps receives a query stringified object that appears as "[Object object]"

Summary: query: { token: '[object Object]' }, params: { token: '[object Object]' } The folder structure of my pages is as follows: +---catalog | | index.tsx | | products.tsx | | | \---[slug] | index.tsx | ...

What is the best way to showcase Vue data of a selected element from a v-for loop in a

Here is an example of how my json data is structured. I have multiple elements being displayed in a v-for loop, and when one is clicked, I want to show specific information from that element in a modal. [{ "id": 1, "companyNa ...

What is returned by jQuery when it fails to locate a specified class selector within the jQuery object?

Picture this scenario: var $letsTestA = $( '.lets-test-a' ), $letsTestB = $( '.lets-test-b' ); Then, consider this HTML: <div class="lets-test-a"></div> (I omitted .lets-test-b intentionally) Now, what happens if we ...

JavaScript Function for Finding the Time Difference Between Two Dates (in Years, Days, Hours, or Less than One Hour)

I need to calculate the time difference between 2 dates and display it. If the difference is greater than a year, show the number of years only. If it's more than a day, show the number of days. If it's less than a day, show the number of hours. ...