This repository was archived by the owner on Apr 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Lesson 5.1
Mary Alice Moore edited this page Oct 7, 2024
·
5 revisions
You may have noticed that the Airtable API returns table records in an arbitrary (random) order by default.
For example, if you have the following list:
- Item 1
- Item 2
- Item 3
The API might return this:
- Item 3 (how did #3 get up here??)
- Item 1
- Item 2
How do we fix this so that our list items appear in a more logical order? There are a few possible approaches:
- Pass the
viewquery parameter in the API request - Pass the
sortquery parameters (fieldanddirection) in the API request - Use JavaScript to sort the response data from the API
- Locate the fetch request for retrieving list items from Airtable API
- At the end of the URL, append a query parameter with name
viewand valueGrid%20view(or the name of your view in Airtable if you changed it)- hint: URL query begins with a question mark (
?) and is following by name/value pairs separated by an ampersand (name=value&name=value)
- hint: URL query begins with a question mark (
- Run your application and view in browser
- Verify that the order of list items now matches the order seen in Airtable
- In Airtable, open the corresponding table and drag/drop the last record above the first
- Refresh your application and verify that the same list item now appears at the top
- Locate the same fetch URL from the previous section
- At the end of the URL, append the following query parameters (don't forget the
&delimeter):-
sort[0][field]with valueTitle -
sort[0][direction]with value "asc" (short for ascending which means low-to-high or A-to-Z)
-
- View your application in browser
- Verify that your list items now appear in alphabetical order by "Title"
- Locate the same fetch request from the previous section
- Inside the final
thenmethod, start a new line above the existing code - Call the
sortmethod ondata.recordsand pass it a custom callback function:- function should take two parameters: (1)
objectAand (2)objectB - function should compare the
Titlefield for each object and return the following:-
-1if "Title A" is less than "Title B" -
0if "Title A" and "Title B" are the same -
1if "Title A" is greater than "Title B"
-
- function should take two parameters: (1)
- View your application in browser
- Verify that your list items still appear in ascending alphabetical order by "Title" (A-to-Z)
Now let's try reversing the order...
- Update the sort callback function to return the following:
-
1if "Title A" is less than "Title B" -
0if "Title A" and "Title B" are the same -
-1if "Title A" is greater than "Title B"
-
- View your application in browser
- Verify that your list items now appear in descending alphabetical order by "Title" (Z-to-A)
- Create a toggle button so the user can switch between ascending and descending sort order
- Sort by a different field, such as
createdTime - Currently we need to refresh the browser in order to sort new tasks by date. Have a new task automatically sorted when added. HINT: Using the hooks already imported/in place: how would you update the code so the app's state changes with the new task?