HowToPopulateAndUseListControls

From Dabo Wiki
Jump to: navigation, search

A common need in an application is to create a list of choices for the user, who makes a selection. The items in the list correspond to a lookup table, and what is stored in the current table's record is a foreign key (FK) that points to the entry in the lookup table.

There are three controls that can be used to display a list of choices like this: (dListBox, dDropdownList, and dComboBox). They all share the same properties to control list selection behavior, so no matter which one you use in your app, the steps are the same.

First, create a method in your bizobj to get the available choices and their corresponding key values. Exactly how you get them depends on your database design, but something like this should get you started:

# This is in your bizobj class
def getAvailableTypes(self):
    """Return a 2-tuple of lists of the types and their keys."""
    crs = self.getTempCursor()
    crs.execute("select pk, typename from mytable order by typename")
    ds = crs.getDataSet()
    # Create the lists
    names = [rec["typename"] for rec in ds]
    keys = [rec["pk"] for rec in ds]
    return (names, keys)

Then create your list with the following properties:

  • DataSource = <bizobj's DataSource>
  • DataField = <name of the FK field in your table>
  • ValueMode = "Key"
  • Choices = <list of values (without keys) that should appear in the list>
  • Keys = <list of keys for the Choices values>

Note that the lists for Choices and Keys should be in the same order, so that the first element of Keys is the key for the first element of Choices, and the second element of Keys... etc.

Now when you navigate to a record, the FK value in the DataField of that record will cause the list control to show the corresponding name. If the user selects a different item from the list, the FK will be updated with the new choice's key.

(Newbie AppWizard users: see http://leafe.com/archives/msg/342481 to access your bizObj from the ui)