wicket refresh listView in different panel using ajax

Giorgio

Hy guys I'm newbie in Wicket and I'm start playing this framework. I know will be a very useless example but I try to use that for learn wicket basics. This is my situation:

I get a page IndexPage.java that is composed from basically 2 panel, the first one is ListCheesePanel.java ( it basically show a list of cheese with price,etc and a button for to add to the cart the selected cheese) and the second one is CartPanel.java that is essentially a cart wherein you have all the selected cheeses and you can proceed to checkOut.

I would basically add on cheese from my list to the cart using ajax behaviour:

ListCheesePanel.java

public abstract class ListCheesePanel extends GenericPanel<List<Cheese>>{

    private static final long serialVersionUID = 1L;

    public ListCheesePanel(String id,final IModel<List<Cheese>> listCheeseModel) {
        super(id,listCheeseModel);
        PageableListView<Cheese> cheeses = new PageableListView<Cheese>("cheeses", listCheeseModel, 4) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final ListItem<Cheese> item) {

                Cheese cheese = (Cheese) item.getModelObject();
                item.addOrReplace(new Label("name", Model.of(cheese.getName())));
                item.addOrReplace(new Label("description", Model.of(cheese.getDescription())));
                item.addOrReplace(new Label("price", Model.of("$ " + cheese.getPrice())));
                item.addOrReplace(new AjaxFallbackLink<Cheese>("add",item.getModel()) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void onClick(AjaxRequestTarget target) {
                        elementClicked(target, item.getModelObject());
                    }
                });
            }
        };
        addOrReplace(cheeses);
        addOrReplace(new PagingNavigator("navigator", cheeses));
    }

    protected abstract void elementClicked(AjaxRequestTarget target,Cheese cheese);
}

elementClicked is a method that must be ovverride in order to indicate the cheese and the target.

CartPanel.java

public abstract class CartPanel extends GenericPanel<Cart>{

    private static final long serialVersionUID = 1L;

    private WebMarkupContainer cartContainer;
    private ListView<Cheese> cheesesList;   
    private Link<Cart> checkOutLink;
    private boolean checkOutButtonVisibility;

    public CartPanel(String id,final IModel<Cart> cartModel,final boolean checkOutButtonVisibility) {
        super(id,cartModel);
        this.checkOutButtonVisibility=checkOutButtonVisibility;
        cartContainer = new WebMarkupContainer("cartContainer");
        cartContainer.setOutputMarkupId(true);
        addOrReplace(cartContainer);
        cheesesList = new ListView<Cheese>("cart" , getModelObject().getCheeses()){

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final ListItem<Cheese> item) {
                Cheese cheese = item.getModelObject();
                item.addOrReplace(new Label("name",cheese.getName()));
                item.addOrReplace(new Label("price",cheese.getPrice()));
                item.addOrReplace(new Link<Cheese>("remove",item.getModel()) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void onClick() {
                        removeElement(item.getModelObject());
                    }
                });
            }
        };

        cheesesList.setOutputMarkupId(true);        
        cartContainer.addOrReplace(cheesesList);
        cartContainer.addOrReplace(new Label("total",new PropertyModel<Long>(cartModel.getObject(), "total")));

        checkOutLink = new Link<Cart>("checkOut",getModel()){

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                setResponsePage(new CheckOutPage(getModel()));
            }

            @Override
            protected void onConfigure() {
                super.onConfigure();
                Cart cart = cartModel.getObject();
                List<Cheese> cheeseList = cart.getCheeses();
                setVisible(isCheckOutButtonVisibility() && !cheeseList.isEmpty());
            }

       };    

       addOrReplace(checkOutLink);
    }

    protected abstract void removeElement(Cheese modelObject);

    /* Getters and Setters */

    public boolean isCheckOutButtonVisibility() {
        return checkOutButtonVisibility;
    }
}

IndePage.java

public class IndexPage extends CheeseTemplate {

    private static final long serialVersionUID = 1L;
    private static List<Cheese> cheeses = Arrays.asList(
            new Cheese("Gouda", "Gouda is a yellowish Dutch[...]", 1.65),
            new Cheese("Edam", "Edam (Dutch Edammer) is a D[...]", 1.05),
            new Cheese("Maasdam", "Maasdam cheese is a Dutc[...]", 2.35),
            new Cheese("Brie", "Brie is a soft cows' milk c[...]", 3.15),
            new Cheese("Buxton Blue", "Buxton Blue cheese i[...]", 0.99),
            new Cheese("Parmesan", "Parmesan is a grana, a [...]", 1.99),
            new Cheese("Cheddar", "Cheddar cheese is a hard[...]", 2.95),
            new Cheese("Roquefort", "Roquefort is a ewe's-m[...]", 1.67),
            new Cheese("Boursin", "Boursin Cheese is a soft[...]", 1.33),
            new Cheese("Camembert", "Camembert is a soft, c[...]", 1.69),
            new Cheese("Emmental", "Emmental is a yellow, m[...]", 2.39),
            new Cheese("Reblochon", "Reblochon is a French [...]", 2.99));

    private IModel<List<Cheese>> allCheeseListModel;
    private IModel<Cart> cartModel;
    private Cart cart;

    public IndexPage() {
        super();
        allCheeseListModel = new ListModel<Cheese>(cheeses);
        cart=new Cart();
        cartModel = new Model<Cart>(cart);
    }

    public IndexPage(IModel<Cart> cartModel) {
        super();
        allCheeseListModel = new ListModel<Cheese>(cheeses);
        this.cartModel=cartModel;
    }

