2

I recently created a JavaScript filter to filter data from a product table, I have 5 fields to enter the search, they are: description, model, distributor and stock. I separated the table with the products in another view and left the fields in my index.

enter image description here

I need the table to return the values I put in the fields. I am putting an example that I made with the 'description' field. Since the code got big and I use the same logic for the other fields.

My input:

 <input class="form-control"  placeholder="Descrição" placeholder="Descrição" id="descricao" value="{{ session('descricao') }}" name="descricao">

My script from view

<script src="{{ asset('assets/js/ProductSearch.js') }}"></script>
<script>
        var postSearch = '{{ route('product::searchPost') }}';
        var searchRequest = {
            'descricao':'{{session('descricao')}}',
            'model': '{{ session('model') }}',
            'distributor': '{{ session('distributor') }}',
            'status': '{{ session('status') }}',
            'stock' : '{{ session('stock') }}',
            'image' : '{{ session('image') }}',
        };
    </script>

My table view

<table class="table table-striped table-bordered" id="tableProd" class="table table-bordered">
    <tr>
        <th><center>Imagem</center></th>
        <th>SKU</th>
        <th><center>Produto</center></th>
        <th>Custo</th>
        <th>Preço</th>
        <th>Atualização</th>
        <th>Status</th>
        <th>Estoque</th>
        <th>Distruibuidor</th>
        <th>Categoria</th>
    </tr>
    @foreach($product as $prod)
        <tr>
            <tbody>
            <td><img src="{{$prod->image->erp_image}}" style="width: 50px; height: 50px;" alt="" id="ProdImage"></td>
            <td>{{$prod->erp_model}}</td>
            <td>{{$prod->description->erp_name}}</td>
            <td>R$ {{$prod->erp_cost}}</td>
            <td>R$ {{$prod->erp_price}}</td>
            <td>{{ $prod->erp_modifieddate}}</td>
            <td style="max-width: 45px">
                @if($prod->status == 1)
                    <span class="label label-success">Ativo</span>
                @else
                    <span class="label label-danger">Inativo</span>
                @endif
            </td>
            <td>{{ $prod->erp_quantity}}</td>
            <td>{{ App\Helpers\ProductDistributorHelper::$distributor[$prod->erp_distributor] }}</td>
            <td>
                @foreach($prod->category as $category)
                    {{$category->erp_name}}
                @endforeach
            </td>
</tr>
</table>

My product-search.js

$(document).ready(function() {
    $(document).on('blur', '#descricao', function() {
            var descricao = $('#descricao').val();
            searchRequest['descricao'] = descricao;
            doSearch();
        });
});
    function doSearch() {
    $.post(postSearch, {
            'search_data': JSON.stringify(searchRequest),
            '_token': $('meta[name=csrf-token]').attr('content'),
        }
        , function(data) {
            $('#product-table').html(data);
        });
}

My Controller

public function search(Request $request)
    {
        $product = Product::query();
if($request->isMethod('post'))
        {
$descricao = $data->descricao;
session(['descricao' => $descricao]);
if(strlen(session('descricao')) > 0)
            {
                $product_ids = Description::where('erp_name', 'LIKE', '%'.session('descricao').'%');
                $ids = [];

                foreach($product_ids as $product_data){
                    $ids[] = $product_data->erp_productid;
                }

                $product = $product->whereIn('erp_productid', $ids);
            }

Any suggestion?

Vinícius
  • 443
  • 1
  • 8
  • 29
  • 1
    What i would recommend is to use **AJAX** [Here](http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) is a good example. – Rüzgar Apr 24 '17 at 21:34
  • Thanks, @Rüzgar! The error actually was in my Eloquent relations, not exactly in JavaScript. I got it fixed. – Vinícius Apr 27 '17 at 17:35

0 Answers0