【问题标题】:Collapsible dynamic BOM via jquery?通过jquery可折叠动态BOM?
【发布时间】:2011-10-04 14:52:04
【问题描述】:

我有一个 php 页面,它从代表产品 BOM(物料清单)的 sql2000 存储过程中提取数据。现在它显示为一张平板,效果很好。但是,经过 3 天的谷歌搜索/黑客攻击后,我终生无法弄清楚如何将这些数据转换为缩进的可折叠/可展开表或 UL。

表格如下:

  • ID[
  • BOM_Level
  • Parent_Item
  • Component_Item
  • 组件数量

BOM_Level 就是 BOM 的级别(0-12 之间的数字)。我假设是因为提供了它会很简单,因为 bom_level 确实代表了每个部分的缩进级别,但是唉,它并不简单......

大体结构如下,但零件数量和级别会因产品而异:

0 Top Level
 1 - Assembly 1
  2 - part 1 of Assembly 1
  2 - part 2 of Assembly 1
  2 - part 3 of Assembly 1
  2 - part 4 of Assembly 1
 1 - Assembly 2
  2 - Assembly 1 of Assembly 2
   3 - part 1 of Assembly 1 of Assembly 2
   3 - part 2 of Assembly 1 of Assembly 2
   3 - part 3 of Assembly 1 of Assembly 2

我一直在尝试做类似的事情:

  • 如果 bom_level == previous_bom_level 则创建 li
  • 如果 bom_level > previous_bom_level 添加新的 UL 并开始新的 li
  • 如果BOM级别是

说了这么多,我只是想不通如何做到这一点。有什么想法吗?

【问题讨论】:

  • 我觉得这里的 BOM 不代表浏览器对象模型...
  • 好点!相应地更新了问题

标签: php jquery css sql-server


【解决方案1】:

假设你有这样的数据:

// plus any additional data per entry
$data = array (
  array (
    'id' => '10',
    'bom_level' => '2',
    'parent_item_no' => '2800CF',
    'comp_item_no' => '2800CF-02',
  ),
  array (
    'id' => '66',
    'bom_level' => '3',
    'parent_item_no' => '2800CF-02',
    'comp_item_no' => '2000CF-12',
  ),
  array (
    'id' => '189',
    'bom_level' => '4',
    'parent_item_no' => '2000CF-12',
    'comp_item_no' => '0578',
  ),
  array (
    'id' => '190',
    'bom_level' => '4',
    'parent_item_no' => '2000CF-12',
    'comp_item_no' => '2000CF-SH11',
  ),
  array (
    'id' => '222',
    'bom_level' => '5',
    'parent_item_no' => '2000CF-SH11',
    'comp_item_no' => '1000',
  ),
  array (
    'id' => '191',
    'bom_level' => '4',
    'parent_item_no' => '2000CF-12',
    'comp_item_no' => '2000CF-SH12',
  ),
  array (
    'id' => '223',
    'bom_level' => '5',
    'parent_item_no' => '2000CF-SH12',
    'comp_item_no' => '1000',
  ),
  array (
    'id' => '67',
    'bom_level' => '3',
    'parent_item_no' => '2800CF-02',
    'comp_item_no' => '2000CF-AG01',
  ),
  array (
    'id' => '192',
    'bom_level' => '4',
    'parent_item_no' => '2000CF-AG01',
    'comp_item_no' => '303025-20',
  ),
  array (
    'id' => '68',
    'bom_level' => '3',
    'parent_item_no' => '2800CF-02',
    'comp_item_no' => '2000CF-PL13',
  ),
  array (
    'id' => '193',
    'bom_level' => '4',
    'parent_item_no' => '2000CF-PL13',
    'comp_item_no' => '0500',
  ),
  array (
    'id' => '69',
    'bom_level' => '3',
    'parent_item_no' => '2800CF-02',
    'comp_item_no' => '2000CF-PL14',
  ),
  array (
    'id' => '194',
    'bom_level' => '4',
    'parent_item_no' => '2000CF-PL14',
    'comp_item_no' => '0187',
  ),
  array (
    'id' => '70',
    'bom_level' => '3',
    'parent_item_no' => '2800CF-02',
    'comp_item_no' => '2000CF-SQ01',
  ),
  array (
    'id' => '195',
    'bom_level' => '4',
    'parent_item_no' => '2000CF-SQ01',
    'comp_item_no' => '050018-20',
  ),
  array (
    'id' => '71',
    'bom_level' => '3',
    'parent_item_no' => '2800CF-02',
    'comp_item_no' => '2000CF-WB06',
  ),
  array (
    'id' => '196',
    'bom_level' => '4',
    'parent_item_no' => '2000CF-WB06',
    'comp_item_no' => '040013-20',
  ),
);

comp_item_no 是唯一的,parent_item_no 指向它的父级。以parent_item_no=0 为根节点。

您可以将其映射到另一个结构:

$map = array();
foreach ($data as $entry) {
  if (!isset($map[$entry['parent_item_no']])) {
    $map[$entry['parent_item_no']] = array();
  }

  $map[$entry['parent_item_no']][] = $entry;
}

又可以递归遍历,如下所示:

function helper($children, $map) {
  if (sizeof($children) > 0) {
    echo '<ul>';
    foreach ($children as $parentId => $child) {
      echo '<li>';
      echo $child['comp_item_no'];
      if ($child['comp_item_no'] !== $child['parent_item_no'] && isset($map[$child['comp_item_no']])) {
        helper($map[$child['comp_item_no']], $map);
      }
      echo '</li>';
    }
    echo '</ul>';
  }
}

/**
 * note that '2800CF' here is manually choosen
 * because it has the lowest bom_level! In your real
 * code you might want to capture those nodes while
 * restructuring the array (see above)
 */
helper($map['2800CF'], $map); 

产生一个嵌套的ul。示例(对于上面的数据):

  • 2800CF-02
    • 2000CF-12
      • 0578
      • 2000CF-SH11
        • 1000
      • 2000CF-SH12
        • 1000
    • 2000CF-AG01
      • 303025-20
    • 2000CF-PL13
      • 0500
    • 2000CF-PL14
      • 0187
    • 2000CF-SQ01
      • 050018-20
    • 2000CF-WB06
      • 040013-20

演示:http://codepad.org/FJwX3Z1c

【讨论】:

  • 当你说“父母指向它的父母”时,你的意思是父母指向它的父母ID吗?如果是这种情况(我认为是这样),我不确定如何从表中推断出来,因为“parent_ID”不可用。只有 Parent_Item_no(这是部件号)。我确信这是可能的,我只是不知道该怎么做。我正在尝试这样的事情,但无济于事: if($parent_item_no == $oldparent_item_no){$parentid = $oldid;} else {$parentid = $id; $oldid = $id;}
  • 我应该添加一些细节:我正在尝试循环调用 CALL 的结果,并将该数据提供给与您在示例数组中提供的数据结构相匹配的数组(id,parentid , component_item_no)。上面的代码行在循环中,试图通过将 parent_item_number 与之前的 parent_item_no 进行比较来推断 parentid。
  • 是的,我的意思是 parent 是实际父母的 ID。也许你可以添加一些你实际使用的数据(也许使用 var_export 或其他东西),所以我可以修改示例代码。
  • 根据您所写的 ("parent_ID" isn't available. Only Parent_Item_no (which is the part number)) 我猜,您可以将 parent 更改为 Parent_Item_noid 与项目编号。唯一需要的是,无论您选择什么作为条目的 id,它都必须是唯一的。
  • 感谢您迄今为止的帮助:这是表格的示例:codepad.org/xZ2VLAH5
猜你喜欢
  • 1970-01-01
  • 2016-12-14
  • 2012-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
相关资源
最近更新 更多