this is my third tutorial about some code-stuff I use in my webapps.
One of the functions I miss most in Xojo Webedition is an easy way to sort weblistboxes. In the Xojo examples is a project with a listbox with static columns.
Since I often use dynamic filled listboxes with different column counts, most examples wount work.
Another fact is, that there is no working example or hint in this forum, which solves the problem.
So I used my brain and made a simple solution, that everyone can use.
What you need:
- one Function:
SortListboxPlace it in the
SessionElement for example
- one empty Class:
Parameters & Properties:
SortListbox needs 2 parameter:
LST as WebListBox- this is our listbox which we want to sort
SelectedHeader as integer- this is the id of the column which is the source of the sort order
SortItem needs 2 properties:
ColumnValues() as String- an array where we store one row of our listbox
HeadIndex as integer- the index of the header the user clicked in the listbox
The code for sorting:
- Our Function get the following code:
[code] dim Cols() as SortItem
dim Head() as string
'Load the contents of the Listbox into an array (each row its own) (vals) and add them to an sortitem
'Load the contents of the column whis is about to sort into its own array (head)
for a as integer = 0 to LST.RowCount - 1
dim C as new SortItem_Column C.HeadIndex = SelectedHeader dim values() as string for n as integer = 0 to LST.ColumnCount - 1 values.Append(LST.Cell(a, n)) next C.ColumnValues = values cols.Append(C)
'Use the sortwith-function. So the head-column is sorted, the cols-array with the sortitem-objects will be sorted in the same order
'delete all rows from the listbox
'add the sortet rows back to the listbox
for i as integer = 0 to cols.Ubound
dim vals() as string = cols(i).ColumnValues
dim d as integer = LST.AddRow()
for a as integer = 0 to vals.Ubound LST.Cell(d, a) = vals(a) lst.Heading(a) = ReplaceAll(lst.Heading(a), " ?","") 'remove the sorted-sign from each column-header next
lst.Heading(SelectedHeader) = lst.Heading(SelectedHeader) + " ?"'add a sign to column-header to show which column is sorted
- The Listbox gets the following code in its
All you need to do is, to copy the function an create the simple class, which has no code. After this you add the one codeline to the HeaderPressed event of each of your listboxes. Now you have self-sorted listboxes.
don’t forget to check my other Turorials
Hope you’ll find this usefull!