page.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 (
  15. User struct {
  16. Login string
  17. Name string
  18. }
  19. UserIcons struct {
  20. Login string `json:"text"`
  21. Name string `json:"value"`
  22. Icon string `json:"icon"`
  23. }
  24. Product struct {
  25. ID uint `gorm:"primarykey"`
  26. Name string
  27. }
  28. )
  29. var (
  30. options = []*User{
  31. {Login: "sam", Name: "Sam"},
  32. {Login: "john", Name: "John"},
  33. {Login: "charles", Name: "Charles"},
  34. }
  35. iconOptions = []*UserIcons{
  36. {Login: "sam", Name: "Sam", Icon: "https://cdn.vuetifyjs.com/images/lists/1.jpg"},
  37. {Login: "john", Name: "John", Icon: "https://cdn.vuetifyjs.com/images/lists/2.jpg"},
  38. {Login: "charles", Name: "Charles", Icon: "https://cdn.vuetifyjs.com/images/lists/3.jpg"},
  39. }
  40. loadMoreRes *vuetifyx.AutocompleteDataSource
  41. pagingRes *vuetifyx.AutocompleteDataSource
  42. ExamplePreset *presets.Builder
  43. )
  44. func init() {
  45. db, err := gorm.Open(sqlite.Open("/tmp/my.db"), &gorm.Config{})
  46. if err != nil {
  47. panic(err)
  48. }
  49. db.AutoMigrate(&Product{})
  50. db.Where("1=1").Delete(&Product{})
  51. for i := 1; i < 300; i++ {
  52. db.Create(&Product{Name: fmt.Sprintf("Product %d", i)})
  53. }
  54. ExamplePreset = presets.New()
  55. ExamplePreset.URIPrefix(VuetifyAutoCompletePresetPath).DataOperator(gorm2op.DataOperator(db))
  56. listing := ExamplePreset.Model(&Product{}).Listing()
  57. loadMoreRes = listing.ConfigureAutocompleteDataSource(
  58. &presets.AutocompleteDataSourceConfig{
  59. OptionValue: "ID",
  60. OptionText: "Name",
  61. OptionIcon: func(product interface{}) string {
  62. return fmt.Sprintf("https://cdn.vuetifyjs.com/images/lists/%d.jpg", product.(*Product).ID%4+1)
  63. },
  64. KeywordColumns: []string{
  65. "Name",
  66. },
  67. PerPage: 50,
  68. },
  69. "loadMore",
  70. )
  71. pagingRes = listing.ConfigureAutocompleteDataSource(
  72. &presets.AutocompleteDataSourceConfig{
  73. OptionValue: "ID",
  74. OptionText: "Name",
  75. OptionIcon: func(product interface{}) string {
  76. return fmt.Sprintf("https://cdn.vuetifyjs.com/images/lists/%d.jpg", product.(*Product).ID%4+1)
  77. },
  78. KeywordColumns: []string{
  79. "Name",
  80. },
  81. PerPage: 20,
  82. IsPaging: true,
  83. OrderBy: "Name",
  84. },
  85. "paging",
  86. )
  87. }
  88. func VuetifyAutocomplete(ctx *web.EventContext) (pr web.PageResponse, err error) {
  89. pr.Body = VContainer(
  90. h.H1("Select many (default)"),
  91. vuetifyx.VXAutocomplete().
  92. Label("Load options from a list").
  93. Items(options).
  94. FieldName("Values1").
  95. ItemText("Name").
  96. ItemValue("Login"),
  97. h.H1("Select one"),
  98. vuetifyx.VXAutocomplete().
  99. FieldName("Values2").
  100. Label("Load options from a list").
  101. Items(options).
  102. ItemText("Name").
  103. ItemValue("Login").
  104. Multiple(false),
  105. h.H1("Has icon"),
  106. vuetifyx.VXAutocomplete().
  107. Label("Load options from a list").
  108. Items(iconOptions).
  109. HasIcon(true),
  110. h.H1("Load more from remote resource"),
  111. vuetifyx.VXAutocomplete().
  112. FieldName("Values2").
  113. Label("Load options from data source").
  114. SetDataSource(loadMoreRes),
  115. h.H1("Paging with remote resource"),
  116. vuetifyx.VXAutocomplete().
  117. FieldName("Values2").
  118. Label("Load options from data source").
  119. SetDataSource(pagingRes),
  120. h.H1("Sorting"),
  121. vuetifyx.VXAutocomplete().
  122. FieldName("Values2").
  123. Label("Load options from data source").
  124. Sorting(true).
  125. SetDataSource(pagingRes).ChipColor("red"),
  126. )
  127. return
  128. }
  129. var VuetifyAutocompletePB = web.Page(VuetifyAutocomplete)
  130. const VuetifyAutoCompletePath = "/samples/vuetify-auto-complete"
  131. const VuetifyAutoCompletePresetPath = "/samples/vuetify-auto-complete-preset"
  132. // @snippet_end