Skip to content

Instantly share code, notes, and snippets.

@happyrobots
Created January 14, 2011 22:31
Show Gist options
  • Save happyrobots/780412 to your computer and use it in GitHub Desktop.
Save happyrobots/780412 to your computer and use it in GitHub Desktop.
[Managed Bean] Making Primefaces 2.2.RC1 Data Table Filtering works with multiple selection mode.
// .. import statements
// ViewScoped is important for AJAX
@ManagedBean
@ViewScoped
public class SomeBean {
private T[] selectedElementsFromTheList;
private T[] copyOfSelectedElementsFromTheList;
private List<T> theList;
// ... a constructor, get and set methods...
public T[] getSelectedElementsFromTheList() {
return copyOfSelectedElementsFromTheList; // don't care about the default.
}
public void setSelectedElementsFromTheList(T[] selectedElementsFromTheList) {
this.selectedElementsFromTheList = selectedElementsFromTheList;
}
public List<T> getTheList() {return theList;}
public void addAnElement(SelectEvent event) {
int selectedLength = selectedElementsFromTheList.length;
if (copyOfSelectedElementsFromTheList == null ||
copyOfSelectedElementsFromTheList.length == 0) {
copyOfSelectedElementsFromTheList = new T[selectedLength];
System.arraycopy(selectedElementsFromTheList, 0,
copyOfSelectedElementsFromTheList, 0, selectedLength);
} else {
// event.getObject().getClass() returns the Object type
T selectedElement = (T) event.getObject();
T[] temp = copyOfSelectedElementsFromTheList;
int oldLength = temp.length, newLength = oldLength + 1;
copyOfSelectedElementsFromTheList = new T[newLength];
System.arraycopy(temp, 0, copyOfSelectedElementsFromTheList, 0, oldLength);
copyOfSelectedElementsFromTheList[oldLength] = selectedElement;
}
}
public void removeAnElement(UnselectEvent event) {
T unselectedElement = (T) event.getObject();
int numBackups = copyOfSelectedElementsFromTheList.length;
for (int i = 0; i < numBackups; ++i) {
if (copyOfSelectedElementsFromTheList[i].getId() ==
unselectedElement.getId()) {
numBackups--;
T[] temp = copyOfSelectedElementsFromTheList;
copyOfSelectedElementsFromTheList = new T[numBackups];
System.arraycopy(temp, 0, copyOfSelectedElementsFromTheList, 0, i);
System.arraycopy(temp, i + 1, copyOfSelectedElementsFromTheList, i, numBackups - i);
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment