Dropdown-Menu with optgroup
i am trying to create a dynamic dropdown-menu that receives its entries out of an xml-file at script-startup. first i tried a static version like this:
Tr(td([popup_menu( -name=>'betreff', -values=>[optgroup(-name=>'Mäde开发者_开发知识库ls',
-values=>['Susi','Steffi',''], -labels=>{'Susi'=>'Petra','Steffi'=>'Paula'})
,optgroup(-name=>'Jungs', -values=>['moe', 'catch',''])])]));
that worked fine.
The prob starts when i try to put the -values-parameter of popup_menu into a scalar variable. Should somehow lokk similar to that one:
$popup_values = "[optgroup(-name=>'Mädels', -values=>['Susi','Steffi',''],
-labels=>{'Susi'=>'Petra','Steffi'=>'Paula'}),optgroup(-name=>'Jungs',
-values=>['moe', 'catch',''])]"
or with single quotation marks.
The goal is to build that string by concatenating the syntax-corrected elements of the xml-file. Thats because i do not know a priori how many optgroups or list elements within the optgroups will exist. Any idea?
Thx in advance
Jochen
So you have an XML file which you use to generate that string? Why not directly generate the data structure necessary for the popup_menu
call? It's just an array (you can call optgroup
while "analysing" the XML file)
If you really want to use the string-solution then you could use eval to transform the string to the data structure. Though this solution has certain security issues.
Reading From XML-File
Here's an example of how to transform form XML to the optgroup, this of course depends on how your XML-file looks like.
use strict;
use warnings;
use XML::Simple;
use CGI qw/:standard/;
my $xmlString = join('', <DATA>);
my $xmlData = XMLin($xmlString);
my @popup_values;
foreach my $group (keys(%{$xmlData->{group}})) {
my (@values, %labels);
my $options = $xmlData->{group}->{$group}->{opt};
foreach my $option (keys(%{$options})) {
push @values, $option;
if(exists($options->{$option}->{label}) &&
'' ne $options->{$option}->{label}) {
$labels{$option} = $options->{$option}->{label};
}
}
push @popup_values, optgroup(-name => $group,
-labels => \%labels,
-values => \@values
);
}
print popup_menu(-name=>'betreff', -values=> \@popup_values);
__DATA__
<?xml version="1.0" encoding="UTF-8" ?>
<dropdown>
<group name="Mädels">
<opt name="Susi" label="Petra"/>
<opt name="Steffi" label="Paula"/>
<opt name="" />
</group>
<group name="Jungs">
<opt name="moe" />
<opt name="catch" />
<opt name="" />
</group>
</dropdown>
精彩评论