Foen
December 9th, 2009, 06:20
In ddavison's drop down menu there is a line <lookup>item</lookup> that tells the script which line to look for in the globalLookups.lua file. That's all dandy and useful but I am trying to figure out how to set an if elseif then statement to tell the script where to look in the globalLookups.lua file.
For example if the above drop-down menu has selected swords on it's weapons lookup, then I'd like the next menu to look into <lookup>swords</lookup> or if a mace then maces etc etc that way things can be divided up into subcategories.
The drop-down code doesn't include this facility, but there are a couple of ways you can implement this: having two drop-downs and making only one of them visible, or loading the drop-down contents dynamically.
Two Drop-Downs
In the example, when the weapon type is changed (you must trap this from the onUpdate of the underlying database node or the onValueChanged of the string control) your script can examine the selected value and selectively setVisible the controls associated with a swords drop-down and a second set of controls associated with a maces drop-down. This will work for more than two drop-downs if needed.
Dynamic Loading
When the weapon type is changed, your script can reset the drop-down contents and load a new list into the drop-down, if you are using the version of the drop-down in the Base Ruleset: this version has dynamic population capabilities using an addItems() method. As I write this, I realise there is no 'clear()' method so I'll add one to the code.
Brief Overview of the Base Ruleset Drop-Down
Tags
<DropDown>
<fonts>
<normal>...</normal>
<selected>...</selected>
</fonts>
<frames>
<normal>...</normal>
<selected>...</selected>
</frames>
<position>...</position>
<size>...</size>
<datasource>...</datasource>
<lookup>...</lookup>
<lookupFieldName>...</lookupFieldName>
<target>...</target>
</DropDown>
The fonts and frames tags determine how the rows render, and typically are set so that the selected row is shaded (using the frame) and has a bold font.
The position tag controls where the drop down trigger is positioned (x,y) relative to the inside top right of the target control.
The size is the maximum number of rows displayed in the drop down list. If there are more entries than this the list will have a scrollbar. If there are less, the list will render shorter.
The datasource and lookup tags are mutually exclusive. If present, the datasource tag points to a list in the db.xml which is the source for the list contents (not the target for the selected item). If datasource isn't specified, the lookup tag is queried to find the name of the global list to use.
The lookupFieldName determines, for complex lists, which field in each record is used to populate the list.
The target tag identifies the string/number control to which the list is bound. The target *can* be readonly, in which case the drop down opens, and the value is properly highlighted, but the user cannot change the selected item. It can also be static (for strings) which allows selection only from the list (users cannot type alternative values into the target control).
Methods
The DropDown is a generic control and the DropDownList (which is created dynamically, so doesn't need to be specified in the sheet data) is a window list control, and hence they expose the same properties/methods as their base classes. In addition, DropDown exposes the following methods.
showList() – displays the DropDownList, but only if it initialises correctly.
hideList() – closes the DropDownList.
getValue() – returns the currently selected value or the empty string if nothing has been selected.
setValue(result) – forces the list to accept the result, but only if it is a valid item from it. It also highlights the item and scrolls to make sure it is visible.
getText() – returns the selected display text or the empty string. Generally the display text and the value are the same, but there may be instances where you want to display are string and store an abbreviation or a number, for example.
add(value, text) – adds the entry to the list. If the text parameter is missing, the value is used for both purposes. If there is only one parameter and it is a table, the code either uses the lookupFieldName to find the right value to use or it looks for a Value field and a Text field in the table.
addItems(list) – recursively calls add() for each item in the list. This can be used to populate a drop down dynamically from code quite quickly.
selectitem(opt) – if passed a pointer to one of the windows in the drop down list itself, this will select that window.
You may note that I have tried to follow the DHTML "select" API, where sensible.
Events
The DropDown exposes no events which can be captured in script, and makes use of the following: onInit, onClickDown, onHover. If you want to make use of these events in script for a DropDown, you'll have to run the 'if super.eventName then super.eventName() end;' test at the beginning of the event handler code to make sure you don't accidentally disable the control.
Other
The control makes use of another template control (<Option>) so you need to copy the code for this across as well. The Option control represents an instance in the list and exposes similar methods/events to the DHTML "option" object.
Finally, a DropDown must have a name otherwise it won't work. This is because it creates sub-controls dynamically and they can be created nameless or with duplicates of existing control names.
Stuart
For example if the above drop-down menu has selected swords on it's weapons lookup, then I'd like the next menu to look into <lookup>swords</lookup> or if a mace then maces etc etc that way things can be divided up into subcategories.
The drop-down code doesn't include this facility, but there are a couple of ways you can implement this: having two drop-downs and making only one of them visible, or loading the drop-down contents dynamically.
Two Drop-Downs
In the example, when the weapon type is changed (you must trap this from the onUpdate of the underlying database node or the onValueChanged of the string control) your script can examine the selected value and selectively setVisible the controls associated with a swords drop-down and a second set of controls associated with a maces drop-down. This will work for more than two drop-downs if needed.
Dynamic Loading
When the weapon type is changed, your script can reset the drop-down contents and load a new list into the drop-down, if you are using the version of the drop-down in the Base Ruleset: this version has dynamic population capabilities using an addItems() method. As I write this, I realise there is no 'clear()' method so I'll add one to the code.
Brief Overview of the Base Ruleset Drop-Down
Tags
<DropDown>
<fonts>
<normal>...</normal>
<selected>...</selected>
</fonts>
<frames>
<normal>...</normal>
<selected>...</selected>
</frames>
<position>...</position>
<size>...</size>
<datasource>...</datasource>
<lookup>...</lookup>
<lookupFieldName>...</lookupFieldName>
<target>...</target>
</DropDown>
The fonts and frames tags determine how the rows render, and typically are set so that the selected row is shaded (using the frame) and has a bold font.
The position tag controls where the drop down trigger is positioned (x,y) relative to the inside top right of the target control.
The size is the maximum number of rows displayed in the drop down list. If there are more entries than this the list will have a scrollbar. If there are less, the list will render shorter.
The datasource and lookup tags are mutually exclusive. If present, the datasource tag points to a list in the db.xml which is the source for the list contents (not the target for the selected item). If datasource isn't specified, the lookup tag is queried to find the name of the global list to use.
The lookupFieldName determines, for complex lists, which field in each record is used to populate the list.
The target tag identifies the string/number control to which the list is bound. The target *can* be readonly, in which case the drop down opens, and the value is properly highlighted, but the user cannot change the selected item. It can also be static (for strings) which allows selection only from the list (users cannot type alternative values into the target control).
Methods
The DropDown is a generic control and the DropDownList (which is created dynamically, so doesn't need to be specified in the sheet data) is a window list control, and hence they expose the same properties/methods as their base classes. In addition, DropDown exposes the following methods.
showList() – displays the DropDownList, but only if it initialises correctly.
hideList() – closes the DropDownList.
getValue() – returns the currently selected value or the empty string if nothing has been selected.
setValue(result) – forces the list to accept the result, but only if it is a valid item from it. It also highlights the item and scrolls to make sure it is visible.
getText() – returns the selected display text or the empty string. Generally the display text and the value are the same, but there may be instances where you want to display are string and store an abbreviation or a number, for example.
add(value, text) – adds the entry to the list. If the text parameter is missing, the value is used for both purposes. If there is only one parameter and it is a table, the code either uses the lookupFieldName to find the right value to use or it looks for a Value field and a Text field in the table.
addItems(list) – recursively calls add() for each item in the list. This can be used to populate a drop down dynamically from code quite quickly.
selectitem(opt) – if passed a pointer to one of the windows in the drop down list itself, this will select that window.
You may note that I have tried to follow the DHTML "select" API, where sensible.
Events
The DropDown exposes no events which can be captured in script, and makes use of the following: onInit, onClickDown, onHover. If you want to make use of these events in script for a DropDown, you'll have to run the 'if super.eventName then super.eventName() end;' test at the beginning of the event handler code to make sure you don't accidentally disable the control.
Other
The control makes use of another template control (<Option>) so you need to copy the code for this across as well. The Option control represents an instance in the list and exposes similar methods/events to the DHTML "option" object.
Finally, a DropDown must have a name otherwise it won't work. This is because it creates sub-controls dynamically and they can be created nameless or with duplicates of existing control names.
Stuart