Skip to content

Commit

Permalink
A add a search example on the user list page
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyide committed Dec 19, 2017
1 parent bc15b99 commit 0ca4759
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/models/UserModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ func init() {
/************************************************************/

//get user list
func Getuserlist(page int64, page_size int64, sort string) (users []orm.Params, count int64) {
func Getuserlist(page int64, page_size int64, sort string, searchMap map[string]string) (users []orm.Params, count int64) {
o := orm.NewOrm()
user := new(User)
qs := o.QueryTable(user)
// 实现搜索过滤
for k, v := range searchMap {
qs = qs.Filter(k, v)
}
var offset int64
if page <= 1 {
offset = 0
Expand Down Expand Up @@ -141,4 +145,3 @@ func GetUserById(id int64) (user User) {
o.Read(&user, "Id")
return user
}

6 changes: 5 additions & 1 deletion src/rbac/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ func (this *RoleController) AddAccess() {
func (this *RoleController) RoleToUserList() {
roleid, _ := this.GetInt64("Id")
if this.IsAjax() {
users, count := m.Getuserlist(1, 1000, "Id")
// 先声明map
var searchMap map[string]string
// 再使用make函数创建一个非nil的map,nil map不能赋值
searchMap = make(map[string]string)
users, count := m.Getuserlist(1, 1000, "Id", searchMap)
list, _ := m.GetUserByRoleId(roleid)
for i := 0; i < len(users); i++ {
for x := 0; x < len(list); x++ {
Expand Down
21 changes: 20 additions & 1 deletion src/rbac/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rbac

import (
"github.com/astaxie/beego"
m "github.com/beego/admin/src/models"
)

Expand All @@ -13,15 +14,33 @@ func (this *UserController) Index() {
page_size, _ := this.GetInt64("rows")
sort := this.GetString("sort")
order := this.GetString("order")
// 获取搜索条件
Username__exact := this.GetString("Username__exact")
Nickname__contains := this.GetString("Nickname__contains")

// 先声明map
var searchMap map[string]string
// 再使用make函数创建一个非nil的map,nil map不能赋值
searchMap = make(map[string]string)
// 最后给已声明的map赋值
if len(Username__exact) > 0 {
searchMap["Username__exact"] = Username__exact
}
if len(Nickname__contains) > 0 {
searchMap["Nickname__contains"] = Nickname__contains
}

beego.Warning(searchMap)
if len(order) > 0 {
if order == "desc" {
sort = "-" + sort
}
} else {
sort = "Id"
}
users, count := m.Getuserlist(page, page_size, sort)
users, count := m.Getuserlist(page, page_size, sort, searchMap)
if this.IsAjax() {

this.Data["json"] = &map[string]interface{}{"total": count, "rows": &users}
this.ServeJSON()
return
Expand Down
25 changes: 25 additions & 0 deletions views/easyui/rbac/user.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ function reloadrow(){
$("#datagrid").datagrid("reload");
}
//添加用户弹窗
function addrow(){
$("#dialog").dialog('open');
Expand Down Expand Up @@ -217,6 +218,17 @@ function delrow(){
}
});
}
// 搜索
function searchForm() {
// form表单数据转为json
var data = $("#searchform").serializeArray();
vac.ajax(URL+"/index",data,"POST",function(data){
console.log(data)
$("#datagrid").datagrid("loadData",data)
}
)
}
</script>
<body>
<table id="datagrid" toolbar="#tb"></table>
Expand All @@ -227,7 +239,20 @@ function delrow(){
<a href="#" icon='icon-cancel' plain="true" onclick="delrow()" class="easyui-linkbutton" >删除</a>
<a href="#" icon='icon-reload' plain="true" onclick="reloadrow()" class="easyui-linkbutton" >刷新</a>
<a href="#" icon='icon-edit' plain="true" onclick="updateuserpassword()" class="easyui-linkbutton" >修改用户密码</a>
<div style="float:right;height:auto">
<form id="searchform">
用户名:<input name="Username__exact" value= ""/>
昵称;<input name = "Nickname__contains" value=""/>
<a href="#" icon='icon-search' plain="true" onclick="searchForm()" class="easyui-linkbutton" >搜索</a>
</form>
</div>
</div>
<!--表格内的右键菜单-->
<div id="mm" class="easyui-menu" style="width:120px;display: none" >
<div iconCls='icon-add' onclick="addrow()">新增</div>
Expand Down

0 comments on commit 0ca4759

Please sign in to comment.