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

WidgetDatalist no sensible a acentos #1622

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 36 additions & 7 deletions Core/Lib/Widget/WidgetDatalist.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ protected function inputHtml($type = 'datalist', $extraClass = '')

$list = $this->fieldname . '-list-' . $this->getUniqueId();
$html = '<input type="text" name="' . $this->fieldname . '" value="' . $this->value . '"'
. ' id="input-' . $list . '"'
. ' class="' . $class . '"'
. ' list="' . $list . '"'
. $this->inputHtmlExtraParams()
. ' parent="' . $this->parent . '"'
. ' data-field="' . $this->fieldname . '"'
Expand All @@ -61,12 +61,8 @@ protected function inputHtml($type = 'datalist', $extraClass = '')
. ' data-limit="' . $this->limit . '"'
. '/>';

$html .= '<datalist id="' . $list . '">';
foreach ($this->values as $option) {
$title = empty($option['title']) ? $option['value'] : $option['title'];
$html .= '<option value="' . $title . '" />';
}
$html .= '</datalist>';
$html .= $this->generateAutocompleteScript($list);

return $html;
}

Expand All @@ -89,4 +85,37 @@ protected function setSourceData(array $child, bool $loadData = true)
$this->setValuesFromCodeModel($values, $this->translate);
}
}

/**
* Genera el script de autocomplete para el campo input.
*
* @param $list
* @return string
*/
protected function generateAutocompleteScript($list)
{
$options = array_map(function ($option) {
return empty($option['title']) ? $option['value'] : $option['title'];
}, $this->values);

$optionsJson = json_encode($options, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);

return '<script>
$(document).ready(function() {
const options = ' . $optionsJson . ';

$("#input-' . htmlspecialchars($list, ENT_QUOTES, 'UTF-8') . '").autocomplete({
minLength: 3,
source: function(request, response) {
const term = request.term.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
const matches = options.filter(option => {
const normalizedOption = option.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
return normalizedOption.includes(term);
});
response(matches);
}
});
});
</script>';
}
}