• Home
  • RSS Feed
  • Register
  • 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.

  • Bookmark

Tags: Ajax, Java, Wicket
Filed under Ajax, Java, Wicket |



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.



Archives

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

Xebia Sites

  • Xebia Corporate
  • Xebia France
  • Xebia India

Categories

  • Java (233)
  • Agile (100)
  • General (50)
  • Performance (37)
  • Hibernate (35)
  • Podcast (31)
  • Testing (30)
  • Scrum (27)
  • Spring (24)
  • Project Management (22)
  • Architecture (22)
  • SOA (19)
  • Flex (17)
  • Maven (15)
  • Eclipse (14)
  • JPA (13)
  • Quality Assurance (12)
  • Groovy (12)
  • Articles (11)
  • Grails (11)

Tag Cloud

    Agile Awareness Workshop Maven Java Lean Scrum Agile Testing IntelliJ Hibernate fitnesse Groovy Ajax Xebia plugin Performance Eclipse offshore SOA distributed qcon Introduction to Agile Semantic Web OutOfMemoryError JavaOne Closures Poppendieck Grails Seam sutherland offshoring