page.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package e10_vuetify_autocomplete
  2. // @snippet_begin(VuetifyAutoCompleteSample)
  3. import (
  4. "fmt"
  5. "github.com/qor5/admin/presets"
  6. "github.com/qor5/admin/presets/gorm2op"
  7. . "github.com/qor5/ui/vuetify"
  8. "github.com/qor5/ui/vuetifyx"
  9. "github.com/qor5/web"
  10. h "github.com/theplant/htmlgo"
  11. "gorm.io/driver/sqlite"
  12. "gorm.io/gorm"
  13. )
  14. type myFormValue struct {
  15. Values1 []string
  16. Values2 []string
  17. Value3 string
  18. }
  19. type User struct {
  20. Login string
  21. Name string
  22. }
  23. var selectedItems1 = []*User{
  24. {Login: "sam", Name: "Sam"},
  25. {Login: "charles", Name: "Charles"},
  26. }
  27. var options1 = []*User{
  28. {Login: "sam", Name: "Sam"},
  29. {Login: "john", Name: "John"},
  30. {Login: "charles", Name: "Charles"},
  31. }
  32. var selectedItems2 = []*User{
  33. {Login: "charles", Name: "Charles"},
  34. }
  35. var selectedItems3 = []*User{
  36. {Login: "charles", Name: "Charles"},
  37. }
  38. var options2 = []*User{
  39. {Login: "sam", Name: "Sam"},
  40. {Login: "john", Name: "John"},
  41. {Login: "charles", Name: "Charles"},
  42. }
  43. var globalState = &myFormValue{
  44. Values1: []string{
  45. "sam",
  46. "charles",
  47. },
  48. Values2: []string{
  49. "charles",
  50. },
  51. Value3: "charles",
  52. }
  53. type Product struct {
  54. ID uint `gorm:"primarykey"`
  55. Name string
  56. }
  57. var loadMoreRes *vuetifyx.AutocompleteDataSource
  58. var pagingRes *vuetifyx.AutocompleteDataSource
  59. var ExamplePreset *presets.Builder
  60. func init() {
  61. db, err := gorm.Open(sqlite.Open("/tmp/my.db"), &gorm.Config{})
  62. if err != nil {
  63. panic(err)
  64. }
  65. db.AutoMigrate(&Product{})
  66. db.Where("1=1").Delete(&Product{})
  67. for i := 1; i < 300; i++ {
  68. db.Create(&Product{Name: fmt.Sprintf("Product %d", i)})
  69. }
  70. ExamplePreset = presets.New()
  71. ExamplePreset.URIPrefix(VuetifyAutoCompletePresetPath).DataOperator(gorm2op.DataOperator(db))
  72. listing := ExamplePreset.Model(&Product{}).Listing()
  73. loadMoreRes = listing.ConfigureAutocompleteDataSource(
  74. &presets.AutocompleteDataSourceConfig{
  75. OptionValue: "ID",
  76. OptionText: "Name",
  77. KeywordColumns: []string{
  78. "Name",
  79. },
  80. PerPage: 50,
  81. },
  82. "loadMore",
  83. )
  84. pagingRes = listing.ConfigureAutocompleteDataSource(
  85. &presets.AutocompleteDataSourceConfig{
  86. OptionValue: "ID",
  87. OptionText: "Name",
  88. KeywordColumns: []string{
  89. "Name",
  90. },
  91. PerPage: 20,
  92. IsPaging: true,
  93. OrderBy: "Name",
  94. },
  95. "paging",
  96. )
  97. }
  98. func VuetifyAutocomplete(ctx *web.EventContext) (pr web.PageResponse, err error) {
  99. result := h.Ul()
  100. for _, v := range globalState.Values1 {
  101. result.AppendChildren(h.Li().Text(v))
  102. }
  103. pr.Body = VContainer(
  104. h.H1("An auto complete that you can select multiple options with static data"),
  105. VAutocomplete().
  106. Items(options1).
  107. FieldName("Values1").
  108. ItemText("Name").
  109. ItemValue("Login").
  110. Label("Static Options").
  111. Value(globalState.Values1),
  112. result,
  113. h.H1("An auto complete that you can select multiple options from data source by loading more"),
  114. vuetifyx.VXAutocomplete().
  115. FieldName("Values2").
  116. Label("Load options from data source").
  117. SetDataSource(loadMoreRes),
  118. h.H1("An auto complete that you can select multiple options from data source resource by paging"),
  119. vuetifyx.VXAutocomplete().
  120. FieldName("Values2").
  121. Label("Load options from data source").
  122. SetDataSource(pagingRes),
  123. h.H1("VSelect"),
  124. VSelect().
  125. Items(options1). // Items is the data source
  126. ItemText("Name"). // ItemText is the value that would be displayed to user. the argument is the corresponding field name in the Items. here is user.Name
  127. ItemValue("Login"). // ItemValue is the value that will be passed with the form. same with ItemText, here is user.Login
  128. FieldName("Value3").
  129. Solo(true).
  130. Value(globalState.Value3),
  131. h.Pre(globalState.Value3),
  132. VBtn("Update").
  133. Color("success").
  134. OnClick("update"),
  135. )
  136. return
  137. }
  138. func update(ctx *web.EventContext) (r web.EventResponse, err error) {
  139. globalState = &myFormValue{}
  140. ctx.MustUnmarshalForm(globalState)
  141. selectedItems1 = []*User{}
  142. for _, login := range globalState.Values1 {
  143. for _, u := range options1 {
  144. if u.Login == login {
  145. selectedItems1 = append(selectedItems1, u)
  146. }
  147. }
  148. }
  149. selectedItems2 = []*User{}
  150. for _, login := range globalState.Values2 {
  151. for _, u := range options2 {
  152. if u.Login == login {
  153. selectedItems2 = append(selectedItems2, u)
  154. }
  155. }
  156. }
  157. selectedItems3 = []*User{}
  158. for _, u := range options1 {
  159. if u.Login == globalState.Value3 {
  160. selectedItems3 = append(selectedItems3, u)
  161. }
  162. }
  163. r.Reload = true
  164. return
  165. }
  166. var VuetifyAutocompletePB = web.Page(VuetifyAutocomplete).
  167. EventFunc("update", update)
  168. const VuetifyAutoCompletePath = "/samples/vuetify-auto-complete"
  169. const VuetifyAutoCompletePresetPath = "/samples/vuetify-auto-complete-preset"
  170. // @snippet_end