page.go 878 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package e01_hello_button
  2. import (
  3. "github.com/qor5/web"
  4. . "github.com/theplant/htmlgo"
  5. )
  6. type mystate struct {
  7. Message string
  8. }
  9. func HelloButton(ctx *web.EventContext) (pr web.PageResponse, err error) {
  10. var s = &mystate{}
  11. if ctx.Flash != nil {
  12. s = ctx.Flash.(*mystate)
  13. }
  14. pr.Body = Div(
  15. Button("Hello").Attr("@click", web.POST().EventFunc("reload").Go()),
  16. Tag("input").
  17. Attr("type", "text").
  18. Attr("value", s.Message).
  19. Attr("@input", web.POST().
  20. EventFunc("reload").
  21. FieldValue("Message", web.Var("$event.target.value")).
  22. Go()),
  23. Div().
  24. Style("font-family: monospace;").
  25. Text(s.Message),
  26. )
  27. return
  28. }
  29. func reload(ctx *web.EventContext) (r web.EventResponse, err error) {
  30. var s = &mystate{}
  31. ctx.MustUnmarshalForm(s)
  32. ctx.Flash = s
  33. r.Reload = true
  34. return
  35. }
  36. var HelloButtonPB = web.Page(HelloButton).
  37. EventFunc("reload", reload)