users.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package tables
  2. import (
  3. "fmt"
  4. "github.com/GoAdminGroup/go-admin/context"
  5. "github.com/GoAdminGroup/go-admin/modules/db"
  6. form2 "github.com/GoAdminGroup/go-admin/plugins/admin/modules/form"
  7. "github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
  8. "github.com/GoAdminGroup/go-admin/template"
  9. "github.com/GoAdminGroup/go-admin/template/icon"
  10. "github.com/GoAdminGroup/go-admin/template/types"
  11. "github.com/GoAdminGroup/go-admin/template/types/action"
  12. "github.com/GoAdminGroup/go-admin/template/types/form"
  13. editType "github.com/GoAdminGroup/go-admin/template/types/table"
  14. )
  15. // GetUserTable return the model of table user.
  16. func GetUserTable(ctx *context.Context) (userTable table.Table) {
  17. userTable = table.NewDefaultTable(table.Config{
  18. Driver: db.DriverMysql,
  19. CanAdd: true,
  20. Editable: true,
  21. Deletable: true,
  22. Exportable: true,
  23. Connection: table.DefaultConnectionName,
  24. PrimaryKey: table.PrimaryKey{
  25. Type: db.Int,
  26. Name: table.DefaultPrimaryKeyName,
  27. },
  28. })
  29. info := userTable.GetInfo().SetFilterFormLayout(form.LayoutTwoCol)
  30. info.AddField("ID", "id", db.Int).FieldSortable()
  31. info.AddField("Name", "name", db.Varchar).FieldEditAble(editType.Text).
  32. FieldFilterable(types.FilterType{Operator: types.FilterOperatorLike})
  33. info.AddField("Gender", "gender", db.Tinyint).FieldDisplay(func(model types.FieldModel) interface{} {
  34. if model.Value == "0" {
  35. return "men"
  36. }
  37. if model.Value == "1" {
  38. return "women"
  39. }
  40. return "unknown"
  41. }).FieldEditAble(editType.Select).FieldEditOptions([]map[string]string{
  42. {"value": "0", "text": "men"},
  43. {"value": "1", "text": "women"},
  44. }).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).FieldFilterOptions([]map[string]string{
  45. {"value": "0", "field": "men"},
  46. {"value": "1", "field": "women"},
  47. })
  48. info.AddField("Phone", "phone", db.Varchar).FieldFilterable()
  49. info.AddField("City", "city", db.Varchar).FieldFilterable()
  50. info.AddField("Avatar", "avatar", db.Varchar).FieldDisplay(func(value types.FieldModel) interface{} {
  51. return template.Default().Image().
  52. SetSrc(`//quick.go-admin.cn/demo/assets/dist/img/gopher_avatar.png`).
  53. SetHeight("120").SetWidth("120").WithModal().GetContent()
  54. })
  55. info.AddField("CreatedAt", "created_at", db.Timestamp).
  56. FieldFilterable(types.FilterType{FormType: form.DatetimeRange})
  57. info.AddField("UpdatedAt", "updated_at", db.Timestamp).FieldEditAble(editType.Datetime)
  58. info.AddActionButton("google", action.Jump("https://google.com"))
  59. info.AddActionButton("audit", action.Ajax("/admin/audit",
  60. func(ctx *context.Context) (success bool, data, msg string) {
  61. return true, "", "success"
  62. }))
  63. info.AddButton("google", icon.Google, action.Jump("https://google.com"))
  64. info.AddButton("info", icon.Terminal, action.PopUp("/admin/popup", "Popup Example",
  65. func(ctx *context.Context) (success bool, data, msg string) {
  66. return true, "<h2>hello world</h2>", ""
  67. }))
  68. info.AddButton("ajax", icon.Android, action.Ajax("/admin/ajax",
  69. func(ctx *context.Context) (success bool, data, msg string) {
  70. return true, "", "success"
  71. }))
  72. info.SetTable("users").SetTitle("Users").SetDescription("Users")
  73. formList := userTable.GetForm()
  74. formList.AddField("ID", "id", db.Int, form.Default).FieldNotAllowEdit().FieldNotAllowAdd()
  75. formList.AddField("Ip", "ip", db.Varchar, form.Text)
  76. formList.AddField("Name", "name", db.Varchar, form.Text)
  77. formList.AddField("Gender", "gender", db.Tinyint, form.Radio).
  78. FieldOptions([]map[string]string{
  79. {
  80. "field": "gender",
  81. "label": "men",
  82. "value": "0",
  83. "selected": "checked",
  84. }, {
  85. "field": "gender",
  86. "label": "women",
  87. "value": "1",
  88. "selected": "",
  89. },
  90. })
  91. formList.AddField("Phone", "phone", db.Varchar, form.Text)
  92. formList.AddField("City", "city", db.Varchar, form.Text)
  93. formList.AddField("Custom Field", "role", db.Varchar, form.Text).
  94. FieldPostFilterFn(func(value types.PostFieldModel) interface{} {
  95. fmt.Println("user custom field", value)
  96. return ""
  97. })
  98. formList.AddField("UpdatedAt", "updated_at", db.Timestamp, form.Default).FieldNotAllowAdd()
  99. formList.AddField("CreatedAt", "created_at", db.Timestamp, form.Default).FieldNotAllowAdd()
  100. userTable.GetForm().SetTabGroups(types.
  101. NewTabGroups("id", "ip", "name", "gender", "city").
  102. AddGroup("phone", "role", "created_at", "updated_at")).
  103. SetTabHeaders("profile1", "profile2")
  104. formList.SetTable("users").SetTitle("Users").SetDescription("Users")
  105. formList.SetPostHook(func(values form2.Values) error {
  106. fmt.Println("userTable.GetForm().PostHook", values)
  107. return nil
  108. })
  109. return
  110. }