Wicket SELECT doesn't update it's model -
i've wicket panel list of productviews (as select)
after choose productview select, load product database id of productview details form. can modify product entity , can save when finish.
after save try refresh select list update data, doesn't work(i mean, example, select contains old name of product after rename it, when select same productview, reload entity details form again, , of course new data appears database) don't want reload product list again, want solve memory. here source:
productview:
@entity @table(name = "product") @xmlrootelement public class productview implements serializable{ private static final long serialversionuid = 1l; @id @generatedvalue(strategy = generationtype.identity) @basic(optional = false) @column(name = "id") private long id; @column(name = "name") private string name; @enumerated(enumtype.ordinal) @column(name = "category") private category category; public productview() { } public productview(long id) { this.id = id; } public productview(product product) { this.id = product.getid(); this.name = product.getname(); this.category = product.getcategory(); } // + getters & setters }
product:
@entity @table(name = "product") @xmlrootelement public class product implements serializable { // same productview more data, objects, connections, etc }
and wicket panel comments
private product product; private productview productview; private list<productview> productviews; private form productform; private select categoryselectcomponent; private webmarkupcontainer contentcontainer; public productpanel(string id) { super(id); setoutputmarkupid(true); add(contentcontainer = new webmarkupcontainer("contentcontainer")); // container div contentcontainer.setoutputmarkupid(true); // refreshable contentcontainer.add(productform = new form("productform")); // details form contentcontainer.add(categoryselectcomponent = new select("categoryselectcomponent", new propertymodel<productview>(this, "productview"))); // item select categoryselectcomponent.add( new selectoptions<productview>( // first category "onecategory", new propertymodel<list<productview>>(this, "oneproducts"), // see getoneproducts(); list of productviews new ioptionrenderer<productview>() { @override public string getdisplayvalue(productview p) { return p.getname(); } @override public imodel<productview> getmodel(productview p) { return new model<productview>(p); } })); categoryselectcomponent.add( new selectoptions<productview>( // second category "twocategory", new propertymodel<list<productview>>(this, "twoproducts"), // see gettwoproducts(); new ioptionrenderer<productview>() { @override public string getdisplayvalue(productview p) { return p.getname(); } @override public imodel<productview> getmodel(productview p) { return new model<productview>(p); } })); categoryselectcomponent.add( new selectoptions<productview>( // third category "threecategory", new propertymodel<list<productview>>(this, "threeproducts"), // see getthreeproducts(); new ioptionrenderer<productview>() { @override public string getdisplayvalue(productview p) { return p.getname(); } @override public imodel<productview> getmodel(productview p) { return new model<productview>(p); } })); categoryselectcomponent.add(new onchangeajaxbehavior() { // update form after choose entity @override protected void onupdate(final ajaxrequesttarget art) { product = getproductfacade().find( productview.getid() ); updatepanel(art); } }); productform.add( // details component (textfields, radios, links, etc) edit product ); productform.add(new ajaxsubmitlink("formsubmitlink") { // save entity @override protected void onsubmit(ajaxrequesttarget art, form<?> form) { super.onsubmit(art, form); // don't know necessary @ getproductfacade().edit( product ); updateproductviewincategorymap(art); // important method //art.add(contentcontainer); //it in update method } }); }
more methods inside panel
private map<category, list<productview>> categorymap; // product categories public void initcategorymap() { categorymap = new enummap<category, list<productview>>(productview.class); categorymap.put( category.one, new arraylist<productview>() ); categorymap.put( category.two, new arraylist<productview>() ); categorymap.put( category.three, new arraylist<productview>() ); (productview view : getproductviews()) { categorymap.get(view.getcategory()).add(view); } } //***** products categories ******* final public list<productview> getoneproducts(){ if (categorymap == null){ initcategorymap(); } return categorymap.get( category.one ); } final public list<productview> gettwocategory(){ if (categorymap == null){ initcategorymap(); } return categorymap.get( category.two ); } final public list<productview> getthreeproducts(){ if (categorymap == null){ initcategorymap(); } return categorymap.get( category.three ); } // ************************************** public list<productview> getproductviews() { // product if (productviews == null) { productviews = getproductfacade().findallproductasview(); } return productviews; } private void updatepanel(ajaxrequesttarget art) { // refresh panel art.add(productpanel.this); } private void updateproductviewincategorymap(ajaxrequesttarget art) { // change product in map after save (call onsubmit method of ajaxsubmitlink) for(map.entry<category, list<productview>> entry : categorymap.entryset()){ // search category contains entity if (entry.getvalue().contains( productview )){ entry.getvalue().remove( productview ); // remove entity category break; } } productview = new productview( product ); // new productview modified product categorymap.get( productview.getcategory() ).add( productview ); // add entity it's category's list art.add(contentcontainer); }
and html:
<select class="categoryselect" wicket:id="categoryselectcomponent"> <optgroup label="category one"> <wicket:container wicket:id="onecategory"> <option wicket:id="option"></option> </wicket:container> </optgroup> <optgroup label="category two"> <wicket:container wicket:id="twocategory"> <option wicket:id="option"></option> </wicket:container> </optgroup> <optgroup label="category three"> <wicket:container wicket:id="threecategory"> <option wicket:id="option"></option> </wicket:container> </optgroup> </select>
any idea?
call #setrecreatechoices(true) on selectoptions.
Comments
Post a Comment