github.com/kotovmak/go-admin@v1.1.1/examples/datamodel/user.go (about) 1 package datamodel 2 3 import ( 4 "fmt" 5 6 "github.com/kotovmak/go-admin/context" 7 "github.com/kotovmak/go-admin/modules/db" 8 form2 "github.com/kotovmak/go-admin/plugins/admin/modules/form" 9 "github.com/kotovmak/go-admin/plugins/admin/modules/table" 10 "github.com/kotovmak/go-admin/template" 11 "github.com/kotovmak/go-admin/template/icon" 12 "github.com/kotovmak/go-admin/template/types" 13 "github.com/kotovmak/go-admin/template/types/action" 14 "github.com/kotovmak/go-admin/template/types/form" 15 selection "github.com/kotovmak/go-admin/template/types/form/select" 16 editType "github.com/kotovmak/go-admin/template/types/table" 17 ) 18 19 // GetUserTable return the model of table user. 20 func GetUserTable(ctx *context.Context) (userTable table.Table) { 21 22 userTable = table.NewDefaultTable(table.Config{ 23 Driver: db.DriverMysql, 24 CanAdd: true, 25 Editable: true, 26 Deletable: true, 27 Exportable: true, 28 Connection: table.DefaultConnectionName, 29 PrimaryKey: table.PrimaryKey{ 30 Type: db.Int, 31 Name: table.DefaultPrimaryKeyName, 32 }, 33 }) 34 35 info := userTable.GetInfo().SetFilterFormLayout(form.LayoutThreeCol) 36 info.AddField("ID", "id", db.Int).FieldSortable() 37 info.AddField("Name", "name", db.Varchar).FieldEditAble(editType.Text). 38 FieldFilterable(types.FilterType{Operator: types.FilterOperatorLike}) 39 info.AddField("Gender", "gender", db.Tinyint).FieldDisplay(func(model types.FieldModel) interface{} { 40 if model.Value == "0" { 41 return "men" 42 } 43 if model.Value == "1" { 44 return "women" 45 } 46 return "unknown" 47 }).FieldEditAble(editType.Switch).FieldEditOptions(types.FieldOptions{ 48 {Value: "0", Text: "👦"}, 49 {Value: "1", Text: "👧"}, 50 }).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).FieldFilterOptions(types.FieldOptions{ 51 {Value: "0", Text: "men"}, 52 {Value: "1", Text: "women"}, 53 }) 54 info.AddColumn("Personality", func(value types.FieldModel) interface{} { 55 return "handsome" 56 }) 57 info.AddColumnButtons("see more", types.GetColumnButton("see more", icon.Info, 58 action.PopUp("/see/more/example", "see more", func(ctx *context.Context) (success bool, msg string, data interface{}) { 59 return true, "ok", "<h1>Detail</h1><p>balabala</p>" 60 }))) 61 info.AddField("Phone", "phone", db.Varchar).FieldFilterable() 62 info.AddField("City", "city", db.Varchar).FieldFilterable() 63 info.AddField("Avatar", "avatar", db.Varchar).FieldDisplay(func(value types.FieldModel) interface{} { 64 return template.Default().Image(). 65 SetSrc(`//quick.go-admin.cn/demo/assets/dist/img/gopher_avatar.png`). 66 SetHeight("120").SetWidth("120").WithModal().GetContent() 67 }) 68 info.AddField("CreatedAt", "created_at", db.Timestamp). 69 FieldFilterable(types.FilterType{FormType: form.DatetimeRange}) 70 info.AddField("UpdatedAt", "updated_at", db.Timestamp).FieldEditAble(editType.Datetime) 71 72 info.AddActionButton("google", action.Jump("https://google.com")) 73 info.AddActionButton("Audit", action.Ajax("/admin/audit", 74 func(ctx *context.Context) (success bool, msg string, data interface{}) { 75 fmt.Println("PostForm", ctx.PostForm()) 76 return true, "success", "" 77 })) 78 info.AddActionButton("Preview", action.PopUp("/admin/preview", "Preview", 79 func(ctx *context.Context) (success bool, msg string, data interface{}) { 80 return true, "", "<h2>hello world</h2>" 81 })) 82 info.AddButton("jump", icon.User, action.JumpInNewTab("/admin/info/authors", "authors")) 83 info.AddButton("popup", icon.Terminal, action.PopUp("/admin/popup", "Popup Example", 84 func(ctx *context.Context) (success bool, msg string, data interface{}) { 85 return true, "", "<h2>hello world</h2>" 86 })) 87 88 info.AddButton("iframe", icon.Tv, action.PopUpWithIframe("/admin/iframe", "Iframe Example", 89 action.IframeData{Src: "/admin/info/authors"}, "900px", "560px")) 90 info.AddButton("form", icon.Folder, action.PopUpWithForm(action.PopUpData{ 91 Id: "/admin/popup/form", 92 Title: "Popup Form Example", 93 Width: "900px", 94 Height: "430px", 95 }, func(panel *types.FormPanel) *types.FormPanel { 96 panel.AddField("Name", "name", db.Varchar, form.Text) 97 panel.AddField("Age", "age", db.Int, form.Number) 98 panel.AddField("HomePage", "homepage", db.Varchar, form.Url).FieldDefault("http://google.com") 99 panel.AddField("Email", "email", db.Varchar, form.Email).FieldDefault("xxxx@xxx.com") 100 panel.AddField("Birthday", "birthday", db.Varchar, form.Date).FieldDefault("2010-09-03 18:09:05") 101 panel.AddField("Time", "time", db.Varchar, form.Datetime).FieldDefault("2010-09-05") 102 panel.EnableAjax("Request Success", "Request Failed") 103 return panel 104 }, "/admin/popup/form")) 105 106 info.AddButton("ajax", icon.Android, action.Ajax("/admin/ajax", 107 func(ctx *context.Context) (success bool, msg string, data interface{}) { 108 return true, "success", "" 109 })) 110 info.AddSelectBox("gender", types.FieldOptions{ 111 {Value: "0", Text: "men"}, 112 {Value: "1", Text: "women"}, 113 }, action.FieldFilter("gender")) 114 115 info.SetTable("users").SetTitle("Users").SetDescription("Users") 116 117 formList := userTable.GetForm() 118 formList.AddField("ID", "id", db.Int, form.Default).FieldDisplayButCanNotEditWhenUpdate().FieldDisableWhenCreate() 119 formList.AddField("Ip", "ip", db.Varchar, form.Text) 120 formList.AddField("Name", "name", db.Varchar, form.Text) 121 formList.AddField("Gender", "gender", db.Tinyint, form.Radio). 122 FieldOptions(types.FieldOptions{ 123 {Text: "men", Value: "0"}, 124 {Text: "women", Value: "1"}, 125 }) 126 formList.AddField("Country", "country", db.Tinyint, form.SelectSingle). 127 FieldOptions(types.FieldOptions{ 128 {Text: "China", Value: "0"}, 129 {Text: "America", Value: "1"}, 130 {Text: "England", Value: "2"}, 131 {Text: "Canada", Value: "3"}, 132 }).FieldDefault("0").FieldOnChooseAjax("city", "/choose/country", 133 func(ctx *context.Context) (bool, string, interface{}) { 134 country := ctx.FormValue("value") 135 var data = make(selection.Options, 0) 136 switch country { 137 case "0": 138 data = selection.Options{ 139 {Text: "Beijing", ID: "beijing"}, 140 {Text: "ShangHai", ID: "shangHai"}, 141 {Text: "GuangZhou", ID: "guangZhou"}, 142 {Text: "ShenZhen", ID: "shenZhen"}, 143 } 144 case "1": 145 data = selection.Options{ 146 {Text: "Los Angeles", ID: "los angeles"}, 147 {Text: "Washington, dc", ID: "washington, dc"}, 148 {Text: "New York", ID: "new york"}, 149 {Text: "Las Vegas", ID: "las vegas"}, 150 } 151 case "2": 152 data = selection.Options{ 153 {Text: "London", ID: "london"}, 154 {Text: "Cambridge", ID: "cambridge"}, 155 {Text: "Manchester", ID: "manchester"}, 156 {Text: "Liverpool", ID: "liverpool"}, 157 } 158 case "3": 159 data = selection.Options{ 160 {Text: "Vancouver", ID: "vancouver"}, 161 {Text: "Toronto", ID: "toronto"}, 162 } 163 default: 164 data = selection.Options{ 165 {Text: "Beijing", ID: "beijing"}, 166 {Text: "ShangHai", ID: "shangHai"}, 167 {Text: "GuangZhou", ID: "guangZhou"}, 168 {Text: "ShenZhen", ID: "shenZhen"}, 169 } 170 } 171 return true, "ok", data 172 }, "", `'phone':$(".phone").val(),`) 173 formList.AddField("Phone", "phone", db.Varchar, form.Custom). 174 FieldCustomContent(` 175 <span class="input-group-addon"><i class="fa fa-pencil fa-fw"></i></span> 176 <input type="text" name="{{.Field}}" value="{{.Value}}" class="form-control {{.Field}}" placeholder="please input {{.Head}}">`) 177 formList.AddField("City", "city", db.Varchar, form.SelectSingle). 178 FieldOptionInitFn(func(val types.FieldModel) types.FieldOptions { 179 return types.FieldOptions{ 180 {Value: val.Value, Text: val.Value, Selected: true}, 181 } 182 }).FieldOptions(types.FieldOptions{ 183 {Text: "Beijing", Value: "beijing"}, 184 {Text: "ShangHai", Value: "shanghai"}, 185 {Text: "GuangZhou", Value: "guangzhou"}, 186 {Text: "ShenZhen", Value: "shenzhen"}, 187 }) 188 formList.AddField("Custom Field", "role", db.Varchar, form.Text). 189 FieldPostFilterFn(func(value types.PostFieldModel) interface{} { 190 fmt.Println("user custom field", value) 191 return "" 192 }) 193 194 formList.AddField("UpdatedAt", "updated_at", db.Timestamp, form.Default).FieldDisableWhenCreate() 195 formList.AddField("CreatedAt", "created_at", db.Timestamp, form.Datetime).FieldDisableWhenCreate() 196 197 userTable.GetForm().SetTabGroups(types. 198 NewTabGroups("id", "ip", "name", "gender", "country", "city"). 199 AddGroup("phone", "role", "created_at", "updated_at")). 200 SetTabHeaders("profile1", "profile2") 201 202 formList.SetTable("users").SetTitle("Users").SetDescription("Users") 203 204 formList.SetPostHook(func(values form2.Values) error { 205 fmt.Println("userTable.GetForm().PostHook", values) 206 return nil 207 }) 208 209 return 210 }