217-beego分頁校驗自增Jquery
阿新 • • 發佈:2019-01-12
beego分頁校驗自增Jquery start = (pageIndex-1)*pageSize qs.Limit(pageSize,start).All(&goods) //實現頁碼顯示 <a href="/list?pageIndex={{$value}}" > preIndex := pageIndex-1 nextIndex := pageIndex+1 this.Data["preIndex"]=preIndex this.Data["nextIndex"]=nextIndex if sort=""{ qs.Limit(pageSize,start).All(&goods) this.Data["sort"]="" }else if sort="price"{ qs.OrderBy("Price").Limit(pageSize,start).All(&goods) this.Data["sort"]="price" }else{ qs.OrderBy("Sales").Limit() } 看下完整程式碼 func (this *GoodsController) ShowList() { //獲取資料 typeId, err := this.GetInt("typeId") //校驗資料 if err != nil { beego.Error("請求連結錯誤") return } //處理資料 //查詢操作 o := orm.NewOrm() var goods []models.GoodsSKU qs := o.QueryTable("GoodsSKU").RelatedSel("GoodsType").Filter("GoodsType__Id", typeId) //獲取總記錄數 count, err := qs.Count() if err != nil { beego.Error("請求連結錯誤") return } //獲取總頁數 pageSize := 4 pageCount := math.Ceil(float64(count) / float64(pageSize)) //獲取當前頁碼 pageIndex, err := this.GetInt("pageIndex") if err != nil { pageIndex = 1 } pages := pageEditor(int(pageCount), pageIndex) this.Data["pages"] = pages start := (pageIndex - 1 ) * pageSize //排序 sort := this.GetString("sort") if sort == "" { //預設排序 qs.Limit(pageSize, start).All(&goods) this.Data["sort"] = "" } else if sort == "price" { //價格排序 qs.OrderBy("-Price").Limit(pageSize, start).All(&goods) this.Data["sort"] = "price" } else { //銷量排序 qs.OrderBy("Sales").Limit(pageSize, start).All(&goods) this.Data["sort"] = "sale" } //實現頁碼顯示 上一頁下一頁頁碼處理 var preIndex, nextIndex int if pageIndex == 1 { preIndex = 1 } else { preIndex = pageIndex - 1 } if pageIndex == int(pageCount) { nextIndex = int(pageCount) } else { nextIndex = pageIndex + 1 } this.Data["preIndex"] = preIndex this.Data["nextIndex"] = nextIndex //返回資料 this.Data["pageIndex"] = pageIndex this.Data["typeId"] = typeId this.Data["goods"] = goods this.Layout = "layout.html" this.TplName = "list.html" } //分頁處理函式 func pageEditor(pageCount int, pageIndex int) []int { var pages []int if pageCount < 5 { pages = make([]int, pageCount) for i := 1; i <= pageCount; i++ { pages[i-1] = i } } else if pageIndex <= 3 { pages = make([]int, 5) for i := 1; i <= 5; i++ { pages[i-1] = i } } else if pageIndex >= pageCount-2 { pages = make([]int, 5) for i := 1; i <= 5; i++ { pages[i-1] = pageCount - 5 + i } } else { pages = make([]int, 5) for i := 1; i <= 5; i++ { pages[i-1] = pageIndex - 3 + i } } return pages } //商品搜尋 func (this *GoodsController) HandleSearch(){ //獲取資料 searchName := this.GetString("searchName") //校驗資料 if searchName==""{ this.Redirect("/",302) return } //處理資料 o := orm.NewOrm() //select * from goodsSKU where like o.QueryTable("GoodsSKU").Filter("Name__contains",searchName).All(&goods) this.Data["goods"]=goods //返回資料 this.TplName="" } //找到相應標籤 觸發某個事件 function CountTotalPrice(){ price = $(".show_price").children("em").text() count = $(".num_show").val() //型別轉換 price=parseFloat(price) count=parseInt(count) //計算 $(".total").children("em").text(totalPrice.toFixed()) 再來一次 CountTotalPrice() //計算總價 function CountTotalPrice(){ //找單價和數量標籤,獲取資料 price = $(".show_price").children("em").text() count = $(".num_show").val() //型別轉換 price = parseFloat(price) count = parseInt(count) //計算 totalPrice=price*count //寫回頁面 $(".total").children("em").text(totalPrice.toFixed(2) + "元") } 再來一次 //計算總價 function CountTotalPrice(){ //單價和數量 price = $(".show_price").children("em").text() count = $(".num_show").val() //型別轉換 price = parseFloat(price) count = parseInt(count) //計算 totalPrice =price* count //寫回頁面 $(".total").children("em").text(totalPrice.toFixed(2) + "元") } 再寫一個簡單的 //新增商品數量 $(".add").click(function(){ count = $(".num_show").val() count = parseInt(count) +1 $(".num_show").val(count) CountTotalPrice() }) //減少商品數量 $(".minus").click(function(){ count = $(".num_show").val() count = parseInt(count) -1 if count <1{ count=1 } $(".num_show").val(count) CountTotalPrice() }) 寫點jquery吧 if(isNaN(count)||count < 1 || count.trim().length==0){ count =1 } 完整程式碼 $(".num_show").blur(function(){ count=$(".num_show").val() if(isNaN(count)||count<1||count.trim().length==0){ count=1 } $(".num_show").val(parseInt(count)) CountTotalPrice() }) 我們整理一下點選增加,點選減少和手動輸入 //點選增加 $(".add").click(function(){ count=$(".num_show").val() count=parseInt(count)+1 $(".num_show").val(count) CountTotalPrice() }) //點選減少 $(".minus").click(function(){ count=$(".num_show").val() count=parseInt(count)-1 if(count<1){ count=1 } $(".num_show").val(count) CountTotalPrice() }) //手動輸入 $(".num_show").blur(function(){ count=$(".num_show").val() if(isNaN(count)||count<1||count.trim().length==0){ count=1 } $(".num_show").val(parseInt(count)) CountTotalPrice() }) //看下購物車 $('#add_cart').click(function(){ count= goodsId= $.post("/addCart",param,function(data){ }) }) 然後我們寫一個car.go購物車 type CartController struct{ beego.Controller } func (this *CartController) HandleAddCart(){ //獲取 //校驗 //處理 //返回 //獲取資料 count,err1 := this.GetInt("count") goodsId,err2 := this.GetInt("goodsId") //校驗資料 if err!=nil || err2!=nil{ beego.Error("ajax傳遞資料失敗") return } beego.Info("count=",count,"goodsId=",goodsId) //處理資料 resp := make(map[string]interface{}) userName := this.GetSession("userName") if userName==nil{ resp["errno"]=1 resp["errmsg"]="使用者未登入" this.data["json"]=resp this.ServeJSON() } //redis儲存 conn,err := redis.Dial("tcp",""192.168.123.123:6379") if err!=nil{ resp["errno"]=2 resp["errmsg"]="redis連結失敗" this.Data["json"]=resp this.ServeJSON() } defer conn.Close() conn.Do("hset","cart_"+userName.(string),goodsId,count) //返回資料 resp["errno"]=5 resp["errmsg"]="OK" this.Data["json"]=resp this.ServeJSON() } 看看完整程式碼 func(this*CartController)HandleAddCart(){ //獲取資料 count,err1 :=this.GetInt("count") goodsId,err2:=this.GetInt("goodsId") //校驗資料 if err1 != nil || err2 != nil { beego.Error("ajax傳遞資料失敗") return } //處理資料 //1.有個容器儲存json資料 resp := make(map[string]interface{}) userName := this.GetSession("userName") if userName == nil{ resp["errno"] = 1 resp["errmsg"] = "使用者未登入" //把容器傳遞給前段頁面 this.Data["json"] = resp //指定接受方式 this.ServeJSON() } //處理資料 //redis的儲存 conn,err :=redis.Dial("tcp","192.168.110.81:6379") if err != nil { resp["errno"] = 2 resp["errmsg"] = "redis連結失敗" //把容器傳遞給前段頁面 this.Data["json"] = resp //指定接受方式 this.ServeJSON() } defer conn.Close() conn.Do("hset","cart_"+userName.(string),goodsId,count) //返回資料 resp["errno"] = 5 resp["errmsg"] = "Ok" this.Data["json"] = resp this.ServeJSON() } 完整程式碼 我加點註釋吧 func(this*CartController)HandleAddCart(){ //獲取資料 //我們要知道是哪個商品,然後有多少個,比如id為abc12345,數量為5個 count,err1 :=this.GetInt("count") goodsId,err2:=this.GetInt("goodsId") //校驗資料 if err1 != nil || err2 != nil { beego.Error("ajax傳遞資料失敗") return } //處理資料 //1.有個容器儲存json資料 //我們要來一個map,key為string,value為interface{} resp := make(map[string]interface{}) //從session中取出userName userName := this.GetSession("userName") if userName == nil{ resp["errno"] = 1 resp["errmsg"] = "使用者未登入" //把容器傳遞給前段頁面 this.Data["json"] = resp //指定接受方式 this.ServeJSON() } //處理資料 //redis的儲存 //連線一下redis conn,err :=redis.Dial("tcp","192.168.110.81:6379") if err != nil { resp["errno"] = 2 resp["errmsg"] = "redis連結失敗" //把容器傳遞給前段頁面 this.Data["json"] = resp //指定接受方式 this.ServeJSON() } defer conn.Close() //我們用hset也就是redis的hash儲存,這樣的話,就是一個key對應一個鍵值對 //也就是key : {key:value} 所以userName對應了id和count conn.Do("hset","cart_"+userName.(string),goodsId,count) //返回資料 resp["errno"] = 5 resp["errmsg"] = "Ok" this.Data["json"] = resp this.ServeJSON() } 現在展示一下 func (this *CartController) ShowCart(){ //獲取資料 userName := this.GetSession("userName") if userName==nil{ beego.Error("使用者未登入") this.Redirect("/", 302) return } //從redis獲取資料 conn,err := redis.Dial("tcp", "192.168.123.123:6379") if err!=nil{ } defer conn.Close() //讀取資料 resp,err := conn.Do("hgetall","cart_"+userName.(string)) cartMap,err := redis.IntMap(resp,err) o := orm.NewOrm() //遍歷一下map var goods []map[string]interface{} for goodsId,value:= range cartMap{ temp := make(map[string]interface{}) id,err := strconv.Atoi(goodsId) var goodsSKU models.GoodsSKU goodsSKU.Id=id o.Read(&goodsSKU) temp['goodsSku']=goodsSku temp['count']=value } }