listing.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package basics
  2. import (
  3. "github.com/qor5/docs/docsrc/examples/example_basics"
  4. "github.com/qor5/docs/docsrc/generated"
  5. "github.com/qor5/docs/docsrc/utils"
  6. . "github.com/theplant/docgo"
  7. "github.com/theplant/docgo/ch"
  8. )
  9. var Listing = Doc(
  10. Markdown(`
  11. By the [1 Minute Quick Start](/getting-started/one-minute-quick-start.html), We get a default listing page with default columns, But default columns from database columns rarely fit the needs for any real application. Here we will introduce common customizations on the list page.
  12. - Configure fields that displayed on the page
  13. - Modify the display value
  14. - Display a virtual field
  15. - Default scope
  16. - Extend the dot menu
  17. There would be a runable example at the last.
  18. ## Configure fields that displayed on the page
  19. Suppose we added a new model called ~Category~, the ~Post~ belongs to ~Category~. Then we want to display ~CategoryID~ on the list page.
  20. `),
  21. ch.Code(`
  22. type Post struct {
  23. ID uint
  24. Title string
  25. Body string
  26. CategoryID uint
  27. UpdatedAt time.Time
  28. CreatedAt time.Time
  29. }
  30. type Category struct {
  31. ID uint
  32. Name string
  33. UpdatedAt time.Time
  34. CreatedAt time.Time
  35. }
  36. postModelBuilder.Listing("ID", "Title", "Body", "CategoryID")
  37. `),
  38. Markdown(`
  39. ## Modify the display value
  40. To display the category name rather than category id in the post listing page. The ~ComponentFunc~ would do the work.
  41. The ~obj~ is the ~Post~ record, and ~field~ is the ~CategoryID~ field of this ~Post~ record. You can get the value by ~field.Value(obj)~ function.
  42. `),
  43. ch.Code(`postModelBuilder.Listing().Field("CategoryID").Label("Category").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  44. c := models.Category{}
  45. cid, _ := field.Value(obj).(uint)
  46. if err := db.Where("id = ?", cid).Find(&c).Error; err != nil {
  47. // ignore err in the example
  48. }
  49. return h.Td(h.Text(c.Name))
  50. })
  51. `).Language("go"),
  52. Markdown(`
  53. ## Display virtual fields
  54. `),
  55. ch.Code(`postModelBuilder.Listing("ID", "Title", "Body", "CategoryID", "VirtualValue")
  56. postModelBuilder.Listing().Field("VirtualField").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  57. return h.Td(h.Text("virtual field"))
  58. })
  59. `),
  60. Markdown(`
  61. ## DefaultScope
  62. If we want to display ~Post~ with ~disabled=false~ only. Use the ~Listing().Searcher~ to apply SQL conditions.
  63. `),
  64. ch.Code(`postModelBuilder.Listing().Searcher = func(model interface{}, params *presets.SearchParams, ctx *web.EventContext) (r interface{}, totalCount int, err error){
  65. qdb := db.Where("disabled != true")
  66. return gorm2op.DataOperator(qdb).Search(model, params, ctx)
  67. }
  68. `),
  69. Markdown(`
  70. ## Extend the dot menu
  71. You can extend the dot menu by calling the ~RowMenuItem~ function. If you want to overwrite the default ~Edit~ and ~Delete~ link, you can pass the items you wanted to ~Listing().RowMenu()~
  72. `),
  73. ch.Code(`rmn := postModelBuilder.Listing().RowMenu()
  74. rmn.RowMenuItem("Show").ComponentFunc(func(obj interface{}, id string, ctx *web.EventContext) h.HTMLComponent {
  75. return h.Text("Fake Show")
  76. })
  77. `),
  78. Markdown(`
  79. ## Full Example
  80. `),
  81. ch.Code(generated.PresetsListingSample).Language("go"),
  82. utils.Demo("Presets Listing Customization Fields", example_basics.ListingSamplePath+"/posts", "example_basics/listing.go"),
  83. ).Title("Listing").
  84. Slug("basics/listing")