一. 查询功能分析
- 从item.html页面中复制部分脚本
- datagrid请求的url为/showItem
- 填充的列属性中数据除了tb_item表以外,还有CategoryName是商品对应的类目名称,存在于tb_item_cat表中,所以在查询时是两表查询
$('#item_table').datagrid({ url: '/showItem', columns: [[ {field: 'Id', title: '商品ID', width: 100}, {field: 'Title', title: '商品标题', width: 100}, {field: 'CategoryName', title: '叶子类目', width: 100}, {field: 'SellPoint', title: '卖点', width: 100}, {field: 'Price', title: '价格', width: 100}, {field: 'Num', title: '库存数量', width: 100}, {field: 'Barcode', title: '条形码', width: 100}, {field: 'Status', title: '状态', width: 100}, {field: 'Created', title: '创建日期', width: 100}, {field: 'Updated', title: '更新日期', width: 100} ]],
- EasyUI中Datagrid分页时要求返回数据格式为:(不是EgoResult了,否则无法正确显示)
{"rows":当前页数据,"total":总条数}
二.代码实现
package commonstype Datagrid struct { //当前页显示的数据 Rows interface{} `json:"rows"` //总个数 Total int `json:"total"`}
- 在src下新建文件夹item,并在item文件夹下新建TbItem.go
package item//商品type TbItem struct { Id int Title string SellPoint string Price int Num int Barcode string Image string Cid int Status int8 Created string Updated string}
- 在item下新建TbItemDao.go实现数据访问,注意当数据库中有NULL值时的转换
package itemimport ( "commons" "fmt" "database/sql")/*rows:每页显示的条数page:当前第几页 */func selByPageDao(rows,page int) []TbItem{ //第一个表示:从哪条开始查询,0算起 第二个:查询几个 r,err:=commons.Dql("select * from tb_item limit ?,?",rows*(page-1),rows) if err!=nil{ fmt.Println(err) return nil } ts:=make([]TbItem,0) for r.Next(){ var t TbItem var s sql.NullString //如果直接使用t.Barcode由于数据库中列为Null导致填充错误 r.Scan(&t.Id,&t.Title,&t.SellPoint,&t.Price,&t.Num,&s,&t.Image,&t.Cid,&t.Status,&t.Created,&t.Updated) t.Barcode=s.String ts=append(ts,t) } commons.CloseConn() return ts}
- 在item文件夹下新建TbItemService.go编写业务代码
package itemimport "commons"func showItemService(page,rows int) (e *commons.Datagrid){ ts:=selByPageDao(rows,page) if ts!=nil{ e= new(commons.Datagrid) e.Rows=ts return } return nil}
- 在item文件夹下新建TbItemController.go编写控制器
package itemimport ( "net/http" "strconv" "encoding/json" "commons")func ItemHandler(){ commons.Router.HandleFunc("/showItem",showItemController)}//显示商品信息func showItemController(w http.ResponseWriter,r *http.Request){ page,_:=strconv.Atoi(r.FormValue("page")) rows,_:=strconv.Atoi(r.FormValue("rows")) datagrid:=showItemService(page,rows) b,_:=json.Marshal(datagrid) w.Header().Set("Content-Type","application/json;charset=utf-8") w.Write(b)}
package mainimport ( "net/http" "html/template" "user" "commons" "github.com/gorilla/mux" "item")//显示登录页面func welcome(w http.ResponseWriter, r *http.Request) { t, _ := template.ParseFiles("view/login.html") t.Execute(w, nil)}//restful显示页面func showPage(w http.ResponseWriter, r *http.Request){ vars:=mux.Vars(r) t,_:=template.ParseFiles("view/"+vars["page"]+".html") t.Execute(w,nil)}func main() { commons.Router.PathPrefix("/static").Handler(http.StripPrefix("/static/",http.FileServer(http.Dir("static")))) commons.Router.HandleFunc("/",welcome) //满足/page/{page}格式的处理 commons.Router.HandleFunc("/page/{page}",showPage) //用户 user.UserHandler() //商品 item.ItemHandler() http.ListenAndServe(":80",commons.Router)}