wicket , build on every time RepeatingView is called -
i have copied fromwicket site listeditor component support adding item in run time:
public abstract class listeditor<t> extends repeatingview implements iformmodelupdatelistener { list<t> items; public listeditor(string id, imodel<list<t>> model) { super(id, model); setoutputmarkupid(true); } protected abstract void onpopulateitem(listitem<t> item); public void additem(t value) { items.add(value); listitem<t> item = new listitem<t>(newchildid(), items.size() - 1); add(item); onpopulateitem(item); } @override protected void onbeforerender() { if (!hasbeenrendered()) { list<t> modelobject = getmodelobject(); if (modelobject != null) { items = new arraylist<t>(modelobject); (int = 0; < items.size(); i++) { listitem<t> li = new listitem<t>(newchildid(), i); add(li); onpopulateitem(li); } } else { items = new arraylist<t>(); } } super.onbeforerender(); } @override public void updatemodel() { setmodelobject(items); } /** * indicates whether or not item can removed, use of * {@link removebutton} * * @param items * @param item * @return */ public boolean canremove(list<t> items, t item) { return true; } @suppresswarnings("unchecked") final boolean checkremove(listitem<?> item) { list<t> list = collections.unmodifiablelist(items); listitem<t> li = (listitem<t>) item; return canremove(list, li.getmodelobject()); } /** * gets model * * @return model */ @suppresswarnings("unchecked") public final imodel<list<t>> getmodel() { return (imodel<list<t>>) getdefaultmodel(); } /** * sets model * * @param model */ public final void setmodel(imodel<list<t>> model) { setdefaultmodel(model); } /** * gets model object * * @return model object */ @suppresswarnings("unchecked") public final list<t> getmodelobject() { return (list<t>) getdefaultmodelobject(); } /** * sets model object * * @param object */ public final void setmodelobject(list<t> object) { setdefaultmodelobject(object); } }
problem : want every time refresh called build 0 component , when ever add item, how can achive ?
you can use ajax calls add items normal listview well. make sure item added added underlying list (typically using loadabledetachablemodel
) in ajaxcall, refresh entire listview adding ajaxrequesttarget
, setting setreuseitems
false.
something like:
loadabledetachablemodel<list<myobject>> mylistmodel = new loadabledetachablemodel<list<myobject>>() { public list<myobject> load() { return generatelistdynamically(); //or whatever method use retrieve list including items you've added } } listview<myobject> items = new listview("items", mylistmodel); items.setreuseitems(false); add(items); //a method call in onclick of link adds or removes item. public void onitemadd(ajaxrequesttarget target) { //should accessible, maybe make member of page or target.add(items); }
Comments
Post a Comment