0

Set Column for Default Sorting

asked 2012-01-10 19:03:12 +0800

Coder gravatar image Coder
33

I'm working on an application where the data needs to ibe default sorted on the 4th column when users first enter the application (from there they can sort on any of the columns at will).

I'm new to this language, and haven't been able to find anything that works browsing from google.

Any help would be appreciated.

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2012-01-14 05:05:19 +0800

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

Hi Coder,

Try call the sort method of listheader (if you use Listbox) while create,
for example

<listheader ... onCreate="self.sort(true);"/>

References:
Listheader#sort
Column#sort

Regards,
ben

link publish delete flag offensive edit

answered 2012-01-23 22:39:37 +0800

Coder gravatar image Coder
33

Still not working. I've tried a couple things:

<column label="My Column"
style="columnheader"
sortAscending="${columnAsc}"
sortDescending="${columnDesc}"
onCreate="self.sort(false);" />

and

<column label="My Column"
style="columnheader"
sortAscending="${columnAsc}"
sortDescending="${columnDesc}"
sortDirection="descending"
onCreate="self.sort(false);" />


I've tried both "true" and "false" for the sort parameter. In all cases, the column loads with the descending arrow in the column highlighted, but the values aren't sorted. I've tried the force call (sort(true, true), but that isn't working either

link publish delete flag offensive edit

answered 2012-01-24 03:53:32 +0800

matthewgo gravatar image matthewgo
375

Hi Coder,
Please try the following code:

<grid>
.....
<custom-attributes org.zkoss.zul.grid.autoSort="true"/>
.......
</grid>

link publish delete flag offensive edit

answered 2012-01-25 23:24:06 +0800

Coder gravatar image Coder
33

Matthew, I tried inserting that line as you had it into the code for the column I want to sort on and it had no effect. It did not throw any errors, but did continue sorting on the first column. Was there any adjustments I needed to make to it?

link publish delete flag offensive edit

answered 2012-02-01 01:28:38 +0800

matthewgo gravatar image matthewgo
375

Hi Coder,
Here is a simple sample:

ZKFiddle-Link

MyMail.java
package j2nknbfu$v4;

public class MyMail {

private String subject;

private String sender;

private Integer size;

public MyMail(String subject, String sender, Integer size) {
this.subject = subject;
this.sender = sender;
this.size = size;
}

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public String getSender() {
return sender;
}

public void setSender(String sender) {
this.sender = sender;
}

public Integer getSize() {
return size;
}

public void setSize(Integer size) {
this.size = size;
}

}


MyRowRenderer.java
package j2nknbfu$v4;

import org.zkoss.zk.ui.*;
import org.zkoss.zk.ui.event.*;
import org.zkoss.zk.ui.util.*;
import org.zkoss.zk.ui.ext.*;
import org.zkoss.zk.au.*;
import org.zkoss.zk.au.out.*;
import org.zkoss.zul.*;


public class MyRowRenderer implements RowRenderer {
public void render(final Row row, final java.lang.Object data) {
MyMail mail = (MyMail) data;
new Label(mail.getSubject()).setParent(row);
new Label(mail.getSender()).setParent(row);
new Label(mail.getSize().toString()).setParent(row);
}
}


TestComposer.java
package j2nknbfu$v4;

import org.zkoss.zk.ui.*;
import org.zkoss.zk.ui.event.*;
import org.zkoss.zk.ui.util.*;
import org.zkoss.zk.ui.ext.*;
import org.zkoss.zk.au.*;
import org.zkoss.zk.au.out.*;
import org.zkoss.zul.*;
import java.util.*;

public class TestComposer extends GenericForwardComposer{

private Grid inboxGrid;
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);

inboxGrid.setModel(new ListModelList(getData()));

}

public void onClick$refreshBtn(Event e){
inboxGrid.setModel(new ListModelList(getUpdatedData()));
}

/* simply return a small model here , you could read data from database for your own implementation.*/
private List<MyMail> getData(){
ArrayList<MyMail> list= new ArrayList<MyMail>();

list.add(new MyMail("Test Mail1","TonyQ", 10));
list.add(new MyMail("Test Mail12","Ryan", 100));
list.add(new MyMail("Test Mail13","Simon", 15));
list.add(new MyMail("Test Mail14","Jimmy", 5));
return list;
}

