• Home
  • RSS Feed
  • Log in

Wicket – Updating ListViews using an AjaxLink
Posted by Mischa Dasberg mid-morning: June 4th, 2008

Consider the following senario: we want to display some data in a table like manner, and we want it to update when we click on a link or button. We do not want to do a complete page refresh, we want it in an ajax way. Also we would like the modify the css for each cell.

In wicket you can use a ListView iterate over a List of Objects and display them in a table like manner.
This blog describes how you can update ListView data and modify the css for each cell.

So how do we start?

Well first we need to create a ListView. Lets call it view.
But before we do that we need a List of objects we want to display. So lets create it.
I use a List of TestObject's.

As you can see below, this isn't very hard.

 
private ListView view;
private List<TestObject> objects;
 
objects = Arrays.asList(new TestObject[] {
    new TestObject("one", true),
    new TestObject("two", false),
    new TestObject("three", true)});
 
view = new ListView("view", objects) {
    @Override
    protected void populateItem(ListItem item) {
        Label label = new Label("label", new PropertyModel(item.getModel(), "name"));
        label.setOutputMarkupId(true);
        item.add(label);
    }
};
 

Next we will create an IndicatingAjaxFallbackLink which is basically a AjaxLink that shows an indicator.

 
private IndicatingAjaxFallbackLink link;
 
link = new IndicatingAjaxFallbackLink("link") {
    @Override
    public void onClick(AjaxRequestTarget target) {
        objects = Arrays.asList(new TestObject[] {
            new TestObject("four", false),
            new TestObject("five", true),
            new TestObject("six", false)});
        view.setList(objects);
        target.addChildren(view, Label.class);
    }
};
 

Thats all. Now see that we add a new List of objects to the ListView. To make sure the ListView is updated we add

target.addChildren(view, Label.class);

instead of

target.addComponent(view);

. As a ListView is a repeater, we cannot updated it, but we can update components in a ListView.

So now we can update a ListView, but how do we change the css for each field?

We add an AttributeModifier. In the populateItem method show above we just add the following

 
label.add(new AttributeModifier("class", true, new PropertyModel(item.getModel(), "odd") {
    @Override
    public Object getObject() {
        return ((Boolean) super.getObject()) ? "odd" : "even";
    }
}));
 

The getObject method is overridden, because the method getOdd() on the TestObject returns a boolean. So instead of returning true or false, it now returns "odd" or "even" which are css class styles.

That's it.

I have attached the example project here so you can look into the code yourself.

  • Share/Bookmark

Tags: Ajax, Java, Wicket
Filed under Ajax, Java, Wicket | 10 Comments »



10 Responses to “Wicket – Updating ListViews using an AjaxLink”



    50 Excellent AJAX Tutorials | Tutorials | Smashing Magazine Says:
    Posted at: October 16, 2008 at 11:47 pm

    [...] Wicket – Updating ListViews Using an AjaxLink Create a table that will update without a full page refresh when a link is clicked. [...]



    Useful AJAX Tutorials | Neurosoftware web dev Says:
    Posted at: October 17, 2008 at 8:59 am

    [...] Wicket – Updating ListViews Using an AjaxLink Create a table that will update without a full page refresh when a link is clicked. [...]



    50+ Great Ajax Tutorial | Tech User, Blogger and Designers References Says:
    Posted at: October 21, 2008 at 1:35 pm

    [...] Wicket – Updating ListViews Using an AjaxLink Create a table that will update without a full page refresh when a link is clicked. [...]



    50 Excellent AJAX Tutorials | Evolution : weblog Says:
    Posted at: October 31, 2008 at 6:27 am

    [...] Wicket – Updating ListViews Using an AjaxLink Create a table that will update without a full page refresh when a link is clicked. [...]



    50 Excellent AJAX Tutorials | How2Pc Says:
    Posted at: December 19, 2008 at 9:37 am

    [...] Wicket – Updating ListViews Using an AjaxLinkCreate a table that will update without a full page refresh when a link is clicked. [...]



    50 Excellent AJAX Tutorials « Rohit Dubal Says:
    Posted at: February 16, 2009 at 11:38 am

    [...] Wicket – Updating ListViews Using an AjaxLink Create a table that will update without a full page refresh when a link is clicked. [...]



    Best Ajax Tutorials and Dynamic Solution for PHP ,Asp.net.Ruby On Rrails | Click On Tech Says:
    Posted at: February 23, 2009 at 3:18 pm

    [...] Wicket – Updating ListViews Using an AjaxLink Create a table that will update without a full page refresh when a link is clicked. [...]



    150 AJAX Tutorials » TemplateLite.com Says:
    Posted at: May 14, 2009 at 2:10 am

    [...] Wicket – Updating ListViews Using an AjaxLink – This tutorial shows how to create a table that will update without a full-page refresh when a link is clicked. [...]



    Best of 158 Ajax Framework,JavaScript Libraries and Toolkit Tutorials | Click On Tech Says:
    Posted at: May 14, 2009 at 12:23 pm

    [...] Wicket – Updating ListViews Using an AjaxLink – This tutorial shows how to create a table that will update without a full-page refresh when a link is clicked. [...]



    Ajax Frameworks,JavaScript Libraries and Toolkit Tutorials | Dev Techie Says:
    Posted at: May 15, 2009 at 4:46 am

    [...] Wicket – Updating ListViews Using an AjaxLink - This tutorial shows how to create a table that will update without a full-page refresh when a link is clicked. [...]



Leave a Reply

Click here to cancel reply.

Deployment automation for Java application running on Websphere, WebLogic and JBoss

Archives

  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009

Xebia Sites

  • Xebia Corporate
  • Xebia France
  • Xebia India

Categories

  • Java (282)
  • Agile (109)
  • General (50)
  • Testing (42)
  • Performance (42)
  • Hibernate (36)
  • Scrum (33)
  • Podcast (31)
  • Architecture (31)
  • Spring (28)
  • SOA (24)
  • Maven (22)
  • Project Management (22)
  • Middleware (23)
    • Deployment (14)
  • Flex (17)
  • JPA (17)
  • Eclipse (15)
  • Xebia Labs (15)
  • Quality Assurance (14)

Tag Cloud

    Agile Awareness Workshop Poppendieck Scala IntelliJ esb Spring Hibernate Groovy SOA Closures Seam Functional Programming JavaOne Ajax XML Java Testing Semantic Web Scrum qcon Architecture product owner Grails Lean Xebia Agile Maven Performance fitnesse Introduction to Agile