java ee - How to add a ItemSorter to a table container in Vaadin? -


i have container class extends beanitemcontainer. want add itemsorter date_created attribute sort values in descending order.

container class.

public class notecontainer extends beanitemcontainer<casenote> implements serializable {     private static final long serialversionuid = -5926608449530066014l;      public static final string date_created = "datecreated";     public static final string created_by = "createdby";     public static final string text = "text";     public static final string action = "action";      public static final object[] natural_col_order = new object[] {         action, date_created, created_by, text      };      public static final string[] col_headers_english = new string[] {         "action", "date created/updated", "created/updated by", "note"     };      /**      * default constructor.      *       */     public notecontainer()     {         super(casenote.class);     } } 

casenote entity class, , inside date_created in java.util.date format.

please provide proper solution...

related sort vaadin table

thanx in advance.

beanitemcontainer has prepared set itemsorter easily. can use function:

public void sort(object[] propertyid, boolean[] ascending) 

so example can add sortbydate() function container this. here 3 example classes.

the bean:

public class casenote { private static final random r = new random(); private static final long y_in_millies = 1000l * 60l * 60l * 24l * 365l; private date datecreated = new date(system.currenttimemillis() - math.round(r.nextdouble() * y_in_millies));  private string text = uuid.randomuuid().tostring();  public string gettext() {     return text; }  public date getdatecreated() {     return datecreated; } } 

the container:

public class notecontainer extends beanitemcontainer<casenote> {  public notecontainer() {     super(casenote.class); }  public void sortbydate() {     sort(new string[] { "datecreated" }, new boolean[] { false }); } } 

the test ui:

public class testui extends ui {  @override protected void init(vaadinrequest request) {     notecontainer nc = new notecontainer();     (int = 0; < 10; i++) {         nc.additem(new casenote());     }     nc.sortbydate();      (int = 0; < 10; i++) {         nc.additem(new casenote());     }      table t = new table("mytable", nc);     setcontent(t); } } 

note: items added after calling sortbydate() function not sorted. if want have items sorted after every insert, overwrite additem() functions, call sort() after adding item.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -