web-scope.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package e00_basics
  2. import (
  3. "github.com/qor5/docs/docsrc/utils"
  4. . "github.com/qor5/ui/vuetify"
  5. "github.com/qor5/web"
  6. . "github.com/theplant/htmlgo"
  7. )
  8. // @snippet_begin(WebScopeUseLocalsSample1)
  9. func UseLocals(ctx *web.EventContext) (pr web.PageResponse, err error) {
  10. pr.Body = VCard(
  11. VBtn("Test Can Not Change Other Scope").Attr("@click", `locals.btnLabel = "YES"`),
  12. web.Scope(
  13. VCard(
  14. VBtn("").
  15. Attr("v-text", "locals.btnLabel").
  16. Attr("@click", `
  17. if (locals.btnLabel == "Add") {
  18. locals.items.push({text: "B", icon: "done"});
  19. locals.btnLabel = "Remove";
  20. } else {
  21. locals.items.pop();
  22. locals.btnLabel = "Add";
  23. }`),
  24. VList(
  25. VSubheader(
  26. Text("REPORTS"),
  27. ),
  28. VListItemGroup(
  29. VListItem(
  30. VListItemIcon(
  31. VIcon("").Attr("v-text", "item.icon"),
  32. ),
  33. VListItemContent(
  34. VListItemTitle().Attr("v-text", "item.text"),
  35. ),
  36. ).Attr("v-for", "(item, i) in locals.items").
  37. Attr("x-bind:key", "i"),
  38. ).Attr("v-model", "locals.selectedItem").
  39. Attr("color", "primary"),
  40. ).Attr("dense", ""),
  41. ).Class("mx-auto").
  42. Attr("max-width", "300").
  43. Attr("tile", ""),
  44. ).Init(`{ selectedItem: 1, btnLabel:"Add", items: [{text: "A", icon: "clock"}]}`).
  45. VSlot("{ locals }"),
  46. )
  47. return
  48. }
  49. var UseLocalsPB = web.Page(UseLocals)
  50. // @snippet_end
  51. // @snippet_begin(WebScopeUsePlaidFormSample1)
  52. var materialID, materialName, rawMaterialID, rawMaterialName, countryID, countryName, productName string
  53. func UsePlaidForm(ctx *web.EventContext) (pr web.PageResponse, err error) {
  54. pr.Body = Div(
  55. H3("Form Content"),
  56. utils.PrettyFormAsJSON(ctx),
  57. Div(
  58. Div(
  59. Fieldset(
  60. Legend("Product Form"),
  61. Div(
  62. Label("Product Name"),
  63. Input("").Value(productName).Type("text").Attr(web.VFieldName("ProductName")...),
  64. ),
  65. Div(
  66. Label("Material ID"),
  67. Input("").Value(materialID).Type("text").Disabled(true).Attr(web.VFieldName("MaterialID")...),
  68. ),
  69. web.Scope(
  70. Fieldset(
  71. Legend("Material Form"),
  72. Div(
  73. Label("Material Name"),
  74. Input("").Value(materialName).Type("text").Attr(web.VFieldName("MaterialName")...),
  75. ),
  76. Div(
  77. Label("Raw Material ID"),
  78. Input("").Value(rawMaterialID).Type("text").Disabled(true).Attr(web.VFieldName("RawMaterialID")...),
  79. ),
  80. web.Scope(
  81. Fieldset(
  82. Legend("Raw Material Form"),
  83. Div(
  84. Label("Raw Material Name"),
  85. Input("").Value(rawMaterialName).Type("text").Attr(web.VFieldName("RawMaterialName")...),
  86. ),
  87. Button("Send").Style(`background: orange;`).Attr("@click", web.POST().EventFunc("updateValue").Go()),
  88. ).Style(`background: orange;`),
  89. ).VSlot("{ plaidForm }"),
  90. Button("Send").Style(`background: brown;`).Attr("@click", web.POST().EventFunc("updateValue").Go()),
  91. ).Style(`background: brown;`),
  92. ).VSlot("{ plaidForm }"),
  93. Div(
  94. Label("Country ID"),
  95. Input("").Value(countryID).Type("text").Disabled(true).Attr(web.VFieldName("CountryID")...),
  96. ),
  97. web.Scope(
  98. Fieldset(
  99. Legend("Country Of Origin Form"),
  100. Div(
  101. Label("Country Name"),
  102. Input("").Value(countryName).Type("text").Attr(web.VFieldName("CountryName")...),
  103. ),
  104. Button("Send").Style(`background: red;`).Attr("@click", web.POST().EventFunc("updateValue").Go()),
  105. ).Style(`background: red;`),
  106. ).VSlot("{ plaidForm }"),
  107. Div(
  108. Button("Send").Style(`background: grey;`).Attr("@click", web.POST().EventFunc("updateValue").Go())),
  109. ).Style(`background: grey;`)),
  110. ).Style(`width:600px;`),
  111. )
  112. return
  113. }
  114. func updateValue(ctx *web.EventContext) (er web.EventResponse, err error) {
  115. ctx.R.ParseForm()
  116. if v := ctx.R.Form.Get("ProductName"); v != "" {
  117. productName = v
  118. }
  119. if v := ctx.R.Form.Get("MaterialName"); v != "" {
  120. materialName = v
  121. materialID = "66"
  122. }
  123. if v := ctx.R.Form.Get("RawMaterialName"); v != "" {
  124. rawMaterialName = v
  125. rawMaterialID = "88"
  126. }
  127. if v := ctx.R.Form.Get("CountryName"); v != "" {
  128. countryName = v
  129. countryID = "99"
  130. }
  131. er.Reload = true
  132. return
  133. }
  134. var UsePlaidFormPB = web.Page(UsePlaidForm).
  135. EventFunc("updateValue", updateValue)
  136. // @snippet_end
  137. const WebScopeUseLocalsPagePath = "/samples/web-scope-use-locals"
  138. const WebScopeUsePlaidFormPagePath = "/samples/web-scope-use-plaid-form"