hi,
is there any way to tell what column the user has sorted on and in what
direction?
there is an OnSortOrderChanged() callback but there are no parameters to
provide the callback with any information other than the fact that the user
clicked on one of the column headers. having more information would be very
useful when reloading or modifying the data in a ListCtrl and re-sorting it
according to the currently sorted column and direction.
ideally, the callback should be passed col and ascending parameters that could
be stored and used later in a call to SortListItems. or better, so as not to
break any existing code, a new method could be added to supply this information
whenever needed.
although undocumented, the following can be used:
col = self._col
ascending = self._colSortFlag[self._col]
but we shouldn't have to read the source to determine this
i've attached a patch to listctrl.py that adds a GetSortState method to
ColumnSorterMixin in the hope that it might be adopted.
failing that, here's a subclass that provides the same functionality
for anyone who's interested:
class ColumnSorterMixin2(wx.lib.mixins.listctrl.ColumnSorterMixin):
'''Variant of ColumnSorterMixin that lets the client know which
column the user/programmer has sorted on and in what direction.'''
def GetSortState(self):
'''Return a tuple containing the index of the column that was last sorted
and the sort direction for that column.
Usage:
col, ascending = self.GetSortState()
# Make changes to list items... then resort
self.SortListItems(col, ascending)
'''
return (self._col, self._colSortFlag[self._col])
cheers,
raf
p.s. in order for its name to be accurate, OnSortOrderChanged should probably be
called whenever SortListItems is called as well as whenever the user clicks on a
column header (but the attached patch doesn't make this change).
listctrl.py.GetSortState.patch (896 Bytes)