一.需求分析
- 从 02资料/章节214商品修改页面显示信息 中把资源拷贝到项目中
- 点击”查询商品”页面中编辑按钮弹出修改页面
- 页面显示的数据是服务端根据客户端传递过来的商品id进行查询,查询时除了查询商品表(tb_item)以外还需要查询商品描述表(tb_item_desc)和商品类目表(tb_item_cat)把这些数据返回json
- 客户端要求商品类目名称(CategoryName)和描述(Desc)
二.代码实现
//给修改页面使用type TbItemDescChild struct { TbItem CategoryName string Desc string}
//根据主键查询内容func selByIdDao(id int) *TbItem{ rows,err:= commons.Dql("select * from tb_item where id=?",id) if err!=nil{ fmt.Println(err) return nil } if rows.Next(){ t := new (TbItem) var s sql.NullString rows.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 return t } return nil}
- 在/item/desc/TbItemDescDao.go中添加
//根据主键查询func selByIdDao(id int) *TbItemDesc{ r,err:=commons.Dql("select * from tb_item_desc where item_id=?",id) if err!=nil{ fmt.Println(err) return nil } if r.Next(){ t := new(TbItemDesc) r.Scan(&t.ItemId,&t.ItemDesc,&t.Created,&t.Updated) return t } return nil}
- 在/item/desc/TbItemDescService.go中添加
func SelByIdService(id int) * TbItemDesc{ return selByIdDao(id)}
- 在/item/TbItemService.go中添加
//修改页面显示信息func showItemDescCatService(id int) TbItemDescChild{ item := selByIdDao(id) var c TbItemDescChild c.Id = item.Id c.Updated = item.Updated c.Created = item.Created c.Barcode = item.Barcode c.Cid = item.Cid c.Title = item.Title c.SellPoint = item.SellPoint c.Price = item.Price c.Image = item.Image c.Status = item.Status c.Num = item.Num //商品类目 c.CategoryName = cat.ShowCatByIdService(c.Cid).Name //商品描述 c.Desc = desc.SelByIdService(c.Id).ItemDesc return c}
- 在/item/TbItemController.go中添加
//显示修改页面信息func showItemDescCatController(w http.ResponseWriter, r *http.Request){ id,_:= strconv.Atoi(r.FormValue("id")) c:=showItemDescCatService(id) b,_:=json.Marshal(c) w.Header().Set(commons.HEADER_CONTENT_TYPE,commons.JSON_HEADER) w.Write(b)}func ItemHandler() { commons.Router.HandleFunc("/showItem", showItemController) commons.Router.HandleFunc("/item/delete", delByIdsController) commons.Router.HandleFunc("/item/instock", instockController) commons.Router.HandleFunc("/item/offstock", offstockController) commons.Router.HandleFunc("/item/imageupload", imagesUploadController) commons.Router.HandleFunc("/item/add", insertControllew) commons.Router.HandleFunc("/item/showItemById", showItemDescCatController) //商品修改页面信息显示}