Wednesday Feb 07, 2007
Wednesday Feb 07, 2007
I have seen a lot of inquiries in the users alias and the forum about how to do coordinated drop-down lists. Coordinated drop-downs are basically a set of nested master-detail records that follow the same pattern that we show in Using Databound Components to Access a Database. With coordinated drop-downs, whenever the user selects a new item from the master drop-down, you need to refresh the list in the detail drop-down.
In the following example, I show how to coordinate drop-down lists that contain data from the Travel database. The first drop-down lists all the PERSON records. The second drop-down shows all the TRIP records for the selected PERSON, and the third drop-down shows all the FLIGHT records for the selected TRIP. Finally, the detail data shows data from the selected FLIGHT.
The trick with coordinating multiple drop-downs, is that you need to trickle down the refreshing of the lists. A change in the first drop-down's selection requires refreshing the second list, which in turn, requires refreshing the third list, and so on.
In this example, I took the easy route. When the user makes a new selection from the PERSON drop-down, I select the first record by default and coordinate the TRIP drop-down by showing all the trips for the first-person. Then, I show all the flights for the first TRIP record, and the detail data for the first FLIGHT record.
public void initFlightDropDown() {
try {
tripDataProvider.cursorFirst();
getSessionBean1().getFlightRowSet().setObject(
1, tripDataProvider.getValue("TRIP.TRIPID"));
flightDataProvider.refresh();
} catch (Exception e) {
error("Cannot switch to trip " +
tripDataProvider.getValue("TRIP.TRIPID"));
log("Cannot switch to person " +
tripDataProvider.getValue("TRIP.TRIPID"), e);
}
}
public void initFlightDetail() {
try {
flightDataProvider.cursorFirst();
flightRowSet.setObject(
1, flightDataProvider.getValue("FLIGHT.FLIGHTID"));
flightDataProvider.refresh();
} catch (Exception e) {
error("Cannot switch to flight " +
flightDataProvider.getValue("FLIGHT.FLIGHTID"));
log("Cannot switch to flight " +
flightDataProvider.getValue("FLIGHT.FLIGHTID"), e);
}
}
public void prerender() {
if ( personDD.getSelected() == null ) {
try {
personDataProvider.cursorFirst();
getSessionBean1().getTripRowSet().setObject(
1, personDataProvider.getValue("PERSON.PERSONID"));
tripDataProvider.refresh();
} catch (Exception e) {
error("Cannot switch to person " +
personDataProvider.getValue("PERSON.PERSONID"));
log("Cannot switch to person " +
personDataProvider.getValue("PERSON.PERSONID"), e);
}
initFlightDropDown();
initFlightDetail();
} else {
try {
// Synchronize data providers with current selections
personDataProvider.setCursorRow(
personDataProvider.findFirst(
"PERSON.PERSONID", personDD.getSelected()));
tripDataProvider.setCursorRow(
tripDataProvider.findFirst(
"TRIP.TRIPID", tripDD.getSelected()));
flightDataProvider.setCursorRow(
flightDataProvider.findFirst(
"FLIGHT.FLIGHTID", flightDD.getSelected()));
} catch (Exception e) {
error("Cannot switch to selections");
log("Cannot switch to selections", e);
}
}
}
public void personDD_processValueChange(ValueChangeEvent event) {
try {
getSessionBean1().getTripRowSet().setObject(
1, personDD.getSelected());
tripDataProvider.refresh();
} catch (Exception e) {
error("Cannot switch to person " +
personDataProvider.getValue(
"PERSON.PERSONID"));
log("Cannot switch to person " +
personDataProvider.getValue(
"PERSON.PERSONID"), e);
}
initFlightDropDown();
initFlightDetail();
}
public void tripDD_processValueChange(ValueChangeEvent event) {
try {
getSessionBean1().getFlightRowSet().setObject(
1, tripDD.getSelected());
flightDataProvider.refresh();
} catch (Exception e) {
error("Cannot switch to trip " +
tripDataProvider.getValue(
"TRIP.TRIPID"));
log("Cannot switch to trip " +
tripDataProvider.getValue(
"TRIP.TRIPID"), e);
}
initFlightDetail();
}
public void flightDD_processValueChange(ValueChangeEvent event) {
try {
flightRowSet.setObject(
1, flightDD.getSelected());
flightDataProvider1.refresh();
} catch (Exception e) {
error("Cannot switch to flight " +
flightDataProvider.getValue(
"FLIGHT.FLIGHTID"));
log("Cannot switch to flight " +
flightDataProvider.getValue(
"FLIGHT.FLIGHTID"), e);
}
}