Menu

#2750 Sort call to model not receiving the right parameters

5.0.7
closed
nobody
None
5
2014-08-21
2011-06-29
Darius
No

We have a Grid that has a ListModelExt attached. We have implemented/overridden the method: sort(Comparator c, boolean ascending)
The columns of our grid has custom Ascending and Descending comparators.

When a user clicks on a column (not using the column-context menu) everything works fine.
The first click calls sort(ASC_comparator, true),
and the next calls sort(DESC_comparator, false), and so on

However, if the user uses the column context-menu and chooses "Sort Descending", the call to the ListModelExt receives the Ascending sort comparator, instead of the Descending one.

I discovered that this only happens if the column's sort-direction is "natural". If the column sort-direction has been explicitly set to "ascending" (even though not ever sorted), then it passes in the Descending Comparator

Discussion

  • Peter Kuo

    Peter Kuo - 2011-07-11

    I tested with following sample code, and can't reproduce the problem.
    I always get the correct comparator.
    As System.out.println(cmpr); printed, it's correct comparator

    zul:

    <zk>
    <window >
    <zscript><![CDATA[
    ListModel strset = new org.zkoss.zktest.test2.grid.FakeListModel(10);
    class MyRowComparator implements Comparator {
    private boolean _asc;
    public MyRowComparator(boolean ascending) {
    _asc = ascending;
    }
    public int compare(Object o1, Object o2) {
    Row r1 = (Row)o1, r2 = (Row)o2;
    int v = ((Comparable)r1.getValue()).compareTo(r2.getValue());
    return _asc ? v: -v;
    }
    public String toString() {
    return "MyRowComparator [_asc=" + _asc +"]";
    }

    }
    Comparator asc = new MyRowComparator(true);
    Comparator dsc = new MyRowComparator(false);
    ]]>
    </zscript>

    <grid id="grid" width="200px" model="${strset}">
    <columns menupopup="auto">
    <column label="options" sort="auto" sortAscending="${asc}" sortDescending="${dsc}"/>
    </columns>
    </grid>

    </window>
    </zk>

    java:

    /* MainWindow.java

    {{IS_NOTE
    Purpose:

    Description:

    History:
    Oct ,31st 2007, Created by Jeff Liu
    }}IS_NOTE

    Copyright (C) 2007 Potix Corporation. All Rights Reserved.

    {{IS_RIGHT
    This program is distributed under GPL Version 3.0 in the hope that
    it will be useful, but WITHOUT ANY WARRANTY.
    }}IS_RIGHT
    */
    package org.zkoss.zktest.test2.grid;

    import java.util.Comparator;

    import org.zkoss.zul.AbstractListModel;
    import org.zkoss.zul.ListModelExt;
    import org.zkoss.zul.event.ListDataEvent;
    /**
    *
    * @author Jeff
    *
    */
    public class FakeListModel extends AbstractListModel implements ListModelExt {

    private int _size;
    private boolean _asc = true;

    public FakeListModel() {
    this(10000);
    }
    public FakeListModel(int size) {
    _size = size;
    }

    // ListModelExt
    public void sort(Comparator cmpr, boolean asc) {
    // System.out.println("==================SORT DATA================");
    System.out.println(cmpr);
    _asc = asc;
    invalidate();
    }
    public void invalidate() {
    fireEvent(ListDataEvent.CONTENTS_CHANGED, -1, -1);
    }

    //AbstractListModel
    public Object getElementAt(int v) {
    String value = "Option "+(_asc ? v: _size - v - 1);
    // System.out.println(value);
    return value;
    }

    //AbstractListModel
    public int getSize() {
    return _size;
    }

    //AbstractListModel
    public void setSize(int size){
    _size = size;
    }
    }

     
  • Peter Kuo

    Peter Kuo - 2011-07-11

    Can you provide a sample code that can reproduce the problem?

     
  • Peter Kuo

    Peter Kuo - 2011-07-12
    • status: open --> pending
     
  • Darius

    Darius - 2011-08-02

    Thanks for the response. I tested your source code, and it actually has the same issue. So, maybe I didn't describe what happens.

    If I run your example, and pull up the zul file, I see "Option 0", "Option 1", sorted ascending. If i click on the header, nothing seems to change (it stays sorted ascending). This is as expected. So far, so good. If I click again, it sorts descending. This sequence works well.

    Now, refresh or pull up the page again. Do not click on the header. Instead, open the up the context-menu for the column and select the "Sort Descending" option.

    The sort that is requested is Ascending, even though I'd have expected it to be descending.

    Note: The only way to test this is to choose descending *the FIRST time*, before the column has already been sorted one way or the other.

    Thanks

     
  • Darius

    Darius - 2011-08-02
    • status: pending --> open
     
  • BenBai

    BenBai - 2011-08-03
     
  • BenBai

    BenBai - 2011-08-03
    • status: open --> closed
     
  • BenBai

    BenBai - 2011-08-03
     

Log in to post a comment.

MongoDB Logo MongoDB