我想迭代以下多级哈希引用并希望创建父->子关系以及拖放功能。以下是我从转储器获得的哈希引用。所以我想创建一个部分并显示所有父子关系信息在其中我只能将子元素拖放到其父元素中,还可以拖放每个父元素以更改显示顺序。但我的首要任务是正确显示父->子关系。
哈希如下:
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'> </div>
<div class='item_info'>
<img src='/images/icons/folder.png' />
<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'> </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;
}