1

Under normal conditions no huge data or paging even, how can you sort a ZK listbox by using more than one column ?

Sort by the first column, sort by the second and so on.

And defenetely how do you keep the sorted icon on label of header intact unless you click it again ?

I have a ZK version 7.0.3 for reference.

hephestos
  • 434
  • 1
  • 6
  • 19

2 Answers2

2

Use MVVM and do the actual sorting in the view model. In the view model have a custom comparator which sorts on multiple fields. Your sortable listbox listheaders would look something like this:

<listheader label="Name" sort="auto(each.name)" onSort="@command('nameSort', asc=event.isAscending())" />

and in your view model:

@Command
@NotifyChange("names")
public void nameSort(@BindingParam("asc") final boolean isAscending) {
    // modify your custom comparator
}

As an aside, if you don't have big data and aren't using paging do you really need to sort on multiple fields?

Col
  • 381
  • 1
  • 7
  • 13
  • Thanks Col, MVVM I do use and to be honest it never slipped my mind to use this command argument for sorting. After some hours thinking the problem I solved in a bizarre way. I use MVVM, I use an object which holds an array of the keys sorted and the type of them. when I click a column the sort mode and the id of label are stored. or replaced. Then the data really get updated and sorted from db call. which is also a db call with paging capability. I do have paging and I do have a large dataset I put those constraints in the question just for the simplicity and for the easiness of the problem – hephestos May 27 '18 at 00:54
  • schematically let's say is like: user clicks on columns, event onSort is fired which updates a mediator object with reference to key id of column and sort mode, then event onPaging gets the page of the listbox ( or zero in absense ) and then hits the database. The only thing that is ugly, is that I had to put an additional button in the listhead that turns to "natural" the order. So the user can reset the sort method for the column. As the listheader seems not to have three-state sorting mode. – hephestos May 27 '18 at 00:57
  • 1
    You should add an answer and accept it for someone with the same problem in the future; otherwise it looks like an unsolved problem. Glad you got it sorted. – Col May 27 '18 at 07:33
1

It can even be easier because auto sort also supports multiple fields also.

sort="auto(lastName, firstName)" 

Like this, no extra code needed for the header icon.

chillworld
  • 4,207
  • 3
  • 23
  • 50