Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dropdown fields #51

Merged
merged 1 commit into from
Oct 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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