Using AngularJS to filter and order items in an ng-repeat loop with multiple variables

I hold a dataset with employment history. There are two fields for the employment dateTo:

yearTo, monthTo

To accurately sort the employment history by the most recent dateTo, I utilize the following code:

<tr id="employment_history_" ng-repeat="history in candidate.employmentHistory | orderBy : ['-yearTo','monthTo',] | date:['YYYY', 'MMM'] ">

The sorting functions as intended. However, there is one dilemma. The field monthTo may include the value Present instead of solely the month date format like JAN, FEB MAR, etc.

My question is how can I prioritize records with the monthTo value of Present to appear first on the list despite having a date format?

Answer №1

If you're looking for a good starting point, check out this JSFiddle that can guide you in the right direction: JSFiddle

The concept behind this approach is to provide an array of predicates to the orderBy directive, as demonstrated in your provided example. While string predicates were used in your example, it's worth noting that function predicates can also be utilized. A function predicate allows you to determine the values used for ordering, based on the item being sorted.

ng-repeat="hist in main.history | orderBy: ['-yearTo', main.orderMonthTo]"

this.orderMonthTo = function(item) {
    if (item.monthTo === 'Present') {
    return -13;
  } else {
    return Number(moment(item.monthTo, 'MMM').format('M'))*-1;
  }
}

For more detailed information on this topic, refer to this resource. Additionally, in the JSFiddle example, the momentjs library was used to simplify date handling.

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

able to move through the content without displaying the scrollbar

I am looking to keep my content able to scroll, but I don't want the scrollbar to be visible. I have written my CSS like this: #content-three{ width:100%; height:100%; position:relative; overflow:hidden; } .block_s{ width:90%; ...

Adjust the alignment and floating of text within an iframe on the same domain

Is it possible to align text on the right side of an iframe that contains a width and text inside? The iframe's src is from the same domain. If so, how can this be achieved through JavaScript? ...

Can you combine multiple user validation rules with express-validator?

I have a set of rules that are almost similar, except for one where the parameter is optional and the other where it is mandatory. I need to consolidate them so that I can interchangeably use a single code for both cases. Is there a way to merge these rul ...

Adjusting canvas height in Storybook - Component does not fit properly due to low canvas height

I had a component that I needed to add to Storybook. It was working fine, but the styling was slightly off. I managed to resolve this by adding inline styling with position: absolute. Here is how it looks now: const Template: any = (args: any): any => ( ...

sending multiple checkbox selections to a PHP script using jQuery

<form id="foo"> <input type="text" name="voucher" placeholder="voucher ID" class="bill_fillter"/> <input type="text" name="voucher" placeholder="voucher ID" class="bill_fillter"/> <input type="text" name="voucher" placeholder="voucher ...

Is a Promise nested within another Promise?

My current task involves retrieving the lat/long of 2 addresses using promises. Once this initial promise is resolved, I need to parse a file that correlates with these lat/long coordinates. Everything works as expected and I can handle the values in the ...

Tips for waiting for an event from a specific element in Playwright

Is there a way to await an event on a specific element using Playwright? Similar to page.waitForEvent, but focusing only on a particular element rather than the entire page. More information can be found in the documentation. ...

Unable to apply CSS styles to a form input using Ajax

Alright, so I've got this Ajax form that successfully fetches the data and displays it in #register_msg. However, I'm also trying to apply some styles to the input forms. Here's my Ajax code, along with the form. But for some reason, the st ...

Is there a way to transmit React code using Express?

Currently, I'm in the process of creating a coloring demonstration. Initially, I had an SVG file at hand, but I made the decision to utilize svgr for its conversion into a React component. This strategy will offer me the flexibility to easily modify i ...

SQL inputs with information that updates automatically / autofill SQL input boxes

Is it feasible to create a webpage that can connect to an SQL database, retrieve information from a table, and display it in a text box based on the input provided in another text box? Essentially, I am looking for a way to enable autofill functionality. I ...

Obtaining user roles from server without using JWT tokens

My main objective is to provide user roles from the backend. For instance, if a user wishes to access resources in my backend, they can log in using Google credentials. The Angular app retrieves the access token from the Google authorization server and s ...

An unusual html element

During a recent exploration of a website's code using the inspect tool, I stumbled upon a tag that was completely unfamiliar to me. <gblockquote></gblockquote> I've come across a blockquote before, but never a gblockquote. Interest ...

Searching for items using angularFireCollection?

When retrieving data objects, I initially used the implicit method: setData = function(segment){ var url = 'https://myFireBase.firebaseio.com/'; var rawData = angularFire(url+segment,$rootScope,'data',{}); rawData.then(function(d ...

Generating hills with PlaneGeometry in Three.js

Currently, I am searching for a straightforward technique to generate non-uniform hills in Three.js. By utilizing particles and positioning them with a sine wave algorithm like the following snippet: x = Math.random() * 1000 y = Math.sin( x / freq ) * ...

Combine theme configuration options within Material-UI

I am interested in setting up custom theme rules in Material-UI. My goal is to create both light and dark themes and extend them with some shared settings. My initial idea was to store the common settings for the light and dark themes in a separate variab ...

Styling text by reducing its size with CSS

Utilizing a CSS class to truncate text in the accordion header, which includes both a URL and plain text fields that need to be displayed on the same line. Desired Output: .../abc/xyz Count:42 Current output: .../abc/xyz Count:42/ (An odd slash is ...

creating dynamic column headers with JavaScript

I am looking for a way to make the column names dynamic so that I don't have to manually update them every time. Here is my code snippet: jqGrid11.prototype = { display : function() { $('body').append(this.html.join("")); $("#jqGrid").j ...

Accessing configuration properties from a JavaScript file in AngularJS version 1.5.X

Looking to retrieve configuration values based on environment. The config.js file contains the following entries: config.js: baseUrl='/home', abcUrl='/abc' I am attempting to access the baseUrl and abcUrl within one of my services us ...

What is the best way to position a single element in React using the Grid Component without causing any overlap?

I am struggling with positioning 3 components on my react page: PageHeader, SideMenu & FeatureList (which consists of Display Cards). Here is the code for each component: App.js // App.js code here... PageHeader.js // PageHeader.js code here... SideMenu ...

Accessing the URL causes malfunctioning of the dynamic routing in Angular 2

I am currently working on implementing dynamic routing functionality in my Angular application. So far, I have successfully achieved the following functionalities: Addition of routing to an existing angular component based on user input Removal of routin ...