Enum Data Types
With the enum data type, a
developer can include a number of user-specified values
within a single UserPref element. You
can think of this type as an HTML drop-down list that allows the user to
select a value from several different choices.
For example, let’s say we’re building a movie-rating widget and
want to allow users to store movie ratings from 1–5 (with 0 meaning the
user did not rate the movie). We could increase the base UserPref node with our additional EnumValue
nodes:
<UserPref name="rating" display_name="Rating" datatype="enum" default_value="0"> <EnumValue value="1" display_value="Worst Movie Ever"/> <EnumValue value="2" display_value="I just wasted 2 hours of my life"/> <EnumValue value="3" display_value="Meh"/> <EnumValue value="4" display_value="That was pretty entertaining"/> <EnumValue value="5" display_value="Epic"/> </UserPref>
We specify enum as our data
type, and within the UserPref node we
define the EnumValue nodes for each
rating value the user can choose from. Each node has a display value
that the user sees (e.g., “Epic”) and a value that the developer uses to
denote the chosen option (e.g., “5”).
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access