0

how to sort grid column that is a date or a decimal amount

asked 2013-03-28 21:06:23 +0800

rickybaker gravatar image rickybaker
25 1

i know how to add sort="auto" in the zul for sorting. if i do this on a column with Dates or a dollar amount (minus the dollar sign) it sorts as a string

delete flag offensive retag edit

Comments

please refer to the updated sample

benbai ( 2013-04-17 09:51:57 +0800 )edit

5 Answers

Sort by ยป oldest newest most voted
0

answered 2013-04-16 10:22:58 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

updated 2013-04-17 09:51:12 +0800

You can use a Comparator,

e.g.,

test.zul

<zk>
    <div id="div" apply="org.zkoss.bind.BindComposer"
        viewModel="@id('vm') @init('test.TestVM')">
        <grid model="@load(vm.model)">
            <columns>
                <column label="Date"
                    sortAscending="@load(vm.dateAscComparator)"
                    sortDescending="@load(vm.dateDescComparator)"/>
                <column label="Value"
                    sortAscending="@load(vm.valueAscComparator)"
                    sortDescending="@load(vm.valueDescComparator)"/>
            </columns>
            <template name="model">
                <row>
                    <label value="${each.date}"/>
                    <label value="${each.value}"/>
                </row>
            </template>
        </grid>
    </div>
</zk>

TestVM.java

package test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;

public class TestVM {
    private ListModelList _model;
    private SimpleDateFormat _sdf = new SimpleDateFormat("yyyy-MM-dd");
    public ListModel getModel () throws ParseException {
        if (_model == null) {
            List<Data> l = new ArrayList<Data>();
            l.add(new Data(_sdf.parse("2013-01-01"), new Double(200)));
            l.add(new Data(_sdf.parse("2013-01-02"), new Double(100)));
            _model = new ListModelList(l);
        }
        return _model;
    }
    public Comparator getDateAscComparator () {
        return new Comparator () {
            public int compare(Object o1, Object o2) {
                Date d1 = ((Data)o1).getDate();
                Date d2 = ((Data)o2).getDate();
                return d1.before(d2)? 1
                        : d1.after(d2)? -1
                        : 0;
            }
        };
    }
    public Comparator getDateDescComparator () {
        return new Comparator () {
            public int compare(Object o1, Object o2) {
                Date d1 = ((Data)o1).getDate();
                Date d2 = ((Data)o2).getDate();
                return d1.before(d2)? -1
                        : d1.after(d2)? 1
                        : 0;
            }
        };
    }
    public Comparator getValueAscComparator () {
        return new Comparator () {
            public int compare(Object o1, Object o2) {
                Double d1 = ((Data)o1).getValue();
                Double d2 = ((Data)o2).getValue();
                return d1 > d2? 1
                        : d1 < d2? -1
                        : 0;
            }
        };
    }
    public Comparator getValueDescComparator () {
        return new Comparator () {
            public int compare(Object o1, Object o2) {
                Double d1 = ((Data)o1).getValue();
                Double d2 = ((Data)o2).getValue();
                return d1 > d2? -1
                        : d1 < d2? 1
                        : 0;
            }
        };
    }
    public static class Data {
        private Date _date;
        private Double _value;
        public Data (Date date, Double value) {
            _date = date;
            _value = value;
        }
        public Date getDate () {
            return _date;
        }
        public Double getValue () {
            return _value;
        }
    }
}

For more information, please refer to the document

Grids#Sorting

link publish delete flag offensive edit
0

answered 2013-04-18 15:53:34 +0800

dougraider gravatar image dougraider
16 1

I have written a comparator and put it in my ZUL page as follows:

<grid id="statusGrid" mold="paging" pagesize="06" >="" <columns="" >="" <column="" width="100px" label="ID" visible="false"/> <column width="90px" id="fitsIdColm" label="FITS ID" style="text-align: center" sort="auto"/> <column width="85px" id="statusColm" label="STATUS" style="text-align: center" sort="auto"/> <column width="180px" id="originatorColm" label="ORIGINATOR" style="text-align: center" sort="auto"/> <column width="150px" id="lastModTimeColm" label="LAST MODIFIED TIME" sortdescending="DateComparatorDescending"/>

I have written a comparator and put it in my composer as follows:

public class DateComparatorDescending implements Comparator {

// public class DateComparatorDescending implements Comparator { @Override public int compare( comm1, comm2) {

        long t1 = comm1.getDate().getTime();
        long t2 = comm2.getDate().getTime();
        return comm2.getDate().compareTo(comm1.getDate());
    }
}

When I try to reference my zul page, I get this error java.lang.ClassNotFoundException: DateComparatorDescending

