users.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. selection "github.com/GoAdminGroup/go-admin/template/types/form/select"
  14. editType "github.com/GoAdminGroup/go-admin/template/types/table"
  15. )
  16. // GetUserTable return the model of table user.
  17. func GetUserTable(ctx *context.Context) (userTable table.Table) {
  18. userTable = table.NewDefaultTable(table.Config{
  19. Driver: db.DriverSqlite,
  20. CanAdd: true,
  21. Editable: true,
  22. Deletable: true,
  23. Exportable: true,
  24. Connection: table.DefaultConnectionName,
  25. PrimaryKey: table.PrimaryKey{
  26. Type: db.Int,
  27. Name: table.DefaultPrimaryKeyName,
  28. },
  29. })
  30. info := userTable.GetInfo().SetFilterFormLayout(form.LayoutTwoCol)
  31. info.AddField("ID", "id", db.Int).FieldSortable()
  32. info.AddField("Name", "name", db.Varchar).FieldEditAble(editType.Text).
  33. FieldFilterable(types.FilterType{Operator: types.FilterOperatorLike})
  34. info.AddField("Gender", "gender", db.Tinyint).FieldDisplay(func(model types.FieldModel) interface{} {
  35. if model.Value == "0" {
  36. return "men"
  37. }
  38. if model.Value == "1" {
  39. return "women"
  40. }
  41. return "unknown"
  42. }).FieldEditAble(editType.Select).FieldEditOptions(types.FieldOptions{
  43. {Value: "0", Text: "men"},
  44. {Value: "1", Text: "women"},
  45. }).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).FieldFilterOptions(types.FieldOptions{
  46. {Value: "0", Text: "men"},
  47. {Value: "1", Text: "women"},
  48. })
  49. info.AddField("Phone", "phone", db.Varchar).FieldFilterable()
  50. info.AddField("City", "city", db.Varchar).FieldFilterable()
  51. info.AddField("Avatar", "avatar", db.Varchar).FieldDisplay(func(value types.FieldModel) interface{} {
  52. return template.Default().Image().
  53. SetSrc(`//quick.go-admin.cn/demo/assets/dist/img/gopher_avatar.png`).
  54. SetHeight("120").SetWidth("120").WithModal().GetContent()
  55. })
  56. info.AddField("CreatedAt", "created_at", db.Timestamp).
  57. FieldFilterable(types.FilterType{FormType: form.DatetimeRange})
  58. info.AddField("UpdatedAt", "updated_at", db.Timestamp).FieldEditAble(editType.Datetime)
  59. info.AddActionButton("google", action.Jump("https://google.com"))
  60. info.AddActionButton("audit", action.Ajax("/admin/audit",
  61. func(ctx *context.Context) (success bool, msg string, data interface{}) {
  62. return true, "success", ""
  63. }))
  64. info.AddButton("google", icon.Google, action.Jump("https://google.com"))
  65. info.AddButton("info", icon.Terminal, action.PopUp("/admin/popup", "Popup Example",
  66. func(ctx *context.Context) (success bool, msg string, data interface{}) {
  67. return true, "", "<h2>hello world</h2>"
  68. }))
  69. info.AddButton("ajax", icon.Android, action.Ajax("/admin/ajax",
  70. func(ctx *context.Context) (success bool, msg string, data interface{}) {
  71. return true, "success", ""
  72. }))
  73. info.SetTable("users").SetTitle("Users").SetDescription("Users")
  74. formList := userTable.GetForm()
  75. formList.AddField("ID", "id", db.Int, form.Default).FieldNotAllowEdit().FieldNotAllowAdd()
  76. formList.AddField("Ip", "ip", db.Varchar, form.Text)
  77. formList.AddField("Name", "name", db.Varchar, form.Text)
  78. formList.AddField("Gender", "gender", db.Tinyint, form.Radio).
  79. FieldOptions(types.FieldOptions{
  80. {Text: "men", Value: "0"},
  81. {Text: "women", Value: "1"},
  82. }).FieldDefault("0")
  83. formList.AddField("Phone", "phone", db.Varchar, form.Text)
  84. formList.AddField("Country", "country", db.Tinyint, form.SelectSingle).
  85. FieldOptions(types.FieldOptions{
  86. {Text: "China", Value: "0"},
  87. {Text: "America", Value: "1"},
  88. {Text: "England", Value: "2"},
  89. {Text: "Canada", Value: "3"},
  90. }).FieldDefault("0").FieldOnChooseAjax("city", "/choose/country",
  91. func(ctx *context.Context) (bool, string, interface{}) {
  92. country := ctx.FormValue("value")
  93. var data = make(selection.Options, 0)
  94. switch country {
  95. case "0":
  96. data = selection.Options{
  97. {Text: "Beijing", ID: "beijing"},
  98. {Text: "ShangHai", ID: "shangHai"},
  99. {Text: "GuangZhou", ID: "guangZhou"},
  100. {Text: "ShenZhen", ID: "shenZhen"},
  101. }
  102. case "1":
  103. data = selection.Options{
  104. {Text: "Los Angeles", ID: "los angeles"},
  105. {Text: "Washington, dc", ID: "washington, dc"},
  106. {Text: "New York", ID: "new york"},
  107. {Text: "Las Vegas", ID: "las vegas"},
  108. }
  109. case "2":
  110. data = selection.Options{
  111. {Text: "London", ID: "london"},
  112. {Text: "Cambridge", ID: "cambridge"},
  113. {Text: "Manchester", ID: "manchester"},
  114. {Text: "Liverpool", ID: "liverpool"},
  115. }
  116. case "3":
  117. data = selection.Options{
  118. {Text: "Vancouver", ID: "vancouver"},
  119. {Text: "Toronto", ID: "toronto"},
  120. }
  121. default:
  122. data = selection.Options{
  123. {Text: "Beijing", ID: "beijing"},
  124. {Text: "ShangHai", ID: "shangHai"},
  125. {Text: "GuangZhou", ID: "guangZhou"},
  126. {Text: "ShenZhen", ID: "shenZhen"},
  127. }
  128. }
  129. return true, "ok", data
  130. })
  131. formList.AddField("City", "city", db.Varchar, form.SelectSingle).
  132. FieldOptionInitFn(func(val types.FieldModel) types.FieldOptions {
  133. return types.FieldOptions{
  134. {Value: val.Value, Text: val.Value, Selected: true},
  135. }
  136. })
  137. formList.AddField("Custom Field", "role", db.Varchar, form.Text).
  138. FieldPostFilterFn(func(value types.PostFieldModel) interface{} {
  139. fmt.Println("user custom field", value)
  140. return ""
  141. })
  142. formList.AddField("UpdatedAt", "updated_at", db.Timestamp, form.Default).FieldNotAllowAdd()
  143. formList.AddField("CreatedAt", "created_at", db.Timestamp, form.Default).FieldNotAllowAdd()
  144. userTable.GetForm().SetTabGroups(types.
  145. NewTabGroups("id", "ip", "name", "gender", "country", "city").
  146. AddGroup("phone", "role", "created_at", "updated_at")).
  147. SetTabHeaders("profile1", "profile2")
  148. formList.SetTable("users").SetTitle("Users").SetDescription("Users")
  149. formList.SetPostHook(func(values form2.Values) error {
  150. fmt.Println("userTable.GetForm().PostHook", values)
  151. return nil
  152. })
  153. return
  154. }