可折叠动态BOM通过jquery? [英] Collapsible dynamic BOM via jquery?

查看:133
本文介绍了可折叠动态BOM通过jquery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php页面从代表产品的BOM(Bill of Material)的sql2000存储过程中提取数据。现在它显示为一个平台,工作伟大。然而,经过3天的谷歌/黑客攻击,我不能为我的生活找出数据转换成缩进可折叠/展开表或UL。



表格如下:




  • ID [< - unique]

  • BOM_Level

  • Parent_Item

  • Component_Item

  • Component_qty


b $ b

BOM_Level就是BOM的级别(0-12之间的数字)。我假设,因为这是提供它将是简单的,因为bom_level真的表示每个部分的缩进水平,但唉,它不是简单的....



一般结构如下,但部件数量和级别会因产品而异:

  0顶级
1 - 大会1
2 - 大会1的第1部分
2 - 大会1的第2部分
2 - 大会1的第3部分
2 - 大会第4部分1
1 - 装配2
2 - 装配2的装配1
3 - 装配2的装配1的第1部分
3 - 装配2的装配1的第2部分
3 - Assembly 2的第1部分的第3部分

我一直在尝试做:




  • 如果bom_level = == previous_bom_level,则创建li

  • 如果bom_level> previous_bom_level添加新的UL并开始新的li

  • 如果BOM级别是<



如果这样说,我就不能相当包装我的头如何做到这一点。任何想法?

解决方案

假设你有这样的数据:

  //每个条目加上任何额外的数据
$ 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'=& ',
'parent_item_no'=>'2000CF-12',
'comp_item_no'=>'0578',
),
数组(
'id '=>'190',
'bom_level'=>'4',
'parent_item_no'=>'2000CF-12',
'comp_item_no'=& -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'=& ',
'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'=& ',
'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'=& ',
'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'=& ',
'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>';
}
}

/ **
注意,'2800CF'这里是手动选择
*,因为它有最低的bom_level!在你真正的
*代码中,你可能想要捕获这些节点,而
*重组数组(见上文)
* /
helper($ map ['2800CF'],$地图);

生成嵌套ul。示例(针对上述数据):


  • 2800CF-02
    • 2000CF- <>
    • 2000CF-SH12
        2000 2000CF-SH12
  • 2000CF-AG01
    • 303025-20
    >
  • 0500
  • >
  • 050018-20
  • 2000CF-WB06
    • 040013-20

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


    I have a php page pulling data from a sql2000 stored procedure representing the BOM (Bill of Material) for a product. Right now it's displaying as a flat table, and works great. However, after 3 days of googling/hacking, I can't for the life of me figure out how tranform that data into an indented collapsable/expandable table or UL.

    Table is as follows:

    • ID[<--unique]
    • BOM_Level
    • Parent_Item
    • Component_Item
    • Component_qty

    The BOM_Level is exactly that, the level of the BOM (a number between 0-12). I was assuming because that was provided it would be simple, in that the bom_level really represents the level of indentation of each part, but alas, it has not been simple....

    The general structure is as follows, but the number of parts and levels will vary by product:

    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
    

    I've been trying to do something like:

    • If the bom_level is == previous_bom_level then create li
    • If the bom_level is > previous_bom_level add a new UL and start new li
    • If the BOM level is < previous bom level, wrap up the previous UL and start another set of li.

    With all that being said, I just can't quite wrap my head around how to do this. Any ideas?

    解决方案

    Suppose you have data like this:

    // 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',
      ),
    );
    

    Where comp_item_no is unique, and parent_item_no points to it's parent. With parent_item_no=0 being a root node.

    You can map this to another structure with:

    $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;
    }
    

    Which in turn can be recursively traversed, with something like this:

    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); 
    

    to produce a nested ul. Example (for the data from above):

    • 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

    Demo: http://codepad.org/FJwX3Z1c

    这篇关于可折叠动态BOM通过jquery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