基本使用
生成了:
import (
...
)
func GetUserTable() (userTable table.Table) {
// config the table model.
userTable = table.NewDefaultTable(table.Config{...})
info := userTable.GetInfo()
// set id sortable.
info.AddField("ID", "id", db.Int).FieldSortable(true)
info.AddField("Name", "name", db.Varchar)
...
info.SetTable("users").SetTitle("Users").SetDescription("Users").
SetAction(template.HTML(`<a href="http://google.com"><i class="fa fa-google"></i></a>`)) // custom operation button
...
}
// 添加一个字段,字段标题为 ID,字段名为 id,字段类型为 int
info.AddField("ID", "id", db.Int)
info.AddField("Name", "name", db.Varchar)
// 添加第三个字段,一个sql表不存在的字段
info.AddField("Custom", "custom", db.Varchar)
修改显示输出
// 根据字段的值输出对应的内容
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"
})
// 输出html
info.AddField("Name", "name", db.Varchar).FieldDisplay(func(model types.FieldModel) interface{} {
return "<span class='label'>" + model.Value + "</span>"
})
FieldDisplay方法接收的匿名函数绑定了当前行的数据对象,可以在里面调用当前行的其它字段数据
info.AddField("First Name", "first_name", db.Varchar).FieldHide()
// 不存的字段列
info.AddField("Full Name", "full_name", db.Varchar).FieldDisplay(func(model types.FieldModel) interface{} {
return model.Row["first_name"].(string) + " " + model.Row["last_name"].(string)
})
禁用编辑按钮
info.HideEditButton()
禁用删除按钮
info.HideDeleteButton()
info.HideFilterArea()
预查询
info.SetFilterFormLayout(layout form.Layout)
// 顺序
info.SetSortAsc()
// 倒序
info.SetSortDesc()
// 增加字段名
info.AddField("Role Name", "role_name", db.Varchar).FieldJoin(types.Join{
Table: "role", // 连表的表名
Field: "id", // 要连表的字段
JoinField: "user_id", // 连表的表的字段
})
这将会生成类似这样的sql语句:
select ..., role.`role_name` from users left join role on users.`id` = role.`user_id` where ...
如果您需要新增一些功能按钮,可以调用:
例如:
import (
...
"github.com/GoAdminGroup/go-admin/template/icon"
"github.com/GoAdminGroup/go-admin/template/types/action"
...
)
info.AddButton("今日情况", icon.Save, action.PopUp("/admin/data/analyze", "数据分析"))
添加了一个popup的操作,将会去请求对应路由,对应路由返回的就是popup的内容,「数据分析」为对应popup的标题。