Thread Gruppierungsproblematik, wie lösen? (9 answers)
Opened by mandawar at 2008-02-04 12:51

murphy
 2008-02-05 18:14
#105594 #105594
User since
2004-07-19
1776 Artikel
HausmeisterIn
[Homepage]
user image
mandawar+2008-02-05 15:33:17--
[...]
Es reicht also wenn der Artikelkatalog angezeigt wird, und Daten müssen an sich nicht verändert werden.


Beispiel wie man es machen könnte:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use 5.008;
use strict;
use warnings;

use IO::Handle;
use Text::CSV;
use JSON;

# A nested hash of groups
my %hierarchy;

# Parse the CSV input data into %hierarchy
{
    my $csv = new Text::CSV();

    while (my $fields = $csv->getline(\*DATA)) {
        my ($article, @groups) = @$fields;
        
        my $anchor = \%hierarchy;
        foreach (@groups) {
            $anchor->{$_}->{__name__} = $_;
            $anchor = $anchor->{$_}
        }

        push @{$anchor->{__articles__}}, $article;
    }

    die sprintf("Error parsing CSV input: %s\n", $csv->error_diag())
        unless ($csv->eof());
}

# Convert the hierarchy data into JS format
my $hierarchy_json = do {
    my $json = new JSON();
    $json->utf8(1)->pretty(1);

    $json->encode(\%hierarchy);
};

# Print the HTML page
print <<EOD
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Article Catalog</title>
  <script type="text/javascript"><![CDATA[
    /* Group hierarchy generated by Perl script */
    var hierarchy = $hierarchy_json;

    /* Group listboxes */
    var group0;
    var group1;
    var group2;

    /* Articles list */
    var articles;

    /* Function to fill a group selection box */
    function fillInGroups(group, data) {
      while (group.options.length > 0)
        group.options[group.options.length - 1] = null;

      for (var name in data)
        if (!name.match(/^__/))
          group.options[group.options.length] =
            new Option(name, name, true, false);

      group.data = data;
    }

    /* Function to fill a groups selection box according to its
     * parent group selection */
    function fillInSubgroups(group, parent) {
      fillInGroups(group, parent.data[parent.value])
    }

    /* Function to fill in the article data according to the group
     * selection */
    function fillInArticles(group) {
      while (articles.firstChild != null)
        articles.removeChild(articles.firstChild);

      for each (var article in group.data[group.value].__articles__) {
        var text = document.createTextNode(article);
        var item = document.createElement("li");
        item.appendChild(text);
        articles.appendChild(item);
      }
    }

    /* Initialization when the page has been loaded */
    function init() {
      group0 = document.getElementById("group0");
      group1 = document.getElementById("group1");
      group2 = document.getElementById("group2");

      articles = document.getElementById("articles");

      fillInGroups(group0, hierarchy);
    }
  ]]></script>
</head>
<body onload="init()">
  <h1>Article Catalog</h1>

  <h2>Group Selection</h2>
  <form action="javascript:alert('This%20is%20just%20a%20widget%20container!')">
    <select id="group0" size="1" onchange="fillInSubgroups(group1, group0)"/>
    <select id="group1" size="1" onchange="fillInSubgroups(group2, group1)"/>
    <select id="group2" size="1" onchange="fillInArticles(group2)"/>
  </form>

  <h2>Articles</h2>
  <ul id="articles"/>
</body>
</html>
EOD

__DATA__
42,Rhabarber,Quark,Quatsch
55,Rhabarber,Blubb,Schmarrn
27,Rhabarber,Quark,Stuss
38,Bananenbrei,Unfug,Kaese
67,Bananenbrei,Unsinn,Irgendwas


Wenn der Browser auf dem es laufen soll keinen ganz aktuellen JavaScript-Interpreter hat, muss man eventuell
Code: (dl )
for each (var article in group.data[group.value].__articles__) {

durch etwas wie
Code: (dl )
1
2
3
var array = group.data[group.value].__articles__;
for (var index in array) {
var article = array[index];

ersetzen.
When C++ is your hammer, every problem looks like your thumb.

View full thread Gruppierungsproblematik, wie lösen?