Filtering JSON records by JavaScript or PHP

1

I have a question about how to do data filtering when the client fills an input field. I'm looking for all server logs (running PHP) through a jQuery JSON request and would like to know how to make a less "restrictive" JavaScript filter since the indexOf () method returns only records for precisely typed values , but when I perform the search directly by query the result is closer than I need. I do not want to use querys to DB very often because of performance issues.

The following is the javascript code (where I get the value entered by the user, and I make the filter in the JSON records):

if( value.nome_produto.indexOf(busca) != -1 ){
    tr += '<tr class='_item_em_falta _tr_relative'>
        <td> ${value.registro} </td>
        <td> 
            <a href='detalhe.php?codigo=${value.produto_id}'>
                <img class='_minha_img' src='${value.imagem_pequeno}'>
            </a>
        </td>
        <td>
            <a href='detalhe.php?codigo=${value.produto_id}'>
                ${value.nome_produto}
            </a>
        </td>
        <td> ${value.preco_produto} </td>
        <td> ${value.origem_produto} </td>
        <td> ${value.quantidade_total} </td>
        <td> ${value.quantidade_vendida} </td>
    </tr>';

The indexOf () filter used above only returns products with the same name as the one entered by the user (Case Sensitive and etc). I would like to use a form that allows you to filter the data in a less restrictive way (like the LIKE% example example% command in MySQL)

    
asked by anonymous 30.03.2018 / 01:55

1 answer

0

The% s of MySQL and of PHP make a similar search type. What you can do to solve your Case Sensitive problem in javascript, is to equalize the values. Ex:

var v1 = value.nome_produto.toUpperCase();
var v2 = busca.toUpperCase();

if( v1.indexOf(v2) != -1 ){
    tr += '<tr class='_item_em_falta _tr_relative'>
        <td> ${value.registro} </td>
        <td> 
            <a href='detalhe.php?codigo=${value.produto_id}'>
                <img class='_minha_img' src='${value.imagem_pequeno}'>
            </a>
        </td>
        <td>
            <a href='detalhe.php?codigo=${value.produto_id}'>
                ${value.nome_produto}
            </a>
        </td>
        <td> ${value.preco_produto} </td>
        <td> ${value.origem_produto} </td>
        <td> ${value.quantidade_total} </td>
        <td> ${value.quantidade_vendida} </td>
    </tr>';

With this it will return true for a comparison like: test == TEST!

    
30.03.2018 / 17:40