You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by "Mathias P.W Nilsson" <ma...@snyltarna.se> on 2008/05/05 19:23:37 UTC
Preserve form on ajax update
I've read this on the wicket wiki to add new component via ajax.
http://cwiki.apache.org/WICKET/how-to-repaint-a-listview-via-ajax.html
http://cwiki.apache.org/WICKET/how-to-repaint-a-listview-via-ajax.html
My problem is that if the user have added some values in the form and the
clicks the
ajax update link the form isn't preserved. How can I preserve the following
form
public class AttributePage extends BaseAdministrationPage {
private se.edgesoft.hairless.entities.item.Item item;
List<SubItem> subItems = new LinkedList<SubItem>();
List<Element> elements;
public List<SubItem> getSubItems() {
return subItems;
}
public AttributePage( se.edgesoft.hairless.entities.item.Item item ){
this.item = item;
elements = getCategoryDao().getElements(getItem().getSubCategory() );
final CSSFeedbackPanel panel = new CSSFeedbackPanel( "feedback" );
panel.setOutputMarkupId(true);
add( panel );
/**
final IModel subItemsModel = new LoadableDetachableModel(){
private static final long serialVersionUID = 1L;
protected Object load(){
return subItems;
}
};
*/
for( int i = 0; i < 10; i++ ){
SubItem subItem = new SubItem();
subItem.setItem(getItem());
subItems.add( subItem );
subItem.setSortOrder(i);
List<Attribute> attributes = new LinkedList<Attribute>();
for( Element element : getElements() ){
Attribute attribute = new Attribute();
attribute.setData( null );
attribute.setElement( element );
attribute.setSubItem(subItem);
attributes.add( attribute );
subItem.setAttributes(attributes);
}
}
final WebMarkupContainer itemContainer = new
WebMarkupContainer("itemContainer");
itemContainer.setOutputMarkupId(true);
itemContainer.add(getItemListView());
final Form attributeForm = new Form( "attributeForm" );
IndicatingAjaxButton ajaxBtn = new IndicatingAjaxButton("create",
attributeForm ){
private static final long serialVersionUID = 1L;
@Override
protected IAjaxCallDecorator getAjaxCallDecorator()
{
return new IAjaxCallDecorator()
{
private static final long serialVersionUID = 1L;
public CharSequence decorateScript(CharSequence script)
{
return "document.getElementById('"+ getMarkupId()
+"').disabled=true;"+script;
}
public CharSequence decorateOnFailureScript(CharSequence script)
{
return script+"document.getElementById('"+ getMarkupId()
+"').disabled=false;";
}
public CharSequence decorateOnSuccessScript(CharSequence script)
{
return script+"document.getElementById('"+ getMarkupId()
+"').disabled=false;";
}
};
}
protected void onSubmit(AjaxRequestTarget target, Form form){
this.setEnabled(false);
target.addComponent(this);
target.addComponent(panel);
se.edgesoft.hairless.entities.item.Item item = getItemDao().getItem(
getItem().getId() );
Long cachedBalance = new Long( 0 );
for( SubItem subItem : subItems ){
cachedBalance += subItem.getBalance();
subItem.setItem( item );
getItemDao().save( subItem );
for( Attribute attribute : subItem.getAttributes() ){
attribute.setSubItem(subItem);
getItemDao().save(attribute);
}
}
item.setCachedBalance(cachedBalance);
getItemDao().save(item);
this.setEnabled( true );
}
protected void onError(AjaxRequestTarget target, Form form)
{
target.addComponent(panel);
}
};
ajaxBtn.setOutputMarkupId( true );
attributeForm.add(itemContainer);
attributeForm.setOutputMarkupId( true );
attributeForm.add( ajaxBtn );
add( attributeForm );
add(new AjaxLink( "newAttributeLink" ){
private static final long serialVersionUID = 1L;
@Override
public void onClick( AjaxRequestTarget target ) {
target.addComponent(attributeForm);
newAttribute();
attributeForm.addOrReplace(itemContainer);
}
});
}
private void newAttribute(){
SubItem subItem = new SubItem();
subItem.setItem(getItem());
subItems.add( subItem );
subItem.setSortOrder(getSubItems().size() + 1);
List<Attribute> attributes = new LinkedList<Attribute>();
for( Element element : getElements() ){
Attribute attribute = new Attribute();
attribute.setData( null );
attribute.setElement( element );
attribute.setSubItem(subItem);
attributes.add( attribute );
subItem.setAttributes(attributes);
}
}
private ListView getItemListView(){
return new ListView("subItems", getSubItems() ){
private static final long serialVersionUID = 1L;
@Override
protected void populateItem(ListItem listItem ) {
final SubItem subItem = (SubItem) listItem.getModelObject();
TextField sortOrder = new TextField("sortOrder", new PropertyModel(
subItem, "sortOrder" ));
sortOrder.setRequired( true );
listItem.add( sortOrder );
TextField balance = new TextField("balance", new PropertyModel( subItem,
"balance" ));
balance.setRequired( true );
listItem.add(balance);
listItem.add( new ListView( "attributes" , subItem.getAttributes()){
private static final long serialVersionUID = 1L;
@Override
protected void populateItem(ListItem attributeItem ) {
Attribute attribute = (Attribute) attributeItem.getModelObject();
TextField attributeField = new TextField( "attribute", new
PropertyModel(attribute, "data" ) );
attributeField.add( StringValidator.lengthBetween(1, 100));
attributeField.setRequired( true );
attributeItem.add( attributeField );
}
});
}
};
}
public se.edgesoft.hairless.entities.item.Item getItem(){
return item;
}
public List<Element> getElements() {
return elements;
}
}
--
View this message in context: http://www.nabble.com/Preserve-form-on-ajax-update-tp17066683p17066683.html
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Preserve form on ajax update
Posted by Jeremy Thomerson <je...@thomersonfamily.com>.
I haven't looked at all of your code, but have you taken a look at
http://wicketframework.org/apidocs/wicket/ajax/form/AjaxFormComponentUpdatingBehavior.html
or
http://wicketframework.org/apidocs/wicket/ajax/form/AjaxFormSubmitBehavior.html
Maybe those will help you out.
Jeremy Thomerson
http://www.wickettraining.com
On Mon, May 5, 2008 at 12:23 PM, Mathias P.W Nilsson <ma...@snyltarna.se>
wrote:
>
> I've read this on the wicket wiki to add new component via ajax.
>
> http://cwiki.apache.org/WICKET/how-to-repaint-a-listview-via-ajax.html
> http://cwiki.apache.org/WICKET/how-to-repaint-a-listview-via-ajax.html
>
> My problem is that if the user have added some values in the form and the
> clicks the
> ajax update link the form isn't preserved. How can I preserve the
> following
> form
>
> public class AttributePage extends BaseAdministrationPage {
>
> private se.edgesoft.hairless.entities.item.Item item;
> List<SubItem> subItems = new LinkedList<SubItem>();
> List<Element> elements;
>
> public List<SubItem> getSubItems() {
> return subItems;
> }
>
> public AttributePage( se.edgesoft.hairless.entities.item.Item item
> ){
> this.item = item;
> elements =
> getCategoryDao().getElements(getItem().getSubCategory() );
> final CSSFeedbackPanel panel = new CSSFeedbackPanel(
> "feedback" );
> panel.setOutputMarkupId(true);
> add( panel );
>
> /**
> final IModel subItemsModel = new LoadableDetachableModel(){
> private static final long serialVersionUID = 1L;
> protected Object load(){
>
>
> return subItems;
> }
> };
> */
>
> for( int i = 0; i < 10; i++ ){
> SubItem subItem = new SubItem();
> subItem.setItem(getItem());
> subItems.add( subItem );
> subItem.setSortOrder(i);
> List<Attribute> attributes = new
> LinkedList<Attribute>();
> for( Element element : getElements() ){
> Attribute attribute = new Attribute();
> attribute.setData( null );
> attribute.setElement( element );
> attribute.setSubItem(subItem);
> attributes.add( attribute );
> subItem.setAttributes(attributes);
> }
> }
>
> final WebMarkupContainer itemContainer = new
> WebMarkupContainer("itemContainer");
> itemContainer.setOutputMarkupId(true);
> itemContainer.add(getItemListView());
>
> final Form attributeForm = new Form( "attributeForm" );
>
>
> IndicatingAjaxButton ajaxBtn = new
> IndicatingAjaxButton("create",
> attributeForm ){
>
> private static final long serialVersionUID = 1L;
>
> @Override
> protected IAjaxCallDecorator getAjaxCallDecorator()
> {
> return new IAjaxCallDecorator()
> {
> private static final long
> serialVersionUID = 1L;
> public CharSequence
> decorateScript(CharSequence script)
> {
> return
> "document.getElementById('"+ getMarkupId()
> +"').disabled=true;"+script;
> }
> public CharSequence
> decorateOnFailureScript(CharSequence script)
> {
> return
> script+"document.getElementById('"+ getMarkupId()
> +"').disabled=false;";
> }
> public CharSequence
> decorateOnSuccessScript(CharSequence script)
> {
> return
> script+"document.getElementById('"+ getMarkupId()
> +"').disabled=false;";
> }
> };
> }
>
> protected void onSubmit(AjaxRequestTarget target,
> Form form){
> this.setEnabled(false);
> target.addComponent(this);
> target.addComponent(panel);
> se.edgesoft.hairless.entities.item.Item
> item = getItemDao().getItem(
> getItem().getId() );
> Long cachedBalance = new Long( 0 );
> for( SubItem subItem : subItems ){
> cachedBalance +=
> subItem.getBalance();
> subItem.setItem( item );
> getItemDao().save( subItem );
> for( Attribute attribute :
> subItem.getAttributes() ){
>
> attribute.setSubItem(subItem);
>
> getItemDao().save(attribute);
> }
> }
> item.setCachedBalance(cachedBalance);
> getItemDao().save(item);
> this.setEnabled( true );
>
> }
>
> protected void onError(AjaxRequestTarget target, Form form)
> {
> target.addComponent(panel);
> }
> };
> ajaxBtn.setOutputMarkupId( true );
>
>
> attributeForm.add(itemContainer);
> attributeForm.setOutputMarkupId( true );
> attributeForm.add( ajaxBtn );
> add( attributeForm );
>
>
> add(new AjaxLink( "newAttributeLink" ){
> private static final long serialVersionUID = 1L;
>
> @Override
> public void onClick( AjaxRequestTarget target ) {
> target.addComponent(attributeForm);
> newAttribute();
> attributeForm.addOrReplace(itemContainer);
>
> }
>
> });
>
> }
>
> private void newAttribute(){
> SubItem subItem = new SubItem();
> subItem.setItem(getItem());
> subItems.add( subItem );
> subItem.setSortOrder(getSubItems().size() + 1);
> List<Attribute> attributes = new LinkedList<Attribute>();
> for( Element element : getElements() ){
> Attribute attribute = new Attribute();
> attribute.setData( null );
> attribute.setElement( element );
> attribute.setSubItem(subItem);
> attributes.add( attribute );
> subItem.setAttributes(attributes);
> }
> }
>
> private ListView getItemListView(){
> return new ListView("subItems", getSubItems() ){
>
> private static final long serialVersionUID = 1L;
>
> @Override
> protected void populateItem(ListItem listItem ) {
>
> final SubItem subItem = (SubItem)
> listItem.getModelObject();
> TextField sortOrder = new
> TextField("sortOrder", new PropertyModel(
> subItem, "sortOrder" ));
> sortOrder.setRequired( true );
> listItem.add( sortOrder );
>
> TextField balance = new
> TextField("balance", new PropertyModel( subItem,
> "balance" ));
> balance.setRequired( true );
> listItem.add(balance);
>
> listItem.add( new ListView( "attributes" ,
> subItem.getAttributes()){
>
> private static final long
> serialVersionUID = 1L;
>
> @Override
> protected void
> populateItem(ListItem attributeItem ) {
> Attribute attribute =
> (Attribute) attributeItem.getModelObject();
>
> TextField attributeField =
> new TextField( "attribute", new
> PropertyModel(attribute, "data" ) );
> attributeField.add(
> StringValidator.lengthBetween(1, 100));
> attributeField.setRequired(
> true );
> attributeItem.add(
> attributeField );
> }
>
> });
> }
>
>
>
> };
> }
>
>
> public se.edgesoft.hairless.entities.item.Item getItem(){
> return item;
> }
>
> public List<Element> getElements() {
> return elements;
> }
> }
>
> --
> View this message in context:
> http://www.nabble.com/Preserve-form-on-ajax-update-tp17066683p17066683.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>