DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
| Excerpt | ||
|---|---|---|
| ||
How to use checkboxes to select items in a listview |
Using checkboxes to select items in a listview is a common thing to do. For example, you want to select items to delete from a list.
The easiest and neatest way to do this is probably using a org.apache.wicket.markup.html.form.CheckGroup
Another One way to do this, is to wrap your 'business objects' so that it has an additional boolean property that you can use in your form. The following example uses such a wrapper, where the 'name' field is the wrapped object.
...
| Code Block |
|---|
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import wicket.IFeedback;
import wicket.markup.html.WebPage;
import wicket.markup.html.basic.Label;
import wicket.markup.html.form.CheckBox;
import wicket.markup.html.form.Form;
import wicket.markup.html.list.ListItem;
import wicket.markup.html.list.ListView;
import wicket.markup.html.panel.FeedbackPanel;
import wicket.model.PropertyModel;
/** input web page. */
public class FormInput extends WebPage
{
public FormInput()
{
final FeedbackPanel feedback = new FeedbackPanel("feedback");
add(feedback);
add(new InputForm("inputForm", feedback));
}
/** form for processing the input. */
private class InputForm extends Form
{
// holds NameWrapper elements
private List data;
public InputForm(String name, IFeedback feedback)
{
super(name, feedback);
// add some dummy data
data = new ArrayList();
data.add(new NameWrapper("one"));
data.add(new NameWrapper("two"));
data.add(new NameWrapper("three"));
data.add(new NameWrapper("four"));
// add a nested list view; as the list is nested in the form, the form will
// update all FormComponent childs automatically.
add(ListView listView = new ListView("list", data)
{
protected void populateItem(ListItem item)
{
NameWrapper wrapper = (NameWrapper)item.getModelObject();
item.add(new Label("name", wrapper.getName()));
item.add(new CheckBox("check", new PropertyModel(wrapper, "selected")));
}
};
listView.setReuseItems(true);
add(listView);
}
public void onSubmit()
{
info("data: " + data); // print current contents
}
}
}
|
...