top of page
Table Sort

Table Sort

Sort a table by clicking its column headers

Intermediate.png

Intermediate

3K

Published:

February 2, 2021

Girl Enjoying her Drink

by

Anchor 1
Hire a Developer

Example Description

In this example we built a table with sortable header. Clicking any column header sorts the table according to that column in ascending order. Each subsequent click to that header toggles the sort between ascending and descending.

Example Code

Tab 1

.

Tab 2

.

Tab 3

.

This code solution can be complicated.

How We Built It

Database Collection


We created one collection called Solar System. This collection includes details on the planets in the solar system.



Page Elements


We added the following elements:

  • Table: displays information about the planets in the solar system. 

  • Container box: for the table headings. We grouped all the heading texts into a single group that we'll use later in our code. In order to group items, select all the items and click the “Group Elements” button.

  • Button: for clearing the sort.



Page Code


We declared a constant called columnObject that holds the columns of our table.


In our page code we have the following functions:


  • updateTableRows: gets data from the collection and updates the table rows with this data. This function runs when the dataset is ready and loaded or when a header has been clicked.


  • formatDate: formats the date field from the collection to our desired format.


  • setHTML: sets the text and color of the heading. Headers that are clicked get a red color and also an up or down arrow added to their heading text, according to the order the data is sorted by (ascending or descending).


  • setTableSortingEvents: defines the event handlers: Clicking a header event handler: when clicking a header the event handler loops through all headers and changes them to blue. It changes the clicked header to red and adds an up or down arrow to the heading text according to the sort direction (first click is ascending and if it is already sorted in ascending order it changes the sort to descending order). It then calls updateTableRows() to update the table with the correctly sorted data. Clicking the clear sort button event handler: changes all the headers to blue and removes the arrows from the heading texts.


  • onReady: calls setTableSortingEvents() to create the event handlers, updates the table heading and calls updateTableRows() to get the initial solar system data.

APIs We Used

Non-code example.

Non-code example.

Non-code example.

Related Articles

Article Link

Hire a Developer

Velo solutions are powerful tools, but building them on your own can be challenging. Let an experienced Velo development shop build it for you, so you can keep working on your site or business.

Related Examples

Did this help?

Yes

|

No

Thanks for your feedback!

Repeater Context

Repeater Context

Change a specific item in a repeater when a site visitor interacts with it

Intermediate.png

Beginner

Infinite Scroll

Infinite Scroll

Add items to a repeater when a site visitor scrolls down the page

Intermediate.png

Intermediate

To Do List

To Do List

Keep track of your tasks with a To Do List

Intermediate.png

Intermediate

Anchor 2
bottom of page