I have the following code in JQuery, a function to filter an array that I store in a global variable, it is a JSON that I ask for AJAX.
function filtrar(dato){
var filtrado = content.filter(function (a) {
return a.nombre == dato;
});
if (filtrado.length>0) {
$("#resultado").empty();
for (var i = 0; i < filtrado.length; i++) {
var newRow =
"<tr>" +
"<td>" + filtrado[i].idproducto + "</td>" +
"<td>" + filtrado[i].nombre + "</td>" +
"<td>" + filtrado[i].marca + "</td>" +
"<td>" + filtrado[i].categoria + "</td>" +
"<td>" + filtrado[i].precio + "</td>" +
"</tr>";
$(newRow).appendTo("#resultado");
}
}
};
It works perfect, but it filters, that is, it erases the entire table and shows those that coincide strictly, that is, the entire product name in this case. How can I filter character by character if the name contains part of the searched string?
var content;
$(document).ready(function() {
listar();
var nombreBusqueda;
$("#nombre").keyup(function() {
nombreBusqueda=$("#nombre").val();
filtrar(nombreBusqueda);
}
);
});
Up there the code when I load the page and I type a search. I want to look in the array and not in the DOM because I plan to paginate in the future and I want to search over the total of records