版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33781658/article/details/86253422
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
}
}