I have a problem with the presentation of the content of the matches that are shown in a textbox with the JQuery autocomplete library since my text has ñ
and puts strange symbols, I think it is not decoding my text well but I do not know how to correct this problem.
I make an AJAX call to a page that returns the match results and I feel like the problem is in the AJAX:
<script>
$(document).ready(function () {
$("#btnBusca").css({ 'display': 'none' });
$("#filtroBusqueda").change(function () {
var op = $(this).val();
switch (op) {
case "rs":
$("#rs").css({ 'display': 'block' });
$("#ln").css({ 'display': 'none' });
$("#nc").css({ 'display': 'none' });
$("#btnBusca").css({ 'display': 'block' });
break;
case "ln":
$("#ln").css({ 'display': 'block' });
$("#rs").css({ 'display': 'none' });
$("#nc").css({ 'display': 'none' });
$("#btnBusca").css({ 'display': 'block' });
break;
case "nc":
$("#nc").css({ 'display': 'block' });
$("#rs").css({ 'display': 'none' });
$("#ln").css({ 'display': 'none' });
$("#btnBusca").css({ 'display': 'block' });
}
});
$("#txtRazon").autocomplete({
source: function (request, response) {
$.ajax({
type: "post",
url: "coincidenciaRazonSocial",
dataType: "json",
data: {
q: request.term
},
success: function (data) {
response($.map(data, function (item) {
return {
label: item.nombre,
value: item.nombre,
}
}));
}
});
}
});
});
</script>
<div>
<h5><span class="glyphicon glyphicon-th"></span> Búsqueda</h5>
<form id="formularoBuscar" method="post" class="form-horizontal " action="#">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="form-group">
<label class="col-lg-1 control-label">Filtro</label>
<div class="col-lg-11">
<select name="filtroBusqueda" id="filtroBusqueda" class="form-control">
<option value="" disabled selected>Seleccione Opción</option>
<option value="rs"> x Razón social</option>
<option value="ln">x Línea</option>
<option value="nc">x Número cuenta</option>
</select>
</div>
</div>
</div>
</div>
<div id="rs" style="display: none">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="form-group">
<label class="col-lg-1 control-label">Razón social</label>
<div class="col-lg-11">
<input type="text" class="form-control" id="txtRazon" />
</div>
</div>
</div>
</div>
</div>
<div id="ln" style="display: none">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="form-group">
<label class="col-lg-1 control-label">Número telefonico</label>
<div class="col-lg-11">
<input type="text" class="form-control" id="txtTelefono" />
</div>
</div>
</div>
</div>
</div>
<div id="nc" style="display: none">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="form-group">
<label class="col-lg-1 control-label">Número cuenta</label>
<div class="col-lg-11">
<input type="text" class="form-control" id="txtCuenta" />
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="form-group" id="btnBusca">
<label class="col-lg-1 control-label"></label>
<div class="col-lg-11">
<button type="button" id="btnBuscarAdenda">Buscar</button>
</div>
</div>
</div>
</div>
</form>
</div>
As shown in the image, it is not a problem of encoding in UTF, but
./coincidenciaRazonSocial
HTML entities are being received from the autocomplete ( ) file. In the case of theÑ
, encoded asÑ
.To fix it, we can modify the response received by AJAX in this part of the code:
Calling a function to decode:
And use the following function to convert any decimal entity to its corresponding character:
Note: this function only applies to decimal numeric entities such as the one shown in the image (
Ñ
), and does not replace others such asá
,>
orΣ
(hexadecimal).To show an example, I uploaded a text file with 3 items (
"DISEÑO 01"
,..02
and..03
), which we will get by AJAX:Since the records arrive encoded as HTML entities in the json, I can think of 2 possible solutions
Patch the
jQuery Autocomplete
so that it uses the value oflabel
asHTML
(innerHTML) instead oftexto
(innerText).In the doc , there is a link to Scott González's extension that does this.
Decode them before sending them back.
In
coincidenciaRazonSocial
, you should:The name is NOT encoded in the DB : Check that you are not encoding the
nombre
. For example:The name IS encoded in the DB : So you should decode them. For example:
In my case I was working with CodeIgniter and Jquery, what worked for me was to use the following method
In the label I use htmlspecialchars and with that I solved the issue.
Cheers