Skip to content

Commit

Permalink
Merge pull request #36 from colinedmz/feature-dropdownfield
Browse files Browse the repository at this point in the history
Feature DropdownField
  • Loading branch information
unclecheese committed Jan 25, 2016
2 parents 8515478 + 5223698 commit 1460a7d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
39 changes: 39 additions & 0 deletions code/BootstrapDropdownField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

class BootstrapDropdownField extends DropdownField {
protected $optionsList;

public function __construct($name, $title = NULL, $options = array(), $value = NULL) {
parent::__construct($name, $title, $value);

This comment has been minimized.

Copy link
@n8-dev

n8-dev May 26, 2016

Missing $options
#49

$this->optionsList = $options;

return $this;
}

public function setOptions($opts) {
$this->optionsList = $opts;

return $this;
}

public function getOptions() {
$options = ArrayList::create();
$selectedValue = $this->Value();
foreach ($this->optionsList as $val => $label) {
$isSelected = $selectedValue == (string) $val;
$options->push(ArrayData::create(array(
'Label' => $label,
'Value' => $val,
'Selected' => $isSelected
)));
}

return $options;
}

public function Field($attributes = array()) {
Requirements::javascript(BOOTSTRAP_FORMS_DIR . "/javascript/bootstrap_forms.js");

return $this->renderWith('BootstrapDropdownField');
}
}
13 changes: 13 additions & 0 deletions javascript/bootstrap_forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ $(function() {
}).filter('.active').click();
}

// dropdowns need to be configured when javascript is enabled.
$('.dropdown-select').hide();
$('.dropdown-toggle').show();

// sync selected list item with the select control
$('.dropdown-menu a').on('click', function(ev) {
var rel = $(this).attr('rel'),
value = $(this).data('value');

$(rel + '_select').val(value);
$(rel + '_select').change();
$(rel + '_label').text($(this).text());
});

});

Expand Down
24 changes: 24 additions & 0 deletions templates/BootstrapDropdownField.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<span class="btn-group">
<select name="$Name" id="{$id}_select" class="dropdown-select">
<% loop $Options %>
<option <% if $Selected %>selected="selected"<% end_if %> value="$Value">$Label</option>
<% end_loop %>
</select>
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">
<% loop $Options %>
<% if $Selected %>
<span id="{$Top.id}_label" data-default="$Top.Title" class="dropdown-label">$Label</span>
<% end_if %>
<% end_loop %>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="{$id}_label">
<% loop $Options %>
<li role="presentation">
<a data-value="$Value" rel="#$Top.id" role="menuitem" tabindex="-1"
href="#">$Label
</a>
</li>
<% end_loop %>
</ul>
</span>

0 comments on commit 1460a7d

Please sign in to comment.