- In my previous post we have learnt about creating a fake JSON API using JSON-SERVER Node module for rapid application development.
- In this Demo, “We will learn to use sorting and paging using JSON-SRVER node module”.
- We have used data.json file containing students details.The content of data.json file is as follows.
{ "students": [ { "id": 123, "name": "Sandeep", "subject": "Computer", "marks": 23 }, { "id": 193, "name": "Raja", "subject": "Mathematics", "marks": 25 }, { "id": 223, "name": "Smith", "subject": "Geography", "marks": 20 }, { "id": 313, "name": "Sandeep", "subject": "History", "marks": 21 } ] }
- The URL to access whole students data is local host in port 3000.The following screenshot shows the students JSON data.
- Now if we want to sort the data based on marks property we can pass URL parameter _sort and _order.The _sort option takes the property name and _order option takes the direction ASC and DESC.For our case _sort property takes marks and _order takes ASC.The following screenshot shows the sorting based on marks in ascending order.
- The console shows the log for the above sorting based on marks.
- For pagination we need to use _start and _end parameter to request for the range of data.The following screenshot shows the pagination for 1to 4.