users.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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(ctx, 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.LayoutFilter)
  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.Switch).FieldEditOptions(types.FieldOptions{
  43. {Value: "0", Text: "👨"},
  44. {Value: "1", Text: "👩"},
  45. }).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).FieldFilterOptions(types.FieldOptions{
  46. {Value: "0", Text: "men"},
  47. {Value: "1", Text: "women"},
  48. })
  49. info.AddColumn("personality", func(value types.FieldModel) interface{} {
  50. return "handsome"
  51. })
  52. info.AddColumnButtons(ctx, "see more", types.GetColumnButton("more", icon.Info,
  53. action.PopUp("/see/more/example", "Detail", func(ctx *context.Context) (success bool, msg string, data interface{}) {
  54. return true, "ok", "<h1>Detail</h1><p>balabala</p><p>this feature will be released in v1.2.7</p>"
  55. })))
  56. info.AddField("Phone", "phone", db.Varchar).FieldFilterable()
  57. info.AddField("City", "city", db.Varchar).FieldFilterable()
  58. info.AddField("Avatar", "avatar", db.Varchar).FieldDisplay(func(value types.FieldModel) interface{} {
  59. return template.Default().Image().
  60. SetSrc(`//quick.go-admin.cn/demo/assets/dist/img/gopher_avatar.png`).
  61. SetHeight("120").SetWidth("120").WithModal().GetContent()
  62. })
  63. info.AddField("CreatedAt", "created_at", db.Timestamp).
  64. FieldFilterable(types.FilterType{FormType: form.DatetimeRange})
  65. info.AddField("UpdatedAt", "updated_at", db.Timestamp).FieldEditAble(editType.Datetime)
  66. info.AddActionButton(ctx, "google", action.Jump("https://google.com"))
  67. info.AddActionButton(ctx, "audit", action.Ajax("/admin/audit",
  68. func(ctx *context.Context) (success bool, msg string, data interface{}) {
  69. return true, "success", ""
  70. }))
  71. info.AddActionButton(ctx, "Preview", action.PopUp("/admin/preview", "Preview",
  72. func(ctx *context.Context) (success bool, msg string, data interface{}) {
  73. return true, "", "<h2>hello world</h2>"
  74. }))
  75. info.AddButton(ctx, "google", icon.Google, action.Jump("https://google.com"))
  76. info.AddButton(ctx, "popup", icon.Terminal, action.PopUp("/admin/popup", "Popup Example",
  77. func(ctx *context.Context) (success bool, msg string, data interface{}) {
  78. return true, "", "<h2>hello world</h2>"
  79. }))
  80. info.AddButton(ctx, "iframe", icon.Tv, action.PopUpWithIframe("/admin/iframe", "Iframe Example",
  81. action.IframeData{Src: "/admin/info/profile/new"}, "900px", "480px"))
  82. info.AddButton(ctx, "ajax", icon.Android, action.Ajax("/admin/ajax",
  83. func(ctx *context.Context) (success bool, msg string, data interface{}) {
  84. return true, "success", ""
  85. }))
  86. info.AddSelectBox(ctx, "gender", types.FieldOptions{
  87. {Value: "0", Text: "men"},
  88. {Value: "1", Text: "women"},
  89. }, action.FieldFilter("gender"))
  90. info.SetTable("users").SetTitle("Users").SetDescription("Users")
  91. formList := userTable.GetForm()
  92. formList.AddField("ID", "id", db.Int, form.Default).FieldNotAllowEdit().FieldNotAllowAdd()
  93. formList.AddField("Ip", "ip", db.Varchar, form.Text)
  94. formList.AddField("Name", "name", db.Varchar, form.Text)
  95. formList.AddField("Gender", "gender", db.Tinyint, form.Radio).
  96. FieldOptions(types.FieldOptions{
  97. {Text: "men", Value: "0"},
  98. {Text: "women", Value: "1"},
  99. }).FieldDefault("0")
  100. formList.AddField("Phone", "phone", db.Varchar, form.Text)
  101. formList.AddField("Country", "country", db.Tinyint, form.SelectSingle).
  102. FieldOptions(types.FieldOptions{
  103. {Text: "China", Value: "0"},
  104. {Text: "America", Value: "1"},
  105. {Text: "England", Value: "2"},
  106. {Text: "Canada", Value: "3"},
  107. }).FieldDefault("0").FieldOnChooseAjax("city", "/choose/country",
  108. func(ctx *context.Context) (bool, string, interface{}) {
  109. country := ctx.FormValue("value")
  110. var data = make(selection.Options, 0)
  111. switch country {
  112. case "0":
  113. data = selection.Options{
  114. {Text: "Beijing", ID: "beijing"},
  115. {Text: "ShangHai", ID: "shangHai"},
  116. {Text: "GuangZhou", ID: "guangZhou"},
  117. {Text: "ShenZhen", ID: "shenZhen"},
  118. }
  119. case "1":
  120. data = selection.Options{
  121. {Text: "Los Angeles", ID: "los angeles"},
  122. {Text: "Washington, dc", ID: "washington, dc"},
  123. {Text: "New York", ID: "new york"},
  124. {Text: "Las Vegas", ID: "las vegas"},
  125. }
  126. case "2":
  127. data = selection.Options{
  128. {Text: "London", ID: "london"},
  129. {Text: "Cambridge", ID: "cambridge"},
  130. {Text: "Manchester", ID: "manchester"},
  131. {Text: "Liverpool", ID: "liverpool"},
  132. }
  133. case "3":
  134. data = selection.Options{
  135. {Text: "Vancouver", ID: "vancouver"},
  136. {Text: "Toronto", ID: "toronto"},
  137. }
  138. default:
  139. data = selection.Options{
  140. {Text: "Beijing", ID: "beijing"},
  141. {Text: "ShangHai", ID: "shangHai"},
  142. {Text: "GuangZhou", ID: "guangZhou"},
  143. {Text: "ShenZhen", ID: "shenZhen"},
  144. }
  145. }
  146. return true, "ok", data
  147. })
  148. formList.AddField("City", "city", db.Varchar, form.SelectSingle).
  149. FieldOptionInitFn(func(val types.FieldModel) types.FieldOptions {
  150. return types.FieldOptions{
  151. {Value: val.Value, Text: val.Value, Selected: true},
  152. }
  153. })
  154. formList.AddField("Custom Field", "role", db.Varchar, form.Text).
  155. FieldPostFilterFn(func(value types.PostFieldModel) interface{} {
  156. fmt.Println("user custom field", value)
  157. return ""
  158. })
  159. formList.AddField("UpdatedAt", "updated_at", db.Timestamp, form.Default).FieldNotAllowAdd()
  160. formList.AddField("CreatedAt", "created_at", db.Timestamp, form.Default).FieldNotAllowAdd()
  161. userTable.GetForm().SetTabGroups(types.
  162. NewTabGroups("id", "ip", "name", "gender", "country", "city").
  163. AddGroup("phone", "role", "created_at", "updated_at")).
  164. SetTabHeaders("profile1", "profile2")
  165. formList.SetTable("users").SetTitle("Users").SetDescription("Users")
  166. formList.SetPostHook(func(values form2.Values) error {
  167. fmt.Println("userTable.GetForm().PostHook", values)
  168. return nil
  169. })
  170. return
  171. }