0

我想迭代以下多级哈希引用并希望创建父->子关系以及拖放功能。以下是我从转储器获得的哈希引用。所以我想创建一个部分并显示所有父子关系信息在其中我只能将子元素拖放到其父元素中,还可以拖放每个父元素以更改显示顺序。但我的首要任务是正确显示父->子关系。

哈希如下:

my $hash  = {
          '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'
                 }
        };

我需要我的输出如下:

<div id='136666'>Main Forum
    <div>
        <a href='136954'>add child</a>, <a href='137004'>child of child</a>
    </div>
</div>
<div id='136665'>test
    <div>
        <a href='136994'>sub form under test forum</a>, <a href='137012'>another child</a>
    </div>  
</div>
<div id='136719'>Nokia C2-01
    <div id='136720'>Android
        <div>
            <a href='136997'>under category</a> 
        </div>  
    </div>  
</div>

所以它应该看起来像 ie 应该添加到用逗号分隔的父元素中:

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

这是创建哈希的函数:

     sub subforumsTree {
    my $forums = shift; 
    my $pid = shift || ('-1' || '0');
    my $HASH;   
    foreach my $forum ( @{$forums}){
        #next if ($forum->{is_category});           
        if($forum->{parentid} eq $pid){
            $HASH->{$forum->{displayorder}} = {forumid=>$forum->{forumid},parentid=>$forum->{parentid},title=>$forum->{title},is_category=>$forum->{is_category}};  
            my $children =  &subforumsTree( $forums,$forum->{forumid} );
            if( $children ) {
               $HASH->{$forum->{displayorder}}->{'children'} = $children;
            }
        }
    }

     return $HASH;
}

here is the function which I tried for displaying elements:
sub adminforumsList {

    my $hash = shift;
    my $options = '';
    my $iter; 
    $iter = sub {
        my $hash = shift;
        my $indent = shift || '';
        foreach my $k (sort keys %{$hash}) {  
            my $v = $hash->{$k};
                        $options .= "<div id=cat_".$v->{forumid}." class=isDraggable>
                        <div class='col_buttons right' style='white-space: nowrap'>
                        <ul class='ipsControlStrip'>
                        <li class='i_add'>
                        <a href='#' title='New Forum...'>New Forum...</a>
                        </li>
                        <li class='i_edit'>
                        <a href='#' title='Edit Settings...'>Edit Settings...</a>
                        </li>
                        <li class='ipsControlStrip_more ipbmenu' id='menum-26'>
                        <a href='#'>Options</a>
                        </li>
                        </ul>
                        <ul class='acp-menu' id='menum-26_menucontent' style='display: none'>
                        <li class='icon info'><a href='#'>Permissions...</a></li>
                        <li class='icon delete'><a href='#'>Delete Category...</a></li>
                        <li class='icon edit'><a href='#'>Edit as Forum...</a></li>
                        <li class='icon view'><a href='#'>Skin Options...</a></li>
                        </ul>
                        </div><div class='draghandle'>&nbsp;</div>
                        <div class='item_info'>
                        <img src='/images/icons/folder.png' />
                        &nbsp;&nbsp;<strong class='larger_text'>$v->{title}</strong>                
                        </div>";
                        $options .= "<div id='cat_wrap_".$v->{forumid}. "' class='item_wrap'>";
                        $options .= "<div class='item ipsControlRow isDraggable' id='forum_".$v->{forumid}."'>";
                        $options .= "<table style='width: 100%'>
                        <tr>
                        <td style='width: 20px !important; vertical-align: top;'>
                            <div class='draghandle'>&nbsp;</div>
                        </td>
                        <td style=''>
                            <div class='item_info'>
                                <strong class='forum_name'>$v->{title}</strong>
                                <br />
                                <span class='desctext'>$v->{description}</span>
                            </div>
                            </td>                           
                        </tr>
                        </table></div></div></div>";

            if ($v->{children}){
                $iter->($v->{children}, $indent . "--");
            }
        }
    };
    $iter->($hash);
    return $options;
}
4

1 回答 1

0

您没有展示您尝试过的内容,并且您的问题并不完全清楚,这可能意味着您的问题将被关闭。

我不会为你做太多的编程,但我建议你看看Perl Object Oriented Programming

每个论坛可以是一个对象,其中一个字段可以是其他论坛的哈希。如果您已经了解引用,那么编程 OO Perl 并不难。通过将每个论坛定义为论坛类的成员,您可以消除对散列进行操作的棘手部分。

我还建议您将问题分解成更小的部分。一旦您掌握了代码的基础知识,就可以添加拖放功能。你想输出 XML 吗?目前尚不清楚您要做什么。如果您只想存储结构,可以使用Storable。如果您将论坛结构传递给它,它会将整个结构存储在一个文件中,并且您可以将其读回。对于这些类型的任务,Storable 比 XML 更容易使用,而且它是一个标准模块。

但是,如果您必须在 XML 中使用它,您应该使用Perl 中可用的众多XML 模块之一

于 2013-08-14T12:08:31.590 回答