var headerTmpl = "<tr>\n{{ . }}</tr>\n"
var headerRowTmpl = "\t<th{{ if (eq .Align 1) }}align='center'{{ else if (eq .Align 2) }}align='right'{{ end }}>{{ .Title }}</th>\n"
+var rowTmpl = "<tr>\n{{ . }}</tr>\n"
+var rowColTmpl = "\t<td{{ if (eq .Align 1) }}align='center'{{ else if (eq .Align 2) }}align='right'{{ end }}>{{ .Value }}</td>\n"
+
type grid struct {
cols []column
headerTmpl string
headerRowTmpl string
+ rowTmpl string
+ rowColTmpl string
}
func (g *grid) addColumn (c column) {
}
func (g *grid) getRowFromMap (m map[string]interface{}) string {
- ret := "<tr>\n"
+ rtmpl := g.rowTmpl
+ if rtmpl == "" {
+ rtmpl = rowTmpl
+ }
+
+ t, err := template.New("row").Parse(rtmpl)
+ if err != nil {
+ fmt.Println(err)
+ return ""
+ }
+
+ rctmpl := g.rowColTmpl
+ if rctmpl == "" {
+ rctmpl = rowColTmpl
+ }
+
+ tc, err := template.New("rowCol").Parse(rctmpl)
+ if err != nil {
+ fmt.Println(err)
+ return ""
+ }
+
+ ret := ""
for _, c := range g.cols {
v, ok := m[c.Name]
}
}
- align := ""
- if c.Align == 1 {
- align = " align='center'"
- } else if c.Align == 2 {
- align = " align='right'"
- }
+ m := make(map[string]interface{})
+ m["Name"] = c.Name
+ m["Align"] = c.Align
+ m["Value"] = v
- ret += fmt.Sprintf("\t<td%s>%v</td>\n", align, v)
+ var b bytes.Buffer
+ err = tc.Execute(&b, m)
+
+ ret += b.String()
}
- ret += "</tr>\n"
+ var b bytes.Buffer
+ err = t.Execute(&b, ret)
+ if err != nil {
+ fmt.Println(err)
+ return ""
+ }
- return ret
+ return b.String()
}
func missing (row map[string]interface{}) interface{} {
g.headerTmpl = "<thead><tr>\n{{ . }}</tr></thead>\n"
+ g.rowColTmpl = "\t<td data-name='{{.Name}}' {{ if (eq .Align 1) }}align='center'{{ else if (eq .Align 2) }}align='right'{{ end }}>{{ .Value }}</td>\n"
+
c := column{Name: "id", Title: "ID"}
g.addColumn(c)