基本使用


生成了:

  1. import (
  2. ...
  3. )
  4. func GetUserTable() (userTable table.Table) {
  5. // config the table model.
  6. userTable = table.NewDefaultTable(table.Config{...})
  7. info := userTable.GetInfo()
  8. // set id sortable.
  9. info.AddField("ID", "id", db.Int).FieldSortable(true)
  10. info.AddField("Name", "name", db.Varchar)
  11. ...
  12. info.SetTable("users").SetTitle("Users").SetDescription("Users").
  13. SetAction(template.HTML(`<a href="http://google.com"><i class="fa fa-google"></i></a>`)) // custom operation button
  14. ...
  15. }
  1. // 添加一个字段,字段标题为 ID,字段名为 id,字段类型为 int
  2. info.AddField("ID", "id", db.Int)
  3. info.AddField("Name", "name", db.Varchar)
  4. // 添加第三个字段,一个sql表不存在的字段
  5. info.AddField("Custom", "custom", db.Varchar)

修改显示输出

  1. // 根据字段的值输出对应的内容
  2. info.AddField("Gender", "gender", db.Tinyint).FieldDisplay(func(model types.FieldModel) interface{} {
  3. if model.Value == "0" {
  4. return "men"
  5. }
  6. if model.Value == "1" {
  7. return "women"
  8. }
  9. return "unknown"
  10. })
  11. // 输出html
  12. info.AddField("Name", "name", db.Varchar).FieldDisplay(func(model types.FieldModel) interface{} {
  13. return "<span class='label'>" + model.Value + "</span>"
  14. })

FieldDisplay方法接收的匿名函数绑定了当前行的数据对象,可以在里面调用当前行的其它字段数据

  1. info.AddField("First Name", "first_name", db.Varchar).FieldHide()
  2. // 不存的字段列
  3. info.AddField("Full Name", "full_name", db.Varchar).FieldDisplay(func(model types.FieldModel) interface{} {
  4. return model.Row["first_name"].(string) + " " + model.Row["last_name"].(string)
  5. })

禁用编辑按钮

  1. info.HideEditButton()

禁用删除按钮

  1. info.HideDeleteButton()
  1. info.HideFilterArea()

预查询

  1. info.SetFilterFormLayout(layout form.Layout)
  1. // 顺序
  2. info.SetSortAsc()
  3. // 倒序
  4. info.SetSortDesc()
  1. // 增加字段名
  2. info.AddField("Role Name", "role_name", db.Varchar).FieldJoin(types.Join{
  3. Table: "role", // 连表的表名
  4. Field: "id", // 要连表的字段
  5. JoinField: "user_id", // 连表的表的字段
  6. })

这将会生成类似这样的sql语句:

  1. select ..., role.`role_name` from users left join role on users.`id` = role.`user_id` where ...

如果您需要新增一些功能按钮,可以调用:

例如:

  1. import (
  2. ...
  3. "github.com/GoAdminGroup/go-admin/template/icon"
  4. "github.com/GoAdminGroup/go-admin/template/types/action"
  5. ...
  6. )
  7. info.AddButton("今日情况", icon.Save, action.PopUp("/admin/data/analyze", "数据分析"))

添加了一个popup的操作,将会去请求对应路由,对应路由返回的就是popup的内容,「数据分析」为对应popup的标题。