private List<MyMail> getUpdatedData(){
ArrayList<MyMail> list= new ArrayList<MyMail>();

list.add(new MyMail("Test Mail1-updated","TonyQ", 10));
list.add(new MyMail("Test Mail12-updated","Ryan", 100));
list.add(new MyMail("Test Mail13-updated","Simon", 15));
list.add(new MyMail("Test Mail14-updated","Jimmy", 5));
return list;
}

}


index.zul
<zk>
<!-- http://www.zkoss.org/forum/listComment/18064-Auto-sort-on-a-grid-doesn-t-update-after-refresh-of-data?lang=en -->
<div apply="j2nknbfu$v4.TestComposer">
<grid id="inboxGrid" height="350px"
rowRenderer="j2nknbfu$v4.MyRowRenderer"
emptyMessage="Nothing in Inbox.">
<custom-attributes org.zkoss.zul.grid.autoSort="true" />
<columns>
<column label="Subject" />
<column width="150px" label="Received" sort="auto(sender)" onCreate="self.sort(true);"/>
<column width="80px" label="Size" />
</columns>
</grid>

<button label="updated data and refresh" id="refreshBtn" />
</div>
</zk>

link publish delete flag offensive edit

answered 2012-02-01 22:39:10 +0800

Coder gravatar image Coder
33

Still not working.

My layout is:

<grid id="asdf" model="${asdf} rowRenderer="asdf" height="150px">
    <custom-attributes org.zkoss.zul.grid.autoSort="true" />
<columns sizable="true" >
<column sortAscending="${asdf}" sortDescending="${asdf}" width ="x%"><label value="asdf 

 asdf 

 " sclass="word-wrap" multiline="true"> </label></column>
[the above column definition x10]

<column width="x%" label = "asdf" sortAscending="${asdf}" sortDescending="${asdf}" sortDirection="descending" />


I have tried:

<column sortAscending="${asdf}" sortDescending="${asdf}" width ="x%" sort="auto(sender)" onCreate="self.sort(true);" ><label value="asdf asdf " sclass="word-wrap" multiline="true" > </label></column>


<column sortAscending="${asdf}" sortDescending="${asdf}" sortDirection="descending" width ="x%" sort="auto(sender)" onCreate="self.sort(true);" ><label value="asdf asdf " sclass="word-wrap" multiline="true" </label></column>

<column sortDirection="descending" width ="x%" sort="auto(sender)" onCreate="self.sort(true);" ><label value="asdf asdf " sclass="word-wrap" multiline="true" </label></column>

<column sortAscending="${asdf}" sortDescending="${asdf}" sortDirection="descending" width ="x%" sort="auto(sender)" onCreate="self.sort(true, true);" ><label value="asdf asdf " sclass="word-wrap" multiline="true" </label></column>


I have also tried all four of these with the <column ... /> definition.

link publish delete flag offensive edit

answered 2012-02-02 10:42:10 +0800

matthewgo gravatar image matthewgo
375

updated 2012-02-02 10:42:24 +0800

Hi Coder,
I have updated the demo. Please check your customComparator.

ZKFiddle-Link

TestComparator.java
package j2nknbfu$v12;


import java.io.Serializable;
import java.util.Comparator;


public class TestComparator implements Comparator, Serializable {
private boolean asc = true;
private int type = 0;

public TestComparator(boolean asc, int type) {
this.asc = asc;
this.type = type;
}

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}

public int compare(Object o1, Object o2) {
MyMail myMail1 = (MyMail) o1;
MyMail myMail2 = (MyMail) o2;
switch (type) {
case 1: // Compare Title
return myMail1.getSubject().compareTo(myMail2.getSubject()) * (asc ? 1 : -1);
case 2: // Compare First Name
return myMail1.getSender().compareTo(myMail2.getSender()) * (asc ? 1 : -1);
case 3: // Compare Last Name
return myMail1.getSize().compareTo(myMail2.getSize()) * (asc ? 1 : -1);
default: // Full Name
return myMail1.getSubject().compareTo(myMail2.getSubject()) * (asc ? 1 : -1);
}

}

}


MyMail.java
package j2nknbfu$v12;

