The code is not on any repo outside of myco's SVN repo. As I mentioned, it's
based on a LabeledAjaxLinkPanel which is nothing but a AjaxLink with a label
so you'd have to refactor some of it.
Anyhow, here's the Java code:
package
com.mycom.console.platform.components.markup.html.repeater.data.table;
import java.util.Collection;
import org.apache.wicket.ajax.AjaxRequestTarget;
import
org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.util.io.IClusterable;
import
com.mycom.console.platform.components.ajax.markup.html.basic.LabeledAjaxLinkPanel;
/**
* Panel holding on to the label indicator and action links to select all or
clear all items filtered in a data table.
*
* @param <T> Type of elements in the collection used with this panel.
*/
public abstract class SelectAllPanel<T> extends Panel {
private static final long serialVersionUID = 1L;
private DataTable<?, ?> dataTable;
private Collection<T> selections;
private SelectAll selectAll = new SelectAll();
/**
* Constructs a new panel holding on to the label indicator and action
links to select all or clear all items filtered in a data table.
* <br><br>
* The visibility of this panel is controlled by the user's selection as
defined in the provided <code>selections</code> collection.
*
* @param id {@link Panel} id.
* @param dataTable {@link DataTable} associated with this panel.
* @param selections {@link Collection} of elements based on which
the indicators and action links operate.
*/
public SelectAllPanel(String id, DataTable<?, ?> dataTable,
Collection<T> selections) {
super(id);
this.dataTable = dataTable;
this.selections = selections;
selectAll.setSelected(selections.size());
selectAll.setTotal(dataTable.getDataProvider().size());
setOutputMarkupPlaceholderTag(true);
}
@Override
protected void onInitialize() {
super.onInitialize();
final IModel<SelectAll> selectAllModel = new
CompoundPropertyModel<SelectAll>(selectAll);
IModel<String> labelModel = new AbstractReadOnlyModel<String>() {
private static final long serialVersionUID = 1L;
@Override
public String getObject() {
if(selectAll.isAllSelected()) {
return new StringResourceModel(
"SelectAllPanel.selection.all", selectAllModel, "All
${total} items selected."
).getObject();
} else {
return new StringResourceModel(
"SelectAllPanel.selection", selectAllModel,
"${selected} items selected."
).getObject();
}
}
};
add(new Label("selection", labelModel));
IModel<String> selectLinkModel = new StringResourceModel(
"SelectAllPanel.select", selectAllModel, "Select all ${total}
items."
);
add(new LabeledAjaxLinkPanel<Void>("selectLink", selectLinkModel) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
selectAll(target);
}
@Override
protected void manipulateLink(Link<Void> link) {
link.setVisible(!selectAll.isAllSelected());
}
});
add(new LabeledAjaxLinkPanel<Void>("clearLink", new
ResourceModel("SelectAllPanel.clear")) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
clearAll(target);
}
});
}
@Override
protected void onConfigure() {
super.onConfigure();
selectAll.setSelected(selections.size());
selectAll.setTotal(dataTable.getDataProvider().size());
setVisible(!selections.isEmpty());
}
/**
* Callback for when the user has selected the <code>Select all 12345
items.</code> link.
*
* @param target {@link AjaxRequestTarget} to use for Ajax refresh.
*/
abstract public void selectAll(AjaxRequestTarget target);
/**
* Callback for when the user has selected the <code>Clear
selection.</code> link.
*
* @param target {@link AjaxRequestTarget} to use for Ajax refresh.
*/
abstract public void clearAll(AjaxRequestTarget target);
/**
* POJO used as the model object for the labels on the {@link
SelectAllPanel} parent panel.
*/
public static class SelectAll implements IClusterable {
private static final long serialVersionUID = 1L;
private int selected;
private long total;
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public boolean isAllSelected() {
return (selected == total);
}
}
}
Here's the HTML for it:
<html xmlns:wicket="http://wicket.apache.org">
<wicket:panel>
[All/12345 items selected.]
[Select all 12345 items.]
[Clear selection.]
</wicket:panel>
</html>
A quick run-down, you add this SelectAllPanel inside a table toolbar (I add
it to the top toolbar) and then have your page that hosts the table or
whoever you want implement the abstract methods to perform the actual
selection.
Here's an example of how I used it:
addTopToolbar(new AbstractAjaxNavigationToolbar(this) {
private static final long serialVersionUID = 1L;
@Override
protected void updateComponents(AjaxRequestTarget target) {
setSelectAll(getSelectAll());
}
@Override
protected Panel newCustomToolbarPanel(String toolbarPanelId, final
DataTable<?, ?> table) {
selectAllPanel = new SelectAllPanel<Licensee>(toolbarPanelId, table,
selectedLicensee) {
private static final long serialVersionUID = 1L;
@Override
public void selectAll(AjaxRequestTarget target) {
addAllLicensee();
// refreshTable(target);
}
@Override
public void clearAll(AjaxRequestTarget target) {
removeAllLicensee();
// refreshTable(target);
}
};
return selectAllPanel;
}
});
private void addAllLicensee() {
IDataProvider<Licensee> dataProvider = table.getDataProvider();
Iterator<? extends Licensee> iter = dataProvider.iterator(0,
dataProvider.size());
while(iter.hasNext()) {
addSelectedObject(iter.next());
}
}
private void removeAllLicensee() {
selectedLicensee.clear();
setSelectAll(false);
}
// Used to grey out the Select All checkbox in the header row (not shown
here)
private boolean areAllOnPageSelected() {
boolean allOnPageSelected = true;
for(Licensee l : table.getRowsOnPage(table.getCurrentPage())) {
if(!containsObject(l)) {
allOnPageSelected = false;
break;
}
}
return allOnPageSelected;
}
-----
~ Thank you,
[email protected]
--
View this message in context:
http://apache-wicket.1842946.n4.nabble.com/how-to-add-select-deselect-all-checkbox-to-wicket-DataTable-tp4660270p4660344.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]