Selecting a subset of Objects in a WORepetition

Often you need to allow a user to select a subset of items from a WORepetition to be able perform an action on them. This example will do that for you:

public Object item; // The "item" binding on your WORepetition
public NSMutableArray selections = new NSMutableArray();
 
public void setSelectedItem(boolean selected) {
    boolean hasItem = selections.containsObject(item);
    if (selected) {
        if (! hasItem) {
            selections.addObject(item);
        }
    } else if (hasItem) {
        selections.removeObject(item);
    }
}
	
public boolean selectedItem() {
    return selections.containsObject(item);
}

Add a WOCheckbox to your WORepetition and bind its checked binding to selectedItem. Any item selected will be added to the selections array when the form is submitted.

6 thoughts on “Selecting a subset of Objects in a WORepetition

  1. Very helpful, thanks! Lately I’ve just been using popup buttons with “Add” and “Remove” links in a repetition…

  2. Instead of an NSMutableArray (collection accessible by index), you could use an NSMutableSet (collection accessible by hash/identity), ideal if all you want is to verify if an object is contained in a collection.

  3. I have a question if you don’t mind.
    I am trying to do following:

    Once form submits to AddItems method, which just prints all the items. Now for the worepetition list I use NSMutableArray and for item I use string.

    So first I add couple of string to the array, this way worepetition outputs couple of wotextfields, then I edit fields but once I submit this form values in the strings ( in the array ) don’t change…

    Can you tell me why and how can I fix it?
    Thank you.

  4. Melr,

    Java string objects are not mutable.

    Your array is, but not the strings it contains.

    You would need to remove the current string from the array and replace it with the new one.

  5. Okay, I understand. So once I submit woform and let’s say method called:
    public WOActionResults AddItem() {
    ERXApplication.log.info(“TEST => ” + myArray.lastObject().toString());
    return null;
    }

    I have to replace objects in myArray. The problem I don’t understand is, how do I get values of submitted wotextfields? They are mapped dynamically in the myArray…

    Sorry, for asking such a dull questions, I am just starting with WO.

Comments are closed.