public class MyMail {

private String subject;

private String sender;

private Integer size;

public MyMail(String subject, String sender, Integer size) {
this.subject = subject;
this.sender = sender;
this.size = size;
}

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public String getSender() {
return sender;
}

public void setSender(String sender) {
this.sender = sender;
}

public Integer getSize() {
return size;
}

public void setSize(Integer size) {
this.size = size;
}

}


MyRowRenderer.java
package j2nknbfu$v12;

import org.zkoss.zk.ui.*;
import org.zkoss.zk.ui.event.*;
import org.zkoss.zk.ui.util.*;
import org.zkoss.zk.ui.ext.*;
import org.zkoss.zk.au.*;
import org.zkoss.zk.au.out.*;
import org.zkoss.zul.*;


public class MyRowRenderer implements RowRenderer {
public void render(final Row row, final java.lang.Object data) {
MyMail mail = (MyMail) data;
new Label(mail.getSubject()).setParent(row);
new Label(mail.getSender()).setParent(row);
new Label(mail.getSize().toString()).setParent(row);
}
}


TestComposer.java
package j2nknbfu$v12;

import org.zkoss.zk.ui.*;
import org.zkoss.zk.ui.event.*;
import org.zkoss.zk.ui.util.*;
import org.zkoss.zk.ui.ext.*;
import org.zkoss.zk.au.*;
import org.zkoss.zk.au.out.*;
import org.zkoss.zul.*;
import java.util.*;

public class TestComposer extends GenericForwardComposer{

private Grid inboxGrid;
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);

inboxGrid.setModel(new ListModelList(getData()));

}

public void onClick$refreshBtn(Event e){
inboxGrid.setModel(new ListModelList(getUpdatedData()));
}

/* simply return a small model here , you could read data from database for your own implementation.*/
private List<MyMail> getData(){
ArrayList<MyMail> list= new ArrayList<MyMail>();

list.add(new MyMail("Test Mail1","TonyQ", 10));
list.add(new MyMail("Test Mail12","Ryan", 100));
list.add(new MyMail("Test Mail13","Simon", 15));
list.add(new MyMail("Test Mail14","Jimmy", 5));
return list;
}

private List<MyMail> getUpdatedData(){
ArrayList<MyMail> list= new ArrayList<MyMail>();

list.add(new MyMail("Test Mail1-updated","TonyQ", 10));
list.add(new MyMail("Test Mail12-updated","Ryan", 100));
list.add(new MyMail("Test Mail13-updated","Simon", 15));
list.add(new MyMail("Test Mail14-updated","Jimmy", 5));
return list;
}

}


index.zul
<zk>
<!-- http://www.zkoss.org/forum/listComment/18064-Auto-sort-on-a-grid-doesn-t-update-after-refresh-of-data?lang=en -->
<zscript><![CDATA[
Comparator projectAsc = new j2nknbfu$v12.TestComparator(true, 1);
Comparator projectDsc = new j2nknbfu$v12.TestComparator(false, 1);
Comparator senderAsc = new j2nknbfu$v12.TestComparator(true, 2);
Comparator senderDsc = new j2nknbfu$v12.TestComparator(false, 2);
Comparator sizeAsc = new j2nknbfu$v12.TestComparator(true, 3);
Comparator sizeDsc = new j2nknbfu$v12.TestComparator(false, 3);


]]></zscript>
<div apply="j2nknbfu$v12.TestComposer">
<grid id="inboxGrid" height="350px"
rowRenderer="j2nknbfu$v12.MyRowRenderer"
emptyMessage="Nothing in Inbox.">

<columns>
<column label="Subject" sortAscending="${projectAsc}" sortDescending="${projectDsc}" />
<column width="150px" label="Sender" sortAscending="${senderAsc}" sortDescending="${senderDsc}" onCreate="self.sort(true)"/>
<column width="80px" label="Size" sortAscending="${sizeAsc}" sortDescending="${sizeDsc}" />
</columns>
</grid>

<button label="updated data and refresh" id="refreshBtn" />
</div>
</zk>

link publish delete flag offensive edit
Your reply
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

RSS

Stats

Asked: 2012-01-10 19:03:12 +0800

Seen: 402 times

Last updated: Feb 02 '12

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