-
Notifications
You must be signed in to change notification settings - Fork 6
/
cars.go
58 lines (42 loc) · 1.33 KB
/
cars.go
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
package carsdemo
import (
"bytes"
"encoding/gob"
"html/template"
"github.com/EngineerKamesh/igb/igweb/shared/models"
"honnef.co/go/js/dom"
"honnef.co/go/js/xhr"
)
const CarItemTemplate = `
<td class="mdl-data-table__cell--non-numeric">{{.ModelName}}</td>
<td class="mdl-data-table__cell--non-numeric">{{.Color}}</td>
<td class="mdl-data-table__cell--non-numeric">{{.Manufacturer}}</td>
`
var D = dom.GetWindow().Document()
func InitializePage() {
cars()
}
func cars() {
nano := models.Car{ModelName: "Nano", Color: "Yellow", Manufacturer: "Tata"}
ambassador := models.Car{ModelName: "Ambassador", Color: "White", Manufacturer: "HM"}
omni := models.Car{ModelName: "Omni", Color: "Red", Manufacturer: "Maruti Suzuki"}
cars := []models.Car{nano, ambassador, omni}
autoTableBody := D.GetElementByID("autoTableBody")
for i := 0; i < len(cars); i++ {
trElement := D.CreateElement("tr")
tpl := template.New("template")
tpl.Parse(CarItemTemplate)
var buff bytes.Buffer
tpl.Execute(&buff, cars[i])
trElement.SetInnerHTML(buff.String())
autoTableBody.AppendChild(trElement)
}
var carsDataBuffer bytes.Buffer
enc := gob.NewEncoder(&carsDataBuffer)
enc.Encode(cars)
xhrResponse, err := xhr.Send("POST", "/cars-data", carsDataBuffer.Bytes())
if err != nil {
println(err)
}
println("xhrResponse: ", string(xhrResponse))
}