listing.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package example_basics
  2. import (
  3. "time"
  4. "github.com/qor5/admin/presets"
  5. "github.com/qor5/admin/presets/gorm2op"
  6. "github.com/qor5/ui/vuetify"
  7. "github.com/qor5/web"
  8. h "github.com/theplant/htmlgo"
  9. "gorm.io/driver/sqlite"
  10. "gorm.io/gorm"
  11. "gorm.io/gorm/logger"
  12. )
  13. var DB *gorm.DB
  14. func init() {
  15. DB = setupDB()
  16. }
  17. func setupDB() (db *gorm.DB) {
  18. var err error
  19. db, err = gorm.Open(sqlite.Open("/tmp/my.db"), &gorm.Config{})
  20. if err != nil {
  21. panic(err)
  22. }
  23. db.Logger.LogMode(logger.Info)
  24. err = db.AutoMigrate(
  25. &Post{},
  26. &Category{},
  27. )
  28. if err != nil {
  29. panic(err)
  30. }
  31. return
  32. }
  33. const ListingSamplePath = "/samples/listing"
  34. // @snippet_begin(PresetsListingSample)
  35. type Post struct {
  36. ID uint
  37. Title string
  38. Body string
  39. UpdatedAt time.Time
  40. CreatedAt time.Time
  41. Disabled bool
  42. Status string
  43. CategoryID uint
  44. }
  45. type Category struct {
  46. ID uint
  47. Name string
  48. UpdatedAt time.Time
  49. CreatedAt time.Time
  50. }
  51. func ListingSample(b *presets.Builder) {
  52. db := DB
  53. // Setup the project name, ORM and Homepage
  54. b.URIPrefix(ListingSamplePath).DataOperator(gorm2op.DataOperator(db))
  55. // Register Post into the builder
  56. // Use m to customize the model, Or config more models here.
  57. postModelBuilder := b.Model(&Post{})
  58. postModelBuilder.Listing("ID", "Title", "Body", "CategoryID", "VirtualField")
  59. postModelBuilder.Listing().Searcher = func(model interface{}, params *presets.SearchParams, ctx *web.EventContext) (r interface{}, totalCount int, err error) {
  60. qdb := db.Where("disabled != true")
  61. return gorm2op.DataOperator(qdb).Search(model, params, ctx)
  62. }
  63. rmn := postModelBuilder.Listing().RowMenu()
  64. rmn.RowMenuItem("Show").ComponentFunc(func(obj interface{}, id string, ctx *web.EventContext) h.HTMLComponent {
  65. return h.Text("Fake Show")
  66. })
  67. postModelBuilder.Listing().ActionsAsMenu(true)
  68. postModelBuilder.Editing().Field("CategoryID").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  69. categories := []Category{}
  70. if err := db.Find(&categories).Error; err != nil {
  71. // ignore err for now
  72. }
  73. return vuetify.VAutocomplete().Chips(true).FieldName(field.Name).Label(field.Label).Value(field.Value(obj)).Items(categories).ItemText("Name").ItemValue("ID")
  74. })
  75. postModelBuilder.Listing().Field("CategoryID").Label("Category").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  76. c := Category{}
  77. cid, _ := field.Value(obj).(uint)
  78. if err := db.Where("id = ?", cid).Find(&c).Error; err != nil {
  79. // ignore err in the example
  80. }
  81. return h.Td(h.Text(c.Name))
  82. })
  83. postModelBuilder.Listing().Field("VirtualField").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  84. return h.Td(h.Text("virtual field"))
  85. })
  86. b.Model(&Category{})
  87. // Use m to customize the model, Or config more models here.
  88. return
  89. }
  90. // @snippet_end