How do I establish the linkage between my zul page and the corresponding controller?

Thanks,

Doug B ell

link publish delete flag offensive edit
0

answered 2013-04-20 03:48:47 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

You need to create an instance and refer to that instance,

e.g.,

DateComparatorDescending sortDesc = new DateComparatorDescending();

and then refer it as below

sortdescending="${sortDesc}"
link publish delete flag offensive edit
0

answered 2013-04-22 08:01:28 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

A complete sample for put them in zul:

<zk>
    <zscript><![CDATA[
        import java.text.ParseException;
        import java.text.SimpleDateFormat;
        import java.util.ArrayList;
        import java.util.Comparator;
        import java.util.Date;
        import java.util.List;
        import org.zkoss.zul.ListModel;
        import org.zkoss.zul.ListModelList;

        ListModelList _model;
        SimpleDateFormat _sdf = new SimpleDateFormat("yyyy-MM-dd");
        ListModel getModel ()  {
            if (_model == null) {
                List l = new ArrayList();
                try {
                    l.add(new Data(_sdf.parse("2013-01-01"), new Double(200)));
                    l.add(new Data(_sdf.parse("2013-01-02"), new Double(100)));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                _model = new ListModelList(l);
            }
            return _model;
        }
        Comparator getDateAscComparator () {
            return new Comparator () {
                public int compare(Object o1, Object o2) {
                    Date d1 = ((Data)o1).getDate();
                    Date d2 = ((Data)o2).getDate();
                    return d1.before(d2)? 1
                            : d1.after(d2)? -1
                            : 0;
                }
            };
        }
        Comparator getDateDescComparator () {
            return new Comparator () {
                public int compare(Object o1, Object o2) {
                    Date d1 = ((Data)o1).getDate();
                    Date d2 = ((Data)o2).getDate();
                    return d1.before(d2)? -1
                            : d1.after(d2)? 1
                            : 0;
                }
            };
        }
        Comparator getValueAscComparator () {
            return new Comparator () {
                public int compare(Object o1, Object o2) {
                    Double d1 = ((Data)o1).getValue();
                    Double d2 = ((Data)o2).getValue();
                    return d1 > d2? 1
                            : d1 < d2? -1
                            : 0;
                }
            };
        }
        Comparator getValueDescComparator () {
            return new Comparator () {
                public int compare(Object o1, Object o2) {
                    Double d1 = ((Data)o1).getValue();
                    Double d2 = ((Data)o2).getValue();
                    return d1 > d2? -1
                            : d1 < d2? 1
                            : 0;
                }
            };
        }
        class Data {
            private Date _date;
            private Double _value;
            public Data (Date date, Double value) {
                _date = date;
                _value = value;
            }
            public Date getDate () {
                return _date;
            }
            public Double getValue () {
                return _value;
            }
        }
        ListModel model = getModel();
        Comparator dateAscComparator = getDateAscComparator();
        Comparator dateDescComparator = getDateDescComparator();
        Comparator valueAscComparator = getValueAscComparator();
        Comparator valueDescComparator = getValueDescComparator();
    ]]></zscript>
    <div id="div">
        <grid model="${model}">
            <columns>
                <column label="Date"
                    sortAscending="${dateAscComparator}"
                    sortDescending="${dateDescComparator}"/>
                <column label="Value"
                    sortAscending="${valueAscComparator}"
                    sortDescending="${valueDescComparator}"/>
            </columns>
            <template name="model">
                <row>
                    <label value="${each.date}"/>
                    <label value="${each.value}"/>
                </row>
            </template>
        </grid>
    </div>
</zk>

By the way, zscript is for fast prototyping, it is a quick way for you to do POC with your customers but not suggested to use in production.

link publish delete flag offensive edit
0

answered 2013-04-24 15:41:10 +0800

dougraider gravatar image dougraider
16 1

Thank you for your answer. I would rather not use zscript, since, like you said, it is not supposed to be used for this. I would rather use some code in the calling composer as my comparator. I have put the code in the composer, but continue to get a "class not found" exception. Can you send me an simple example of both the composer code and the zk page code, that allows these two items to communicate? Is there some kind of "import" command that I'm missing?

Sorry to be such a pain, but I'm a real newbie at this stuff.

Thanks,

Doug

link publish delete flag offensive edit

Comments

got it, I'll arrange to do a sample

benbai ( 2013-04-25 09:01:36 +0800 )edit

please refer to https://dl.dropboxusercontent.com/u/90546423/grid_sort.zip , full project and corresponding war in it.

benbai ( 2013-04-25 11:12:24 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2013-03-28 21:06:23 +0800

Seen: 113 times

Last updated: Apr 24 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More