Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
This MultiSelectListAttribute and ListAttribute
  • Loading branch information
michelerota committed Jul 1, 2016
1 parent 6daee3b commit 7b68d8f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/Attributes/ListAttribute.php
Expand Up @@ -277,7 +277,6 @@ public function edit($record, $fieldprefix, $mode)
$this->getOwnerInstance()->getPage()->register_loadscript('jQuery("#'.$id.'").select2();');

$selectOptions = [];

$onchange = '';
if (count($this->m_onchangecode)) {
$onchange = ' onChange="'.$this->getHtmlId($fieldprefix).'_onChange(this)"';
Expand Down
27 changes: 15 additions & 12 deletions src/Attributes/MultiSelectListAttribute.php
Expand Up @@ -9,7 +9,7 @@ class MultiSelectListAttribute extends ListAttribute
{

const AF_NO_TOGGLELINKS = 536870912;

/**
* Default field separator.
*
Expand Down Expand Up @@ -144,16 +144,28 @@ public function edit($record, $fieldprefix, $mode)
{
$id = $this->getHtmlId($fieldprefix);
$name = $this->getHtmlName($fieldprefix);
$this->getOwnerInstance()->getPage()->register_loadscript('jQuery("#'.$id.'").select2();');

$selectOptions = [];
$selectOptions['dropdown-auto-width'] = true;
$selectOptions['minimum-results-for-search'] = 10;
$selectOptions['tags'] = true;
if ($this->m_width) {
$selectOptions['width'] = $this->m_width;
}

$data = '';
foreach ($selectOptions as $k => $v) {
$data .= ' data-'.$k.'="'.htmlspecialchars($v).'"';
}

$onchange = '';
if (count($this->m_onchangecode)) {
$onchange = $id.'_onChange(this);';
$onchange = ' onChange="'.$this->getHtmlId($fieldprefix).'_onChange(this)"';
$this->_renderChangeHandler($fieldprefix);
}

$result = '<select multiple id="'.$id.'" name="'.$name.'[]" '.$this->getCSSClassAttribute('form-control').'" '.$onchange.'>';
$result = '<select multiple id="'.$id.'" name="'.$name.'[]" '.$this->getCSSClassAttribute('form-control').'" '.$onchange.$data.'>';

$values = $this->getValues();
if (!is_array($record[$this->fieldName()])) {
Expand All @@ -171,15 +183,6 @@ public function edit($record, $fieldprefix, $mode)

$result .= '</select>';

$selectOptions['tags'] = true;
$selectOptions['width'] = '100%';

$script = "jQuery('#$id').select2(".json_encode($selectOptions).")";
if ($onchange != '') {
$script .= '.on("change", function(){'.$onchange.'})';
}
$result .= '<script>'.$script.';</script>';

return $result;
}

Expand Down

0 comments on commit 7b68d8f

Please sign in to comment.