Skip to content

Commit

Permalink
Add option for preselecting item in select list
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikheil committed Oct 18, 2015
1 parent e6f0a66 commit 10ae72b
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions HTML/TreeMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ class HTML_TreeMenu_Listbox extends HTML_TreeMenu_Presentation
*
* @access public
*/
function HTML_TreeMenu_Listbox($structure, $options = array())
function HTML_TreeMenu_Listbox($structure, $options = array(), $elementID = null, $addHTML = null)
{
$this->HTML_TreeMenu_Presentation($structure);

Expand All @@ -929,6 +929,9 @@ function HTML_TreeMenu_Listbox($structure, $options = array())
$this->indentNum = 2;
$this->linkTarget = '_self';
$this->submitText = 'Go';
$this->activeNode = 0;
$this->elementID = $elementID;
$this->addHTML = $addHTML;

foreach ($options as $option => $value) {
$this->$option = $value;
Expand All @@ -943,6 +946,13 @@ function HTML_TreeMenu_Listbox($structure, $options = array())
*/
function toHTML()
{
if (is_null($this->elementID)) {
$elementID = "catid";
}
else {
$elementID = $this->elementID;
}

static $count = 0;

$nodeHTML = '';
Expand All @@ -956,16 +966,9 @@ function toHTML()
}
}

return sprintf('<form target="%s" action="" onsubmit="var link = ' .
'this.%s.options[this.%s.selectedIndex].value; ' .
'if (link) {this.action = link; return true} else ' .
'return false"><select name="%s">' .
'<option value="">%s</option>%s</select> ' .
'</form>',
$this->linkTarget,
'HTML_TreeMenu_Listbox_' . ++$count,
'HTML_TreeMenu_Listbox_' . $count,
'HTML_TreeMenu_Listbox_' . $count,
return sprintf('<select id="' . $elementID . '" ' . $this->addHTML .
'name="%s"><option value="choose">%s</option>%s</select>',
$elementID,
$this->promoText,
$nodeHTML);
}
Expand All @@ -981,10 +984,14 @@ function toHTML()
*/
function _nodeToHTML($node, $prefix = '')
{
$html = sprintf('<option value="%s">%s%s</option>',
$node->link,
$prefix,
$node->text);
if ($this->activeNode == $node->link) {
$sel = " selected = 'selected'";
}
else {
$sel = "";
}

$html = sprintf('<option%s value="%s">%s%s</option>', $sel, $node->link, $prefix, $node->text);

/*
* Loop through subnodes
Expand Down

0 comments on commit 10ae72b

Please sign in to comment.