How Do I Customize Column Sorting?

This lesson will demonstrate how to customize sorting for columns by handling the SortDataGridColumn message that is sent when the user clicks on a column.

A Data Grid Table

A Data Grid Table

I am going to customize when happens when this table is sorted by the Line Number column.

The Data Grid Group Script

The Data Grid Group Script

In the script for the data grid I added the above handler. SortDataGridColumn is called whenever the dgProps["sort by column"] property is set. This includes when the user clicks on a column header to sort.

The way SortDataGridColumn works is simple. If you pass the message then the data grid will use the default column sorting routines (1). If you don't pass the message then the data grid will not perform any sorting (2) and (3).

I want Line Number to just reverse the current sort so I call ReverseSort which is a built-in data grid helper that reverses whatever the current sort order is(2).

The Result

The Result

Clicking on the Line Number column now reverses the sort.

Custom Sort Example

## Place in Data Grid script.
## This example shows how to perform a custom sort of data. This is similar
## to the internal routine that the data grid uses.
on SortDataGridColumn pColumn
	## Begin by building a list of lines with two items (tab delimited)
	## The first item is the index of each record stored in the data grid.
	## The second item is the key you are going to sort on.
	put the dgData of me into theDataA
	repeat for each key theIndex in theDataA
		put theIndex & tab & theDataA[theIndex][pColumn] & cr after theData
	end repeat
	delete the last char of theData
	set the itemdelimiter to tab
	
	## perform sort. Lookup the current sort direction for this column
	## and use that as sort direction
	put the dgColumnSortDirection[pColumn] of me into theSortDirection
	if theSortDirection is "ascending" then
		sort lines of theData ascending by item 2 to -1 of each	# 
	else
		sort lines of theData descending by item 2 to -1 of each
	end if
	
	## Rebuild the order of indexes in the data grid
	put empty into theIndexSequencing
	repeat for each line theLine in theData
		put item 1 of theLine & comma after theIndexSequencing
	end repeat
	delete the last char of theIndexSequencing
	
	## Set the dgIndexes property to new order
	set the dgIndexes of me to theIndexSequencing
	
	## Tell data grid to hilite column
	HiliteAndStoreSortByColumn pColumn
end SortDataGridColumn

0 Comments

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.