Sorting Angularjs ng-repeat by date

2024/10/13 14:25:19

I am relatively new to AngularJS. Could use some help

I have a table with the following info

<table><tr><th><span ng-click="sortType = 'first_name'; sortReverse = !sortReverse">Referral Name</span></th><th><span ng-click="sortType = 'date'; sortReverse = !sortReverse">Referral Name</span></th></tr><tr ng-repeat="x in referral | orderBy:sortType:sortReverse"><td>name</td><td>date</td></tr>
</tabe>

And the js code is as follows (after the controller connections)

$scope.sortType = '';
$scope.sortReverse = false;

This works perfectly for ascending and descending when sorting the name.

Unfortunately it works similarly in the case of date too (it is sorting alphabetically, rather than by date).

The date format I am getting from the backend(python) is in this format:

i["date"] = i["date"].strftime("%B %d, %Y")
September 13, 2016 <-- this format

I understand the mistake I made, but I am not able to find the solution for it.

How can I sort by date?

Thanks in advance guys.

Answer

Ideally you'd have a sortable object for date. One candidate is an isoformatted date:

i["date"] = i["date"].isoformat()

Now sorting should work just fine but it'll display wonky. So you'll need to use a date filter to format it on the UI:

<table><tr><th><span ng-click="sortType = 'first_name'; sortReverse = !sortReverse">Referral Name</span></th><th><span ng-click="sortType = 'date'; sortReverse = !sortReverse">Referral Name</span></th></tr><tr ng-repeat="x in referral | orderBy:sortType:sortReverse"><td>name</td><td>{{x.date | date : 'MMMM d, yyyy'}}</td></tr>
</table>
https://en.xdnf.cn/q/118069.html

Related Q&A

Html missing when using View page source

Im trying to extract all the images from a page. I have used Mechanize Urllib and selenium to extract the Html but the part i want to extract is never there. Also when i view the page source im not abl…

Move file to a folder or make a renamed copy if it exists in the destination folder

I have a piece of code i wrote for school:import ossource = "/home/pi/lab" dest = os.environ["HOME"]for file in os.listdir(source):if file.endswith(".c")shutil.move(file,d…

Segmentation fault after removing debug printing

I have a (for me) very weird segmentation error. At first, I thought it was interference between my 4 cores due to openmp, but removing openmp from the equation is not what I want. It turns out that wh…

numpy get 2d array where last dimension is indexed according to a 2d array

I did read on numpy indexing but I didnt find what I was looking for.I have a 288*384 image, where each pixel can have a labelling in [0,15]. It is stored in a 3d (288,384,16)-shaped numpy array im.Wit…

Error sending html email with mailgun python API

I can send text email with the Mailgun python API fine:def send_simple_message(mailtext, filename=""):requests.post("https://api.mailgun.net/v3/mydomain.in/messages",auth=("api…

How to take HTML user input and query it via Python SQL?

Is there a way to take user input from HTML, and use python to run the input through to a SQL database? Does the input need to be parsed? I want the the user to be able to type in a store name, and f…

Reading and taking specific file contents in a list in python

I have a file containing:name: Sam placing: 2 quote: Ill win.name: Jamie placing: 1 quote: Be the best.and I want to read the file through python and append specific contents into a list. I want my fir…

Scipy / ctypes build-config: How to load lib?

These docs have a nice example on how to compile some external C/C++ code and load this using ctypes. This works great with manual compilation (where im controlling the names of my library which later …

Webfaction Django 1.4.1: easy_thumbnails 3.0b – Couldnt get the thumbnail error

I use easy_thumbnails and it works fine on a development machine but in production I get errors like shown below, when I use {% thumbnail photo.image 300x170 %} templatetag. Though can directly browse …

acronym replacement with its value using python

i have dictionary like that i need to replace acronyms in text with its value in dictionary i use this code but it doesnt give me the appropriate result when i test the function using acronyms("we…