How to refresh Wicket DataTable after submitting the Search button -
i refresh wicket datatable after submitting search button. here code:
public class searchpanel extends panel { private static final long serialversionuid = 1l; private searchservice searchservice; public searchpanel(string id,final searchservice searchservice) { super(id); this.searchservice = searchservice; add(new searchform("searchform")); } private class searchform extends form { private static final long serialversionuid = 1l; private string searchstring; public searchform(string id) { super(id); add(new textfield("searchstring", new propertymodel(this, "searchstring"))); } public void onsubmit() { pageparameters params = new pageparameters(); searchservice.findbytitleproperty(params.get("searchstring").tostring()); } public string getsearchstring() { return searchstring; } public void setsearchstring(string searchstring) { this.searchstring = searchstring; } } }
and "home page" following:
public class bookstablepage extends webpage implements searchservice { private static final long serialversionuid = 1l; @springbean private bookservice service; private list<icolumn<bookdto, string>> columns; private sortablebookdtodataprovider sortablebookdtodataprovider; private datatable table; public bookstablepage() { add(new searchpanel("searchpanel",this)); columns = new arraylist<icolumn<bookdto, string>>(); columns.add(new propertycolumn(new model<string>("id"), "id")); columns.add(new propertycolumn(new model<string>("isbn"), "isbn")); columns.add(new propertycolumn(new model<string>("title"), "title")); columns.add(new propertycolumn(new model<string>("author"), "author", "author")); columns.add(new propertycolumn(new model<string>("copies"), "copies", "copies")); //sortablebookdtodataprovider = new sortablebookdtodataprovider(service,"brave"); sortablebookdtodataprovider = new sortablebookdtodataprovider(service); table = new datatable("datatable", columns, sortablebookdtodataprovider, 3); table.addtoptoolbar(new headerstoolbar(table, (isortstatelocator) table.getdataprovider())); add(table); } @override public void findbytitleproperty(string value) { system.out.println("search string: "+value); sortablebookdtodataprovider = new sortablebookdtodataprovider(service,value); table = new datatable("datatable", columns, sortablebookdtodataprovider, 3); table.addtoptoolbar(new headerstoolbar(table, (isortstatelocator) table.getdataprovider())); this.replace(table); } }
}
the searchservice following:
public interface searchservice { public void findbytitleproperty(string value); }
this bookstablepage.html
<?xml version="1.0" encoding="utf-8" ?> <html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.3-strict.dtd"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>wicket library application</title> <link rel="stylesheet" href="style.css" type="text/css" media="screen" title="stylesheet" /> </head> <body> <span wicket:id="searchpanel"></span> <table wicket:id="datatable"></table> </body> </html>
and sortablebookdtodataprovider:
public class sortablebookdtodataprovider extends sortabledataprovider<bookdto, string> { private static final long serialversionuid = 1l; private static list<bookdto> list = new arraylist<bookdto>(); private sortablebookdtodataprovidercomparator comparator = new sortablebookdtodataprovidercomparator(); public sortablebookdtodataprovider(bookservice service) { list.addall(service.findall()); setsort("author", sortorder.ascending); } public sortablebookdtodataprovider(bookservice service,string title) { list.addall(service.findbytitle(title)); setsort("author", sortorder.ascending); } @override public iterator<bookdto> iterator(long first, long count) { list<bookdto> newlist = new arraylist<bookdto>(list); collections.sort(newlist, comparator); return newlist.sublist(safelongtoint(first), safelongtoint(first + count)).iterator(); } @override public long size() { return list.size(); } @override public imodel<bookdto> model(final bookdto object) { return model.of(object); } @override public void setsort(string property, sortorder order) { super.setsort(property, order); } public int safelongtoint(long l) { if (l < integer.min_value || l > integer.max_value) { throw new illegalargumentexception (l + " cannot cast int without changing value."); } return (int) l; } class sortablebookdtodataprovidercomparator implements comparator<bookdto>, serializable { public int compare(final bookdto b1, final bookdto b2) { propertymodel<comparable> model1 = new propertymodel<comparable>( b1, getsort().getproperty()); propertymodel<comparable> model2 = new propertymodel<comparable>( b2, getsort().getproperty()); int result = model1.getobject().compareto(model2.getobject()); if (!getsort().isascending()) { result = -result; } return result; } } }
since don't use ajax submit form wicket repaint whole page datatable.
if need use ajax submit need target.add(datatable)
Comments
Post a Comment