将多级散列引用转换为带有perl中父子关系的HTML表

时间:2013-08-22 09:58:28

标签: perl hash ref

我有一个具有父子关系的多级哈希引用,我希望将该哈希转换为包含所有关系的简单HTML表。 我的哈希看起来像:

$VAR1 = {
          '4' => {
                   'forumid' => '136720',
                   'children' => {
                                   '7' => {
                                            'forumid' => '136997',
                                            'title' => 'under category',
                                            'is_category' => '0',
                                            'parentid' => '136720'
                                          }
                                 },
                   'title' => 'Android',
                   'is_category' => '0',
                   'parentid' => '-1'
                 },
          '1' => {
                   'forumid' => '136666',
                   'children' => {
                                   '5' => {
                                            'forumid' => '136954',
                                            'children' => {
                                                            '8' => {
                                                                     'forumid' => '137004',
                                                                     'title' => 'child of child',
                                                                     'is_category' => '0',
                                                                     'parentid' => '136954'
                                                                   }
                                                          },
                                            'title' => 'add child',
                                            'is_category' => '0',
                                            'parentid' => '136666'
                                          }
                                 },
                   'title' => 'Main Forum',
                   'is_category' => '0',
                   'parentid' => '-1'
                 },
          '3' => {
                   'forumid' => '136719',
                   'title' => 'Nokia C2-01',
                   'is_category' => '1',
                   'parentid' => '-1'
                 },
          '2' => {
                   'forumid' => '136665',
                   'children' => {
                                   '6' => {
                                            'forumid' => '136994',
                                            'children' => {
                                                            '9' => {
                                                                     'forumid' => '137012',
                                                                     'title' => 'another child',
                                                                     'is_category' => '0',
                                                                     'parentid' => '136994'
                                                                   }
                                                          },
                                            'title' => 'sub form under test forum',
                                            'is_category' => '0',
                                            'parentid' => '136665'
                                          }
                                 },
                   'title' => 'test',
                   'is_category' => '0',
                   'parentid' => '-1'
                 }
        };

我正在使用以下函数来创建一些HTML表格:

sub adminList {

    my $hash = shift;
    my $options = '';
    my $iter;
    $options .= "<table id='sortable' class='grid' >
                 <tbody>";  
    $iter = sub {
        my $hash = shift;
        my $indent = shift || '';
        foreach my $k (sort keys %{$hash}) {  
            my $v = $hash->{$k};
            $options .= "<tr><td>$v->{title}" ;
            if($indent){
                $options .= "<table id='sort'>
                                <tbody>
                                    <tr>
                                        <td> $indent $v->{title}</td>
                                    </tr>
                                </tbody>
                            </table>                            
                ";
            }           
            if ($v->{children}){
                $iter->($v->{children}, $indent . "--");
            }
            $options .= "</td></tr>";
        }
    };
    $iter->($hash);
    $options .="</tbody></table>";
    return $options;
}

这不会给我想要的结果并重复标题两次而不是父母子女关系。输出应该像表格一样:

Main Forum
-- add child
---- child of child
test
-- sub form under test forum
---- another child
Nokia C2-01
Android
-- under category 

我无法弄清楚我错过了什么?任何帮助,将不胜感激。 创建的表应该像:

<table id="sortable" class="grid" >
    <tbody>
        <tr>           
            <td><label>house-Five</label>
                <table id="sort">
                <tbody>
                    <tr>                    
                     <td>-- child1111</td>
                    </tr>                
                    <tr>                        
                        <td><label>-- child22222</label>
                            <table id="sort">
                            <tbody>
                                <tr><td>---- first child of child22222</td></tr>
                                <tr>
                                    <td>---- second child of child22222
                                            <table id="sort">
                                                <tbody>
                                                    <tr><td>------ first child of second child222222</td></tr>
                                                    <tr><td>------ second child of child22222222</td></tr>
                                                </tbody>
                                            </table>
                                    </td>
                                </tr>
                                </tbody>
                            </table>

                        </td>
                    </tr>
                     <tr>                       
                        <td>-- child33333</td>
                    </tr>
                </tbody>                
                </table>           
            </td>            
        </tr>

        <tr><td>Psition four</td></tr>
        <tr><td>catch me</td></tr>
        <tr><td>Champions at six</td></tr>
        <tr><td>Rosewater</td></tr>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

您要添加标题两次。只需更改以下行

即可
$options .= "<tr><td>$v->{title}";

 $options .= "<tr><td>$v->{title}" unless $indent;

您可能希望将测试合并到以下if中,并将其设为if-else。