我最近创建了一个JavaScript过滤器来过滤产品表中的数据,我有5个字段可以输入搜索,它们是:描述,型号,分销商和库存。我将表与另一个视图中的产品分开,并将字段保留在索引中。
我需要表格来返回我在字段中输入的值。我正在举例说明我用'描述'领域。由于代码变大,我对其他字段使用相同的逻辑。
我的意见
<input class="form-control" placeholder="Descrição" placeholder="Descrição" id="descricao" value="{{ session('descricao') }}" name="descricao">
我的脚本来自视图
<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>
我的表格视图
<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>
我的产品-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);
});
}
我的控制器
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);
}
有什么建议吗?