工具 tools/datatables.go
package tools
import (
"fmt"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/orm"
"strconv"
)
/*
* aColumns []string `SQL Columns to display`
* thismodel interface{} `SQL model to use`
* ctx *context.Context `Beego ctx which contains httpcontext`
* maps []orm.Params `return result in a interface map as []orm.Params`
* count int64 `return iTotalDisplayRecords`
* counts int64 `return iTotalRecords`
*
*/
func Datatables(aColumns []string, thismodel interface{}, Input *context.BeegoInput, where interface{}) (maps []orm.Params, count int64, counts int64) {
/*
* Paging
* 分页请求
*/
iDisplayStart, _ := strconv.Atoi(Input.Query("iDisplayStart"))
iDisplayLength, _ := strconv.Atoi(Input.Query("iDisplayLength"))
/*
* Ordering
* 排序请求
*/
querysOrder := []string{}
if iSortCol_0, _ := strconv.Atoi(Input.Query("iSortCol_0")); iSortCol_0 > -1 {
ranges, _ := strconv.Atoi(Input.Query("iSortingCols"))
for i := 0; i < ranges; i++ {
istring := strconv.Itoa(i)
if iSortcol := Input.Query("bSortable_" + Input.Query("iSortCol_"+istring)); iSortcol == "true" {
sordir := Input.Query("sSortDir_" + istring)
thisSortCol, _ := strconv.Atoi(Input.Query("iSortCol_" + istring))
if sordir == "asc" {
querysOrder = append(querysOrder, aColumns[thisSortCol])
} else {
querysOrder = append(querysOrder, "-"+aColumns[thisSortCol])
}
}
}
}
/*
* Filtering
* 快速过滤器
*/
//querysFilter := []string{}
cond := orm.NewCondition()
if len(Input.Query("sSearch")) > 0 {
for i := 0; i < len(aColumns); i++ {
cond = cond.Or(aColumns[i]+"__icontains", Input.Query("sSearch"))
}
}
/* Individual column filtering */
for i := 0; i < len(aColumns); i++ {
if Input.Query("bSearchable_"+strconv.Itoa(i)) == "true" && len(Input.Query("sSearch_"+strconv.Itoa(i))) > 0 {
cond = cond.And(aColumns[i]+"__icontains", Input.Query("sSearch"))
}
}
//where条件
wheres, ok := where.(map[string]interface{})
if ok {
for k, v := range wheres {
fmt.Println(k, v)
cond = cond.And(k, v)
}
}
fmt.Println(where)
//用户管理GID
gid := Input.Query("gid")
if gid != "" {
gid2, _ := strconv.Atoi(gid)
cond = cond.And("gid", int64(gid2))
}
//客服管理
accountid := Input.Query("aid")
fmt.Println(accountid)
if accountid != "" {
aid, _ := strconv.Atoi(accountid)
cond = cond.And("accountid", int64(aid))
}
/*
* GetData
* 数据请求
*/
o := orm.NewOrm()
qs := o.QueryTable(thismodel)
counts, _ = qs.Count()
qs = qs.Limit(iDisplayLength, iDisplayStart)
qs = qs.SetCond(cond)
for _, v := range querysOrder {
qs = qs.OrderBy(v)
}
qs.Values(&maps)
count, _ = qs.Count()
return maps, count, counts
}
/*
* 显示datatables列表页数据
*/
func (this *UserController) List() {
aColumns := []string{
"Id",
"Username",
"Realname",
"Status",
"Gid",
}
var where = make(map[string]interface{})
err := this.CheckRule("isUserAll")
if err != nil {
where["gid"] = this.GetUserGid()
}
fmt.Println(where)
maps, count, counts := d.Datatables(aColumns, user, this.Ctx.Input, where)
var output = make([][]interface{}, len(maps))
for i, m := range maps {
for _, v := range aColumns {
if v == "Lasttime" {
output[i] = append(output[i], m[v].(time.Time).Format("2006-01-02 15:04:05"))
} else {
output[i] = append(output[i], m[v])
}
}
}
data := make(map[string]interface{}, count)
data["sEcho"] = this.GetString("sEcho")
data["iTotalRecords"] = counts
data["iTotalDisplayRecords"] = count
data["aaData"] = output
this.Data["json"] = data
this.ServeJson()
}
//datatables显示列表
var table = $('#datatables');//表格对象
table.dataTable( {
"sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",//定义DataTable布局的一个强大属性
"sPaginationType": "bootstrap",//分页样式使用bootstrap
"oLanguage": {//语言设置
"sLengthMenu": "每页显示 _MENU_ 条记录",
"sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
"oPaginate": {
"sFirst": "首页",
"sPrevious": "前一页",
"sNext": "后一页",
"sLast": "尾页"
},
"sZeroRecords": "抱歉, 没有找到",
"sInfoEmpty": "没有数据"
},
"bProcessing": true, //当datatable获取数据时候是否显示正在处理提示信息。
"bServerSide": true, //客户端处理分页
"sAjaxSource": "/rule/list", //ajax请求地址
'bStateSave': true, //开关,是否打开客户端状态记录功能。这个数据是记录在cookies中的,打开了这个记录后,即使刷新一次页面,或重新打开浏览器,之前的状态都是保存下来的
"aoColumnDefs": [{ //给每个单独的列设置不同的填充,或者使用aoColumns也行
"aTargets": [3],
"mData": null,
"bSortable": false,
"bSearchable": false,
"mRender": function (data, type, full) {
if(full[3] == 1){
return "路由规则"
}else if(full[3] == 2){
return "普通规则"
}
}
},{
"aTargets": [4],
"mData": null,
"bSortable": false,
"bSearchable": false,
"mRender": function (data, type, full) {
return '<a data-toggle="modal" data-target="#myModal" data-title="' + full[0] + '" class="btn btn-success" href="#"><i class="icon-edit icon-white"></i>修改</a>' +' '+'<a data-title="' + full[0] + '" class="btn btn-danger" href="#' + full[0] + '"><i class="icon-user icon-white"></i>删除</a>';
}
}]
});