在Laravel中使用jQuery自动完成功能删除动态添加的行中的重复条目

时间:2019-03-23 14:40:23

标签: jquery laravel

我正在使用使用jQuery附加行和自动完成脚本的表单。它们工作正常,但是如何删除重复的条目?还是删除下拉菜单中已经选择的条目?

enter image description here

这是我的自动完成功能

public function salesResponse(Request $request){
    $query = $request->get('term','');
    $wh2Summaries=Warehouse2StockSummaries::with('product');

    if($request->type == 'product_code'){
        $wh2Summaries->whereHas('product', function ($q) use ($query) {
            $q->where('product_code', 'LIKE', '%' . $query . '%');
        }); 
    }

    if($request->type=='product_name'){
        $wh2Summaries->whereHas('product', function ($q) use ($query) {
            $q->where('product_code', 'LIKE', '%' . $query . '%');
        }); 
    }

    if($request->type=='product_id'){
        $wh2Summaries->whereHas('product', function ($q) use ($query) {
            $q->where('product_code', 'LIKE', '%' . $query . '%');
        }); 
    }

    $wh2Summaries=$wh2Summaries->get();        
    $data=array();
    foreach ($wh2Summaries as $wh2Summary) {
        $data[]=array(
            'product_code'=>$wh2Summary->product->product_code,
            'name'=>$wh2Summary->product->name,
            'price'=>$wh2Summary->product->price,
            'quantity'=>$wh2Summary->qty_in-$wh2Summary->qty_out,
            'id'=>$wh2Summary->product->id
        );
    }
    if(count($data))
        return $data;
    else
        return [
            'product_code'=>'No product',
            'name'=>'',
            'price'=>'',
            'qty_out'=>'',
            'id'=>''
        ];
} 

这是我的自动完成脚本

//autocomplete script
$(document).on('focus','.autocomplete_txt',function(){
  type = $(this).data('type');

  if(type =='product_code' )autoType='product_code'; 
  if(type =='product_name' )autoType='name'; 
  if(type =='product_price' )autoType='price'; 
  if(type =='quantity' )autoType='quantity'; 
  if(type =='product_id' )autoType='id'; 

   $(this).autocomplete({
       minLength: 0,
       source: function( request, response ) {
            $.ajax({
                url: "{{ route('searchaSaleItems') }}",
                dataType: "json",
                data: {
                    term : request.term,
                    type : type,
                },
                success: function(data) {
                    var array = $.map(data, function (item) {
                       return {
                           label: item[autoType],
                           value: item[autoType],
                           data : item
                       }
                    });
                    response(array)
                }
            });
       },
       select: function( event, ui ) {
           var data = ui.item.data;
           var limit
           id_arr = $(this).attr('id');
           id = id_arr.split("_");
           elementId = id[id.length-1];
           $('#product_code_'+elementId).val(data.product_code);
           $('#product_name_'+elementId).val(data.name);
           $('#product_price_'+elementId).val(data.price).prop('min', data.price);
           $('#product_id_'+elementId).val(data.id);
           $('#quantity_'+elementId).prop('max', data.quantity);
           $('#quantity_warning_'+elementId).text('You have '+data.quantity-data.quantity+' in your stocks');
       }
   });
});

在我当前的代码中,由于重复的条目,我无法控制股票的限制。请帮助我,并提前非常感谢您!

更新 添加我的追加脚本

  var rowCount = 1;

  $('#add').click(function() {
    rowCount++;
    $('#orders').append(<tr id="row'+rowCount+'"><td> . ... ....</td></tr> );
});

0 个答案:

没有答案