    @Override
    public Component getMainPanel() {
        System.out.println("getMainPanel started");

        return new ListCheesePanel(MAIN_PANEL_WICKET_ID,allCheeseListModel){

            private static final long serialVersionUID = 1L;

            @Override
            protected void elementClicked(AjaxRequestTarget target,Cheese cheese) {
                Cart cart = cartModel.getObject();
                cart.getCheeses().add(cheese);
                target.add(?????); // How can i get the cart container to update????
            }
        };
    }

    @Override
    public Component getRightMenuPanel() {
        CartPanel cartPanel = new CartPanel(RIGHT_MENU_PANEL_WICKET_ID,cartModel,true) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void removeElement(Cheese cheese) {
                Cart cart = cartModel.getObject(); 
                cart.getCheeses().remove(cheese);
            }
        };
        cartPanel.setOutputMarkupId(true);
        return cartPanel;
    }

}

My question is:

How can I get the reference of the component to update(so I think is cartContainer in my example because I can't add directly a listView component to the target) if this component is located in another panel??

Thanks in advance

RobAu

What I would do, and really like, is to work with events.

I define a global AjaxUpdateEvent like this:

AjaxUpdateEvent

public class AjaxUpdateEvent
{
    public AjaxRequestTarget target;

    public AjaxUpdateEvent( AjaxRequestTarget target )
    {
        this.target = target;

    }

    public AjaxRequestTarget getTarget()
    {
        return this.target;
    }

}

Then, I subclass events that I'm interested in, for example CheeseAddedEvent extends AjaxUpdateEvent

Then, all the components that need to do something when an cheese is added have a onEvent()

OnEvent

@Override
public void onEvent( IEvent<?> event )
{
    Object payload = event.getPayload();

    @SuppressWarnings( "rawtypes" )
    Class clazz = payload.getClass();

    //or use isAssignableFrom() or instanceof or any method you like
    if ( CheeseAddedEvent.class.equals( clazz ) )
    {
        CheeseAddedEvent e = ( (CheeseAddedEvent) payload );
        e.target.add( this );
    }
    super.onEvent(event);
}

Usage

....
public void onClick(AjaxRequestTarget target)
{
    this.send( this.getPage(), Broadcast.BREADTH, new CheeseAddedEvent( target ) );
}
....

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Refresh panel in Wicket with BootstrapDownloadLink

From Dev

Wicket - refresh component using AJAX - junk after document element

From Dev

Apache Wicket Panel's TextFields dont refresh after closing the Modal Window

From Dev

Wicket dynamic ListView - default item won't get updated by Ajax

From Dev

Refresh Listview after guest was created in different form

From Dev

Wicket Panel inside of a Fragment

From Dev

Title for Tabbed Panel in wicket

From Dev

passing parameters to wicket panel

From Dev

How to refresh div using ajax refresh?

From Dev

How to refresh an element using AJAX?

From Dev

How to refresh an element using AJAX?

From Dev

how to select using a wrap panel as the item panel for listview

From Dev

how to refresh the listView using the Cursor Adapter

From Dev

NullPointerException in viewHolder during ListView refresh using notifyDataSetChanged

From Dev

Update one Wicket Panel from clicking an item in another Wicket Panel

From Dev

Wicket: Submit Form in a Panel from another Panel

From Dev

Wicket: Submit Form in a Panel from another Panel

From Dev

Wicket: FileUploadField with ListView

From Dev

Wicket replacing items in ListView

From Dev

Wicket: FileUploadField with ListView

From Dev

Wicket: add TextField / Panel dynamically

From Dev

How to handle changes in a form with a model-update using Wicket and AJAX

From Dev

R Shiny: automatically refreshing a main panel without using a refresh button

From Dev

How to render a panel using ajax in submenu primefaces

From Dev

C# - ListView won't refresh from different form

From Dev

ajax on submit no refresh form using php

From Dev

How to refresh JS datatables using AJAX

From Dev

How to refresh JS datatables using AJAX

From Dev

WCS AJAX refresh using dojo and controller

Related Related

  1. 1

    Refresh panel in Wicket with BootstrapDownloadLink

  2. 2

    Wicket - refresh component using AJAX - junk after document element

  3. 3

    Apache Wicket Panel's TextFields dont refresh after closing the Modal Window

  4. 4

    Wicket dynamic ListView - default item won't get updated by Ajax

  5. 5

    Refresh Listview after guest was created in different form

  6. 6

    Wicket Panel inside of a Fragment

  7. 7

    Title for Tabbed Panel in wicket

  8. 8

    passing parameters to wicket panel

  9. 9

    How to refresh div using ajax refresh?

  10. 10

    How to refresh an element using AJAX?

  11. 11

    How to refresh an element using AJAX?

  12. 12

    how to select using a wrap panel as the item panel for listview

  13. 13

    how to refresh the listView using the Cursor Adapter

  14. 14

    NullPointerException in viewHolder during ListView refresh using notifyDataSetChanged

  15. 15

    Update one Wicket Panel from clicking an item in another Wicket Panel

  16. 16

    Wicket: Submit Form in a Panel from another Panel

  17. 17

    Wicket: Submit Form in a Panel from another Panel

  18. 18

    Wicket: FileUploadField with ListView

  19. 19

    Wicket replacing items in ListView

  20. 20

    Wicket: FileUploadField with ListView

  21. 21

    Wicket: add TextField / Panel dynamically

  22. 22

    How to handle changes in a form with a model-update using Wicket and AJAX

  23. 23

    R Shiny: automatically refreshing a main panel without using a refresh button

  24. 24

    How to render a panel using ajax in submenu primefaces

  25. 25

    C# - ListView won't refresh from different form

  26. 26

    ajax on submit no refresh form using php

  27. 27

    How to refresh JS datatables using AJAX

  28. 28

    How to refresh JS datatables using AJAX

  29. 29

    WCS AJAX refresh using dojo and controller

HotTag

Archive