| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package tables
- import (
- "fmt"
- "github.com/GoAdminGroup/go-admin/context"
- "github.com/GoAdminGroup/go-admin/modules/db"
- form2 "github.com/GoAdminGroup/go-admin/plugins/admin/modules/form"
- "github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
- "github.com/GoAdminGroup/go-admin/template"
- "github.com/GoAdminGroup/go-admin/template/icon"
- "github.com/GoAdminGroup/go-admin/template/types"
- "github.com/GoAdminGroup/go-admin/template/types/action"
- "github.com/GoAdminGroup/go-admin/template/types/form"
- editType "github.com/GoAdminGroup/go-admin/template/types/table"
- )
- // GetUserTable return the model of table user.
- func GetUserTable() (userTable table.Table) {
- userTable = table.NewDefaultTable(table.Config{
- Driver: db.DriverMysql,
- CanAdd: true,
- Editable: true,
- Deletable: true,
- Exportable: true,
- Connection: table.DefaultConnectionName,
- PrimaryKey: table.PrimaryKey{
- Type: db.Int,
- Name: table.DefaultPrimaryKeyName,
- },
- })
- info := userTable.GetInfo().SetFilterFormLayout(form.LayoutTwoCol)
- info.AddField("ID", "id", db.Int).FieldSortable()
- info.AddField("Name", "name", db.Varchar).FieldEditAble(editType.Text).
- FieldFilterable(types.FilterType{Operator: types.FilterOperatorLike})
- info.AddField("Gender", "gender", db.Tinyint).FieldDisplay(func(model types.FieldModel) interface{} {
- if model.Value == "0" {
- return "men"
- }
- if model.Value == "1" {
- return "women"
- }
- return "unknown"
- }).FieldEditAble(editType.Select).FieldEditOptions([]map[string]string{
- {"value": "0", "text": "men"},
- {"value": "1", "text": "women"},
- }).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).FieldFilterOptions([]map[string]string{
- {"value": "0", "field": "men"},
- {"value": "1", "field": "women"},
- })
- info.AddField("Phone", "phone", db.Varchar).FieldFilterable()
- info.AddField("City", "city", db.Varchar).FieldFilterable()
- info.AddField("Avatar", "avatar", db.Varchar).FieldDisplay(func(value types.FieldModel) interface{} {
- return template.Default().Image().
- SetSrc(`//quick.go-admin.cn/demo/assets/dist/img/gopher_avatar.png`).
- SetHeight("120").SetWidth("120").WithModal().GetContent()
- })
- info.AddField("CreatedAt", "created_at", db.Timestamp).
- FieldFilterable(types.FilterType{FormType: form.DatetimeRange})
- info.AddField("UpdatedAt", "updated_at", db.Timestamp).FieldEditAble(editType.Datetime)
- info.AddActionButton("google", action.Jump("https://google.com"))
- info.AddButton("google", icon.Google, action.Jump("https://google.com"))
- info.AddButton("info", icon.Terminal, action.PopUp("/admin/popup", "Popup Example",
- func(ctx *context.Context) (success bool, data, msg string) {
- return true, "<h2>hello world</h2>", ""
- }))
- info.AddButton("ajax", icon.Android, action.Ajax("/admin/ajax",
- func(ctx *context.Context) (success bool, data, msg string) {
- return true, "", "success"
- }))
- info.SetTable("users").SetTitle("Users").SetDescription("Users")
- formList := userTable.GetForm()
- formList.AddField("ID", "id", db.Int, form.Default).FieldNotAllowEdit().FieldNotAllowAdd()
- formList.AddField("Ip", "ip", db.Varchar, form.Text)
- formList.AddField("Name", "name", db.Varchar, form.Text)
- formList.AddField("Gender", "gender", db.Tinyint, form.Radio).
- FieldOptions([]map[string]string{
- {
- "field": "gender",
- "label": "men",
- "value": "0",
- "selected": "checked",
- }, {
- "field": "gender",
- "label": "women",
- "value": "1",
- "selected": "",
- },
- })
- formList.AddField("Phone", "phone", db.Varchar, form.Text)
- formList.AddField("City", "city", db.Varchar, form.Text)
- formList.AddField("Custom Field", "role", db.Varchar, form.Text).
- FieldPostFilterFn(func(value types.PostFieldModel) interface{} {
- fmt.Println("user custom field", value)
- return ""
- })
- formList.AddField("UpdatedAt", "updated_at", db.Timestamp, form.Default).FieldNotAllowAdd()
- formList.AddField("CreatedAt", "created_at", db.Timestamp, form.Default).FieldNotAllowAdd()
- userTable.GetForm().SetTabGroups(types.
- NewTabGroups("id", "ip", "name", "gender", "city").
- AddGroup("phone", "role", "created_at", "updated_at")).
- SetTabHeaders("profile1", "profile2")
- formList.SetTable("users").SetTitle("Users").SetDescription("Users")
- formList.SetPostHook(func(values form2.Values) error {
- fmt.Println("userTable.GetForm().PostHook", values)
- return nil
- })
- return
- }
|