You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何过滤已添加到表格的商品,使商品选择建议列表不再显示已选商品

如何过滤已添加到表格的商品,使商品选择建议列表不再显示已选商品

我明白你的需求啦——已经添加到表格里的商品,就不要再出现在后续的选择建议里了对吧?其实核心思路很简单,就是把已选商品的ID收集起来,在查询建议的时候排除它们,下面给你具体的实现步骤:

第一步:前端维护并传递已选商品ID列表

当你把商品添加到表格后,前端需要维护一个数组(比如命名为selectedInventoryIds),专门存放已经选中的商品对应的inventory_id(就是你代码里每个建议元素带的inventory_id属性)。每次触发搜索建议请求时,把这个数组一起传给后端。

举个AJAX请求的例子:

// 假设这是已经选中的inventory_id数组,比如已经选了id=1的商品
let selectedInventoryIds = [1];

// 发送搜索请求时带上这个参数
$.ajax({
  url: '/你的搜索接口地址',
  method: 'GET',
  data: {
    key: 输入的搜索关键词,
    almacenOrigen: 仓库ID,
    selected_ids: selectedInventoryIds // 新增这个参数,传递已选ID
  },
  success: function(response) {
    // 渲染建议列表的逻辑
  }
});

第二步:后端接收参数并在查询中排除已选ID

在你的PHP后端代码里,先接收前端传来的已选ID数组,然后在查询商品的时候,用whereNotIn条件排除这些已选的inventory_id:

// 接收已选ID列表,默认是空数组(避免没传参时报错)
$selectedIds = $request->input('selected_ids', []);

// 原有的查询代码,新增whereNotIn条件
$name = DB::table('products')
    ->leftJoin('line_products', 'products.lineProduct_id', '=', 'line_products.id')
    ->leftJoin('inventories', 'products.id', '=', 'inventories.product_id')
    ->leftJoin('warehouses', 'inventories.warehouse_id', '=', 'warehouses.id')
    ->where('inventories.status_id', 1)
    ->where('products.sku', $Sku)
    ->where('warehouses.id', $almacenOrigen)
    ->whereNull('products.deleted_at')
    // 新增这一行:排除已经选中的inventory_id
    ->whereNotIn('inventories.id', $selectedIds)
    ->select(
        'products.sku',
        'products.reference',
        'products.id',
        'products.name as nameP',
        'line_products.skuLength',
        'line_products.expirationLength',
        'line_products.lotLength',
        'line_products.traceabilityLength',
        'line_products.name as nameL',
        'inventories.lot',
        'inventories.warehouse_id',
        'inventories.id as inventory_id',
        'inventories.traceability'
    )
    ->get();

第三步:处理边界情况

  • 如果还没选任何商品,selectedIds是空数组,whereNotIn不会对查询产生影响,会正常返回所有符合条件的商品。
  • 确保前端传递的selected_ids是数组类型,后端用$request->input('selected_ids', [])的方式可以安全获取,避免出现非数组参数导致的报错。

这样修改之后,已经添加到表格里的商品就不会再出现在你的选择建议列表里啦~

备注:内容来源于stack exchange,提问作者user20389386

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 14:47:50