Skip to content

Commit

Permalink
Merge pull request #51 from derralf/dropdown-bugfix
Browse files Browse the repository at this point in the history
Fix dropdown fields
  • Loading branch information
Aaron Carlino committed Oct 26, 2016
2 parents 05e7829 + 90111d9 commit 524b7e2
Show file tree
Hide file tree
Showing 79 changed files with 5,409 additions and 71 deletions.
1 change: 1 addition & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ BootstrapForm:
bootstrap_included: false
jquery_included: false
bootstrap_form_included: false
bootstrap_select_included: false
inline_fields:
- CheckboxField
- FormAction
Expand Down
49 changes: 19 additions & 30 deletions code/BootstrapDropdownField.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
<?php

/**
* Defines a FormField that uses the bootstrap-select JS plugin for making
* dropdown fields nice.
*
*/
class BootstrapDropdownField extends DropdownField {
protected $optionsList;

public function __construct($name, $title = NULL, $options = array(), $value = NULL) {
parent::__construct($name, $title,$options, $value);
$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
)));
/**
* Builds the form field, sets default attributes, and includes JS
*
* @param array $attributes The attributes to include on the formfield
* @return SSViewer
*/
public function FieldHolder($attributes = array ()) {
if(!Config::inst()->get('BootstrapForm', 'bootstrap_select_included')) {
$current_locale = (class_exists('Translatable')) ? Translatable::get_current_locale() : i18n::get_locale();
Requirements::javascript(BOOTSTRAP_FORMS_DIR."/javascript/bootstrap-select/js/bootstrap-select.min.js");
Requirements::javascript(BOOTSTRAP_FORMS_DIR."/javascript/bootstrap-select/js/i18n/defaults-{$current_locale}.js");
Requirements::css(BOOTSTRAP_FORMS_DIR."/javascript/bootstrap-select/css/bootstrap-select.min.css");
}

return $options;
}

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

return $this->renderWith('BootstrapDropdownField');
$this->addExtraClass('selectpicker');
return parent::FieldHolder($attributes);
}
}
14 changes: 11 additions & 3 deletions code/ChosenDropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,20 @@ public function setSearchThreshold($num) {
* @return SSViewer
*/
public function FieldHolder($attributes = array ()) {
Requirements::javascript(FRAMEWORK_DIR."/admin/thirdparty/chosen/chosen/chosen.jquery.js");
Requirements::css(FRAMEWORK_DIR."/admin/thirdparty/chosen/chosen/chosen.css");

if(!Config::inst()->get('BootstrapForm', 'jquery_included')) {
Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
Requirements::javascript(FRAMEWORK_DIR."/admin/thirdparty/chosen/chosen/chosen.jquery.js");
Requirements::css(FRAMEWORK_DIR."/admin/thirdparty/chosen/chosen/chosen.css");
} else {
Requirements::javascript(BOOTSTRAP_FORMS_DIR."/javascript/chosen/chosen.jquery.js");
Requirements::css(BOOTSTRAP_FORMS_DIR."/javascript/chosen/chosen.css");
}

$this->addExtraClass('chosen');
if(!$this->getAttribute('data-search-threshold')) {
$this->setSearchThreshold(self::$default_search_threshold);
}
return parent::FieldHolder($attributes);
}
}
}
293 changes: 293 additions & 0 deletions javascript/bootstrap-select/css/bootstrap-select.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions javascript/bootstrap-select/css/bootstrap-select.css.map

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions javascript/bootstrap-select/css/bootstrap-select.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 524b7e2

Please sign in to comment.