自定义页面


框架支持自己定义一个页面显示,调用引擎的Content方法,如下:

  1. package main
  2. import (
  3. _ "github.com/GoAdminGroup/go-admin/adapter/gin"
  4. _ "github.com/GoAdminGroup/go-admin/modules/db/drivers/mysql"
  5. _ "github.com/GoAdminGroup/themes/adminlte"
  6. "github.com/GoAdminGroup/go-admin/engine"
  7. "github.com/GoAdminGroup/go-admin/examples/datamodel"
  8. "github.com/GoAdminGroup/go-admin/modules/config"
  9. "github.com/GoAdminGroup/go-admin/plugins/admin"
  10. "github.com/GoAdminGroup/go-admin/plugins/example"
  11. "github.com/GoAdminGroup/go-admin/template/types"
  12. "github.com/gin-gonic/gin"
  13. )
  14. func main() {
  15. r := gin.Default()
  16. eng := engine.Default()
  17. cfg := config.Config{}
  18. adminPlugin := admin.NewAdmin(datamodel.Generators)
  19. examplePlugin := example.NewExample()
  20. if err := eng.AddConfig(cfg).AddPlugins(adminPlugin, examplePlugin).Use(r); err != nil {
  21. panic(err)
  22. }
  23. r.Static("/uploads", "./uploads")
  24. // 这样子去自定义一个页面:
  25. r.GET("/"+cfg.PREFIX+"/custom", func(ctx *gin.Context) {
  26. engine.Content(ctx, func() (types.Panel, error) {
  27. return datamodel.GetContent()
  28. })
  29. })
  30. r.Run(":9033")
  31. }

Content方法会将内容写入到框架的context中。

GetContent方法代码如下:

  1. package datamodel
  2. import (
  3. "github.com/GoAdminGroup/go-admin/modules/config"
  4. template2 "github.com/GoAdminGroup/go-admin/template"
  5. "github.com/GoAdminGroup/go-admin/template/types"
  6. "html/template"
  7. )
  8. func GetContent() (types.Panel, error) {
  9. components := template2.Get(config.Get().THEME)
  10. colComp := components.Col()
  11. infobox := components.InfoBox().
  12. SetText("CPU TRAFFIC").
  13. SetColor("blue").
  14. SetNumber("41,410").
  15. SetIcon("ion-ios-gear-outline").
  16. GetContent()
  17. var size = map[string]string{"md": "3", "sm": "6", "xs": "12"}
  18. infoboxCol1 := colComp.SetSize(size).SetContent(infobox).GetContent()
  19. row1 := components.Row().SetContent(infoboxCol1).GetContent()
  20. return types.Panel{
  21. Content: row1,
  22. Title: "Dashboard",
  23. Description: "this is a example",
  24. }, nil
  25. }