root/java/sql-vyuka/src/java/cz/frantovo/sql/vyuka/dto/Tabulka.java @ 12:1b10a6565e8c

Revision 12:1b10a6565e8c, 1.9 KB (checked in by František Kučera <franta-hg@…>, 15 years ago)

Propojení javascriptu a servletu.

Line 
1package cz.frantovo.sql.vyuka.dto;
2
3import cz.frantovo.sql.vyuka.Html;
4import java.util.ArrayList;
5import java.util.Collection;
6
7/**
8 * Tabulka, která je výsledkem SQL dotazu.
9 * @author fiki
10 */
11public class Tabulka implements HtmlObjekt {
12
13    private String[] zahlavi;
14    private Collection<Object[]> hodnoty = new ArrayList<Object[]>();
15
16    public String getHtml() {
17
18        if (getZahlavi() == null || getHodnoty() == null || getZahlavi().length < 1) {
19            return "<p>Chybná tabulka</p>";
20        } else {
21
22            StringBuffer html = new StringBuffer();
23
24            html.append("<table>");
25
26
27            html.append("<thead title=\"Chceš setřídit výsledek podle nějakého sloupce? Co takhle ORDER BY sloupec.\">");
28            html.append("<tr>");
29            for (String z : getZahlavi()) {
30                html.append("<td>" + Html.escapuj(z) + "</td>");
31            }
32            html.append("</tr>");
33            html.append("</thead>");
34
35
36            html.append("<tbody>");
37            for (Object[] hh : getHodnoty()) {
38                for (Object h : hh) {
39                    html.append(formatujRadek(h));
40                }
41            }
42            html.append("</tbody>");
43
44            html.append("</table>");
45
46
47            return html.toString();
48        }
49    }
50
51    private String formatujRadek(Object o) {
52        if (o instanceof Integer) {
53            return "<td class=\"cislo\">" + Html.escapuj(String.valueOf(o)) + "</td>";
54        } else {
55            return "<td>" + Html.escapuj(String.valueOf(o)) + "</td>";
56        }
57    }
58
59    public String[] getZahlavi() {
60        return zahlavi;
61    }
62
63    public void setZahlavi(String[] zahlavi) {
64        this.zahlavi = zahlavi;
65    }
66
67    public Collection<Object[]> getHodnoty() {
68        return hodnoty;
69    }
70
71    public void setHodnoty(Collection<Object[]> hodnoty) {
72        this.hodnoty = hodnoty;
73    }
74}
Note: See TracBrowser for help on using the browser.