--- /dev/null
+package zakgrid
+
+import (
+ "fmt"
+ "bytes"
+ "text/template"
+)
+
+type MissingCol func (map[string]interface{}) interface{}
+
+type Column struct {
+ Name string
+ Title string
+ Align int
+ ToCall MissingCol
+}
+
+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) {
+ g.cols = append(g.cols, c)
+}
+
+func (g *Grid) GetHeader () string {
+ htmpl := g.HeaderTmpl
+ if htmpl == "" {
+ htmpl = headerTmpl
+ }
+
+ t, err := template.New("header").Parse(htmpl)
+ if err != nil {
+ fmt.Println(err)
+ return ""
+ }
+
+ hrtmpl := g.HeaderRowTmpl
+ if hrtmpl == "" {
+ hrtmpl = headerRowTmpl
+ }
+
+ tr, err := template.New("headerRow").Parse(hrtmpl)
+ if err != nil {
+ fmt.Println(err)
+ return ""
+ }
+
+ header := ""
+
+ for i, v := range g.cols {
+ var b bytes.Buffer
+ err = tr.Execute(&b, v)
+
+ header += b.String()
+ fmt.Println(i, v)
+ }
+
+ var b bytes.Buffer
+ err = t.Execute(&b, header)
+ if err != nil {
+ fmt.Println(err)
+ return ""
+ }
+
+ return b.String()
+}
+
+func (g *Grid) GetRowFromMap (m map[string]interface{}) string {
+ type nc struct {
+ Column
+ Value interface{}
+ }
+
+ 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]
+ if !ok {
+ if c.ToCall != nil {
+ v = c.ToCall(m)
+ }
+ }
+
+ nnc := nc{Column: c, Value: v}
+ fmt.Println(nnc)
+
+ var b bytes.Buffer
+ err = tc.Execute(&b, nnc)
+
+ ret += b.String()
+ }
+
+ var b bytes.Buffer
+ err = t.Execute(&b, ret)
+ if err != nil {
+ fmt.Println(err)
+ return ""
+ }
+
+ return b.String()
+}
import (
"fmt"
"log"
- "bytes"
- "text/template"
"database/sql"
_ "github.com/mattn/go-sqlite3"
-)
-
-type missingCol func (map[string]interface{}) interface{}
-
-type Column struct {
- Name string
- Title string
- Align int
- toCall missingCol
-}
-
-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) {
- g.cols = append(g.cols, c)
-}
-
-func (g *grid) getHeader () string {
- htmpl := g.headerTmpl
- if htmpl == "" {
- htmpl = headerTmpl
- }
-
- t, err := template.New("header").Parse(htmpl)
- if err != nil {
- fmt.Println(err)
- return ""
- }
-
- hrtmpl := g.headerRowTmpl
- if hrtmpl == "" {
- hrtmpl = headerRowTmpl
- }
-
- tr, err := template.New("headerRow").Parse(hrtmpl)
- if err != nil {
- fmt.Println(err)
- return ""
- }
-
- header := ""
-
- for i, v := range g.cols {
- var b bytes.Buffer
- err = tr.Execute(&b, v)
-
- header += b.String()
- fmt.Println(i, v)
- }
-
- var b bytes.Buffer
- err = t.Execute(&b, header)
- if err != nil {
- fmt.Println(err)
- return ""
- }
- return b.String()
-}
-
-func (g *grid) getRowFromMap (m map[string]interface{}) string {
- type nc struct {
- Column
- Value interface{}
- }
-
- 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]
- if !ok {
- if c.toCall != nil {
- v = c.toCall(m)
- }
- }
-
- nnc := nc{Column: c, Value: v}
- fmt.Println(nnc)
-
- var b bytes.Buffer
- err = tc.Execute(&b, nnc)
-
- ret += b.String()
- }
-
- var b bytes.Buffer
- err = t.Execute(&b, ret)
- if err != nil {
- fmt.Println(err)
- return ""
- }
-
- return b.String()
-}
+ "example.com/zakgrid/grid"
+)
func missing (row map[string]interface{}) interface{} {
ret := "<button>Save</button>"
}
func main() {
- var g grid
+ var g zakgrid.Grid
- g.headerTmpl = "<thead><tr>\n{{ . }}</tr></thead>\n"
+ g.HeaderTmpl = "<thead><tr>\n{{ . }}</tr></thead>\n"
- g.rowColTmpl = "\t<td data-name='{{.Column.Name}}' {{ if (eq .Align 1) }}align='center'{{ else if (eq .Align 2) }}align='right'{{ end }}>{{ .Value }}</td>\n"
+ g.RowColTmpl = "\t<td data-name='{{.Column.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)
+ c := zakgrid.Column{Name: "id", Title: "ID"}
+ g.AddColumn(c)
- c = Column{Name: "name", Title: "Name"}
- g.addColumn(c)
+ c = zakgrid.Column{Name: "name", Title: "Name"}
+ g.AddColumn(c)
- c = Column{Name: "surname", Title: "Surname"}
- g.addColumn(c)
+ c = zakgrid.Column{Name: "surname", Title: "Surname"}
+ g.AddColumn(c)
- c = Column{Name: "age", Title: "Age"}
- g.addColumn(c)
+ c = zakgrid.Column{Name: "age", Title: "Age"}
+ g.AddColumn(c)
- c = Column{Name: "btn", Title: "Buttons"}
- c.toCall = missing
+ c = zakgrid.Column{Name: "btn", Title: "Buttons"}
+ c.ToCall = missing
c.Align = 2
- g.addColumn(c)
+ g.AddColumn(c)
fmt.Println (g)
- header := g.getHeader()
+ header := g.GetHeader()
fmt.Println(header)
fmt.Println(m)
- fmt.Println(g.getRowFromMap(m))
+ fmt.Println(g.GetRowFromMap(m))
}
}