Programatically creating a WODisplayGroup

I like WODisplayGroups. OK, call me crazy – or lazy – but I like them alot. However, I do find that I often need more control over them than the drag and drop creation gives me. So here is how I like to set up WODisplayGroups programatcially:

Just to prevent any potential confusion: This example demonstrates creating a WODisplayGroup from scratch, and will not work if you’ve created one by dragging an Entity from EOModeler into WebObjects Builder. – Thanks Alex!

Using this pattern setup the _displayGroup object

protected WODisplayGroup _displayGroup;
 
public WODisplayGroup displayGroup() {
	if (_displayGroup == null) {
		prepDisplayGroup();
		find();
	}
	return _displayGroup;
}
public void setDisplayGroup(WODisplayGroup dg) {
	_displayGroup = dg;
}

Add the prepDisplayGroup() method:

public void prepDisplayGroup() {
	_displayGroup = new WODisplayGroup();
	_displayGroup.setNumberOfObjectsPerBatch(10); 
	EOSortOrdering asc = EOSortOrdering.sortOrderingWithKey(
			"keyToSortOn", 
			EOSortOrdering.CompareAscending);
	NSArray ordering = new NSArray(new Object[] {asc});
	_displayGroup.setSortOrderings(ordering);
}

The WODisplayGroup API is here

Finally add the find() method:

public void find() {
	NSArray foundObjects = //fetch your objects here
	displayGroup().setObjectArray(foundObjects);
}

In my find() method I usually take advantage of the objectsFor... methods that can be generated by EOGenerator. These wrap your EOModel’s fetchSpecs in static methods in your EO’s, very cool. Take a look at the EOGenerator EOGJavaSource.eotemplate for more details. Update: I’ve blogged more details about my EOGenerator templates here

I often base my search pages on a common super class that includes a displayGroup along with variables for setting objects per batch and a search string amongst other things.

3 thoughts on “Programatically creating a WODisplayGroup

  1. Hello,

    I am trying to use a WODisplayGroup with a WOBatchNavigationBar which works really well. The small problem that I am running into is that I am “Filtering” the WODisplayGroup in memory with search criteria that the user supplies using the WODisplayGroup.qualifyDisplayGroup(); This looks as though it is returning the correct results, except that the WOBatchNavBar is not updating the number of Objects retrieved, it still has the original number of EO’s retrieved from the Original Fetch.

    Any idea?

  2. Saïd,

    Try using qualifyDataSource instead. The object count comes from WODisplayGroup.allObjects.count. Using qualifyDisplayGroup will not affect the allObjects array (you are simply filtering it in memory), qualifyDataSource will qualify the WODisplayGroup’s dataSource, so the allObjects array will be smaller.

    ;david

  3. Hi ,

    I want to to display data from 2 different table . How can , I use the WODisplay group to do that.

    Regards
    Vaibhav

Comments are closed.