github.com/kotovmak/go-admin@v1.1.1/tests/tables/user.go (about) 1 package tables 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/kotovmak/go-admin/context" 8 "github.com/kotovmak/go-admin/modules/config" 9 "github.com/kotovmak/go-admin/modules/db" 10 "github.com/kotovmak/go-admin/plugins/admin/modules/table" 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: config.GetDatabases().GetDefault().Driver, 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).Where("gender", "=", 0) 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.AddField("Experience", "experience", db.Tinyint). 55 FieldFilterable(types.FilterType{FormType: form.Radio}). 56 FieldFilterOptions(types.FieldOptions{ 57 {Value: "0", Text: "one"}, 58 {Value: "1", Text: "two"}, 59 {Value: "3", Text: "three"}, 60 }).FieldHide() 61 info.AddField("Drink", "drink", db.Tinyint). 62 FieldFilterable(types.FilterType{FormType: form.Select}). 63 FieldFilterOptions(types.FieldOptions{ 64 {Value: "water", Text: "water"}, 65 {Value: "juice", Text: "juice"}, 66 {Value: "red bull", Text: "red bull"}, 67 }).FieldHide() 68 info.AddField("City", "city", db.Varchar).FieldFilterable() 69 info.AddField("Book", "name", db.Varchar).FieldJoin(types.Join{ 70 JoinField: "user_id", 71 Field: "id", 72 Table: "user_like_books", 73 }) 74 info.AddField("Avatar", "avatar", db.Varchar).FieldDisplay(func(value types.FieldModel) interface{} { 75 return "1231" 76 }) 77 info.AddField("CreatedAt", "created_at", db.Timestamp). 78 FieldFilterable(types.FilterType{FormType: form.DatetimeRange}) 79 info.AddField("UpdatedAt", "updated_at", db.Timestamp).FieldEditAble(editType.Datetime) 80 81 // =========================== 82 // Buttons 83 // =========================== 84 85 info.AddActionButton("google", action.Jump("https://google.com")) 86 info.AddActionButton("Audit", action.Ajax("/admin/audit", 87 func(ctx *context.Context) (success bool, msg string, data interface{}) { 88 fmt.Println("PostForm", ctx.PostForm()) 89 return true, "success", "" 90 })) 91 info.AddActionButton("Preview", action.PopUp("/admin/preview", "Preview", 92 func(ctx *context.Context) (success bool, msg string, data interface{}) { 93 return true, "", "<h2>preview content</h2>" 94 })) 95 info.AddButton("jump", icon.User, action.JumpInNewTab("/admin/info/authors", "authors")) 96 info.AddButton("popup", icon.Terminal, action.PopUp("/admin/popup", "Popup Example", 97 func(ctx *context.Context) (success bool, msg string, data interface{}) { 98 return true, "", "<h2>hello world</h2>" 99 })) 100 info.AddButton("ajax", icon.Android, action.Ajax("/admin/ajax", 101 func(ctx *context.Context) (success bool, msg string, data interface{}) { 102 return true, "Oh li get", "" 103 })) 104 info.AddSelectBox("gender", types.FieldOptions{ 105 {Value: "0", Text: "men"}, 106 {Value: "1", Text: "women"}, 107 }, action.FieldFilter("gender")) 108 109 info.SetTable("users").SetTitle("Users").SetDescription("Users") 110 111 formList := userTable.GetForm() 112 113 formList.AddField("Name", "name", db.Varchar, form.Text) 114 formList.AddField("Age", "age", db.Int, form.Number) 115 formList.AddField("Homepage", "homepage", db.Varchar, form.Url).FieldDefault("http://google.com") 116 formList.AddField("Email", "email", db.Varchar, form.Email).FieldDefault("xxxx@xxx.com") 117 formList.AddField("Birthday", "birthday", db.Varchar, form.Datetime).FieldDefault("2010-09-05") 118 formList.AddField("Password", "password", db.Varchar, form.Password) 119 formList.AddField("IP", "ip", db.Varchar, form.Ip) 120 formList.AddField("Cert", "certificate", db.Varchar, form.Multifile).FieldOptionExt(map[string]interface{}{ 121 "maxFileCount": 10, 122 }) 123 formList.AddField("Amount", "money", db.Int, form.Currency) 124 formList.AddField("Content", "resume", db.Text, form.RichText). 125 FieldDefault(`<h1>343434</h1><p>34344433434</p><ol><li>23234</li><li>2342342342</li><li>asdfads</li></ol><ul><li>3434334</li><li>34343343434</li><li>44455</li></ul><p><span style="color: rgb(194, 79, 74);">343434</span></p><p><span style="background-color: rgb(194, 79, 74); color: rgb(0, 0, 0);">434434433434</span></p><table border="0" width="100%" cellpadding="0" cellspacing="0"><tbody><tr><td> </td><td> </td><td> </td></tr><tr><td> </td><td> </td><td> </td></tr><tr><td> </td><td> </td><td> </td></tr><tr><td> </td><td> </td><td> </td></tr></tbody></table><p><br></p><p><span style="color: rgb(194, 79, 74);"><br></span></p>`) 126 127 formList.AddField("Switch", "website", db.Tinyint, form.Switch). 128 FieldHelpMsg("Will not be able to access when the site was off"). 129 FieldOptions(types.FieldOptions{ 130 {Value: "0"}, 131 {Value: "1"}, 132 }) 133 formList.AddField("Fruit", "fruit", db.Varchar, form.SelectBox). 134 FieldOptions(types.FieldOptions{ 135 {Text: "Apple", Value: "apple"}, 136 {Text: "Banana", Value: "banana"}, 137 {Text: "Watermelon", Value: "watermelon"}, 138 {Text: "Pear", Value: "pear"}, 139 }). 140 FieldDisplay(func(value types.FieldModel) interface{} { 141 return []string{"Pear"} 142 }) 143 formList.AddField("Country", "country", db.Tinyint, form.SelectSingle). 144 FieldOptions(types.FieldOptions{ 145 {Text: "China", Value: "china"}, 146 {Text: "America", Value: "america"}, 147 {Text: "England", Value: "england"}, 148 {Text: "Canada", Value: "canada"}, 149 }).FieldDefault("china").FieldOnChooseAjax("city", "/choose/country", 150 func(ctx *context.Context) (bool, string, interface{}) { 151 country := ctx.FormValue("value") 152 var data = make(selection.Options, 0) 153 switch country { 154 case "china": 155 data = selection.Options{ 156 {Text: "Beijing", ID: "beijing"}, 157 {Text: "ShangHai", ID: "shanghai"}, 158 {Text: "GuangZhou", ID: "guangzhou"}, 159 {Text: "ShenZhen", ID: "shenzhen"}, 160 } 161 case "america": 162 data = selection.Options{ 163 {Text: "Los Angeles", ID: "los angeles"}, 164 {Text: "Washington, dc", ID: "washington, dc"}, 165 {Text: "New York", ID: "new york"}, 166 {Text: "Las Vegas", ID: "las vegas"}, 167 } 168 case "england": 169 data = selection.Options{ 170 {Text: "London", ID: "london"}, 171 {Text: "Cambridge", ID: "cambridge"}, 172 {Text: "Manchester", ID: "manchester"}, 173 {Text: "Liverpool", ID: "liverpool"}, 174 } 175 case "canada": 176 data = selection.Options{ 177 {Text: "Vancouver", ID: "vancouver"}, 178 {Text: "Toronto", ID: "toronto"}, 179 } 180 default: 181 data = selection.Options{ 182 {Text: "Beijing", ID: "beijing"}, 183 {Text: "ShangHai", ID: "shangHai"}, 184 {Text: "GuangZhou", ID: "guangzhou"}, 185 {Text: "ShenZhen", ID: "shenZhen"}, 186 } 187 } 188 return true, "ok", data 189 }) 190 formList.AddField("City", "city", db.Varchar, form.SelectSingle). 191 FieldOptionInitFn(func(val types.FieldModel) types.FieldOptions { 192 return types.FieldOptions{ 193 {Value: val.Value, Text: val.Value, Selected: true}, 194 } 195 }).FieldOptions(types.FieldOptions{ 196 {Text: "Beijing", Value: "beijing"}, 197 {Text: "ShangHai", Value: "shanghai"}, 198 {Text: "GuangZhou", Value: "guangzhou"}, 199 {Text: "ShenZhen", Value: "shenzhen"}, 200 }) 201 formList.AddField("Gender", "gender", db.Tinyint, form.Radio). 202 FieldOptions(types.FieldOptions{ 203 {Text: "Boy", Value: "0"}, 204 {Text: "Girl", Value: "1"}, 205 }) 206 formList.AddField("Drink", "drink", db.Varchar, form.Select). 207 FieldOptions(types.FieldOptions{ 208 {Text: "Beer", Value: "beer"}, 209 {Text: "Juice", Value: "juice"}, 210 {Text: "Water", Value: "water"}, 211 {Text: "Red bull", Value: "red bull"}, 212 }). 213 FieldDefault("beer"). 214 FieldDisplay(func(value types.FieldModel) interface{} { 215 return strings.Split(value.Value, ",") 216 }). 217 FieldPostFilterFn(func(value types.PostFieldModel) interface{} { 218 return strings.Join(value.Value, ",") 219 }) 220 formList.AddField("Work Experience", "experience", db.Tinyint, form.SelectSingle). 221 FieldOptions(types.FieldOptions{ 222 {Text: "two years", Value: "0"}, 223 {Text: "three years", Value: "1"}, 224 {Text: "four years", Value: "2"}, 225 {Text: "five years", Value: "3"}, 226 }).FieldDefault("beer") 227 formList.SetTabGroups(types.TabGroups{ 228 {"name", "age", "homepage", "email", "birthday", "password", "ip", "certificate", "money", "resume"}, 229 {"website", "fruit", "country", "city", "gender", "drink", "experience"}, 230 }) 231 formList.SetTabHeaders("input", "select") 232 233 formList.SetTable("users").SetTitle("Users").SetDescription("Users") 234 235 return 236 }