herebebeasties.com
Alastair Maw
Lists
Add Remove Sort
What things?
Anything.
Type of data
String JPEG le ACME widget
Component
Label Image AcmeWidgetPanel
ListView
<div wicket:id="item"> <span wicket:id="name"> Name goes here </span> </div>
List<String> employeeNames = getEmployeeNames(); add(new ListView("item", employeeNames) { public void populateItem(ListItem item) { item.add(new Label("name", item.getModel())); } });
ListEditors ListView
<div wicket:id="container"> <div wicket:id="item"> <div class="handle">Drag me</div> <a wicket:id="deleteLink">Delete</a> <div wicket:id="component">Item goes here</div> </div> </div> container.add(new ListView("item", listOfStuff) { public void populateItem(ListItem item) { item.add(new Link("deleteLink") { public void onClick() { /* ... */ } }); IModel model = item.getModel(); item.add(getItemComponent("component", model)); } });
The behavior
protected void respond(AjaxRequestTarget target) { // Get the new order from the GET param array. String[] newOrder = getRequest().getParameters( container.getMarkupId() + "[]"); // Reorder the backing java.util.List according. reorderList(newOrder); // Repaint the list and recreate the JS Sortable. target.addComponent(listEditorContainer); target.appendJavascript("createSortable('" + container.getMarkupId() + "', '" + getCallbackUrl() + "');"); }
The behavior
We need to run the createSortable() JavaScript when we rst render the page. Behaviors have a method renderHead() which lets you add JS, CSS, etc. to a page.
The behavior
public void renderHead(IHeaderResponse response) { super.renderHead(response); response.renderOnDomReadyJavascript( "createSortable('" + container.getMarkupId() + "', '" + getCallbackUrl() + "');" ); }
Wrinkles
We want handles for drag and drop, so list items can be clickable. This is done with an extra handle param in the options to the JS Sortable.create() call. We need to add a bunch of CSS classes to the elements so we can style the drag handles, etc.
What do we have?
~10 lines of HTML. ~10 lines of JavaScript. ~80 lines of Java. A smidgin of CSS. A one-line properties le if you want to internationalize the Add new link.
Questions?
More talks and source code are available at: http://londonwicket.org/ For more information about Wicket: http://wicket.apache.org