cancel
Showing results for 
Search instead for 
Did you mean: 

How to sort table immediately

Former Member
0 Kudos

Hello,

I implemented a tablesorter for my table but when i run the application, no default column in sorted. Only when i click on a column header, the icon shows and sorting is applied. How can i make the table sort from the start?

Also when i refresh the data of the table node, the icon stays in the same place but the sorting is not correct anymore because the data in de node changed. What is the best way to solve this when i reapply data to the table node?

Many thanks,

Hugo Hendriks

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Hugo,

It is quite possible to force a default sort of your tables. I extended the TableSorter.class we use to allow this with the following additional overloaded sort() method :-

/**
	 * Created by: 	G.Ryan
	 * Data:		04.05.2006
	 * This method will sort the given dataSource (a context node) by the column specified
	 * by its ID.  It can be used on the initial display of a table to force sorting on a
	 * certain column
	 * @param columnId - The ID of the column to be sorted - must be the ID of a TableColumn object in a table which uses the specified dataSource as it's data
	 * @param dataSource - The context node used to supply the table with data
	 */
	public void sort(String columnId, IWDNode dataSource) {
		IWDTableColumn column =
			(IWDTableColumn) table.getView().getElement(columnId);
		ReversableComparator reversable =
			(ReversableComparator) comparatorForColumn.get(column);
		if (reversable == null)
			return; // not a sortable column

		// remove icon of previously sorted column
		if (currentlySortedColumn != null
			&& currentlySortedColumn.getHeader() != null)
			currentlySortedColumn.getHeader().setImageSource(null);

		// bookkeeping
		if (column == currentlySortedColumn) {
			//			reversable.toggleReversed();
		}

		currentlySortedColumn = column;

		// set icon in currently sorted column
		if (currentlySortedColumn.getHeader() != null)
			currentlySortedColumn.getHeader().setImageSource(
				reversable.isReversed()
					? "~sapicons/s_b_srtd.GIF"
					: "~sapicons/s_b_srtu.GIF");

		// sorting
		dataSource.sortElements(reversable);
	}

I'm sure there are other ways to achieve the same but this works for our requirements. Basically, you call the above method after you have populated your context node and pass in the column ID. It defaults to ascending sort of that column.

I also find it annoying that when we refresh the data of the node the sort does not reset - I've not done anything with this yet as the users have not complained but it does annoy me! Probably need to clear the tablesorter and re-assign it to the table and then perform your default column sort when yuo refresh the data. Don't forget you typically only assign the tableSorter the first time through wdDoModifyView otherwise it would lose the sorting each time you refreshed the screen. You need to find a place to assign the refreshed tableSorter when you refresh the data.

Hope that helps,

Gareth.

Former Member
0 Kudos

Much thanks Gareth. That I was looking for.

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi

See the thread

Kind Regards

Mukesh