Django 14购物商城项目(购物车商品选中逻辑)
程序员文章站
2022-05-22 19:55:28
...
Django 13购物商城项目
1、单个商品选中逻辑
(代码在【Django 14购物商城项目】中已经有)
1.1、最终效果展示
1.2、html
没有点击事件时,获取前端出过来的参数C_is_select判断
1.3、js
点击事件发生时,点击时通过异步请求/axf/changecart/到views里面的changecart获得和传输选中状态的数据
1.4、views.py
收到js发过来的异步请求后,进行数据处理
记得这一步是要判断用户登录状态的,所以记得在中间件里面加入changecart的路由
路由
2、购物车全选
2.1、逻辑
2.2、html
2.3、css
这里新加了all_select,修改了css里的好几处,下面是cart.css里目前所有内容
/*底部图标*/
footer .cart span {
background: url(/static/img/cart_selected.png) no-repeat;
background-size: 0.513889rem;
}
footer .cart dd {
color: orange;
}
/*内容*/
#cart {
padding: 1.5rem 0;
z-index: +15;
width: 100%;
background: #fafafa;
overflow: hidden;
}
h3 {
text-align: center;
position: fixed;
width: 100%;
border-bottom: 0.04rem solid lightgray;
line-height: 1.5rem;
background: yellow;
top: 0;
z-index: +100;
}
.full > section {
background: lightpink;
}
.full > section > ul {
border: 0.2rem dashed lightgreen;
border-width: 0.1rem 0;
margin-bottom: 0.2rem;
}
.full > section > ul > li {
padding-left: 0.3rem;
line-height: 0.8rem;
font-size: 0.375rem;
}
.clear:after {
content: "";
display: block;
visibility: hidden;
clear: both;
height: 0;
}
.full > section > ul > li > div > p:last-child {
float: right;
width: 78%;
}
.full > section > ul > li > div > p:last-child > span {
padding: 0.15rem;
}
.infoJustify {
float: left;
width: 20%;
height: 0.8rem;
overflow: hidden;
text-align: justify;
}
.infoJustify b {
display: inline-block;
}
.change {
float: right;
padding-right: 0.2rem;
}
/*闪送超市*/
.bill {
line-height: 0.75rem;
position: relative;
border-bottom: 0.04rem solid lightgreen;
}
.bill > p {
padding: 0 0.3rem;
font-size: 0.3rem;
color: #d35a00;
}
.bill > p:first-child:before {
width: 0.2rem;
height: 0.3rem;
background: yellow;
content: ".";
color: yellow;
margin-right: 0.2rem;
}
.bill > a {
border: 0.05rem solid orangered;
position: absolute;
border-radius: 0.3rem;
padding: 0 0.3rem;
font-size: 0.35rem;
line-height: 0.65rem;
top: 0.2rem;
right: 0.5rem;
}
/*收货时间,收货备注*/
.delivery {
line-height: 1.5rem;
border-bottom: 0.04rem solid lightgray;
font-size: 0.4rem;
padding: 0 0.3rem;
}
.delivery > span:first-child {
padding-right: 0.3rem;
}
.delivery > span:nth-child(2) {
color: orangered;
}
.delivery > span:last-child {
float: right;
}
.delivery > input {
height: 0.8rem;
line-height: 0.8rem;
border-radius: 0.1rem;
border-width: 0.04rem;
width: 70%;
}
.menuList {
border-bottom: 0.04rem solid lightgray;
height: 2.5rem;
position: relative;
}
.confirm , .all_select{
padding: 0.95rem 0;
width: 15%;
height: 2.5rem;
display: inline-block;
box-sizing: border-box;
text-align: center;
float: left;
}
.confirm > span , .all_select> span{
box-sizing: border-box;
border: 0.04rem solid orange;
background: white;
display: inline-block;
width: 0.6rem;
height: 0.6rem;
overflow: hidden;
border-radius: 50%;
line-height: 0.6rem;
}
.confirm > span > span ,.all_select> span > span{
background: yellow;
font-size: 0.5rem;
display: block;
}
.menuList > a {
width: 84%;
display: inline-block;
font-size: 0.4rem;
line-height: 1rem;
}
.menuList > a > img {
margin-top: 0.25rem;
width: 25%;
height: 100%;
float: left;
}
.menuList > a > p {
width: 70%;
height: 1rem;
float: right;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
/*去除a标签的连接蓝色属性 text-decoration设置文本修饰属性*/
text-decoration: none;
color: #333;
}
.menuList > section {
position: absolute;
right: 0.4rem;
bottom: 0.4rem;
height: 0.75rem;
border-radius: 1rem;
}
.menuList > section > button {
background: white;
border: 1px solid orange;
border-radius: 1111px;
color: red;
display: inline-block;
text-align: center;
line-height: 0.65rem;
font-weight: 900;
width: 0.75rem;
height: 0.75rem;
}
.menuList > section > span {
display: inline-block;
width: 0.5rem;
text-align: center;
line-height: 0.5rem;
font-size: 0.4rem;
}
.presenPrice:before {
content: "¥";
font-size: 0.33rem;
}
/*下单,付钱*/
.payTheBill {
height: 1.5rem;
position: relative;
}
.payTheBill .all_select {
width: 10%;
padding-top: 0.4rem;
padding-left: 0.4rem;
}
.payTheBill > p {
line-height: 1.5rem;
text-indent: 0.3rem;
}
.payTheBill > p > span:first-child {
padding-right: 0.8rem;
}
.payTheBill > p > span:last-child {
padding-left: 0.3rem;
color: red;
}
.payTheBill > p > span:last-child:before {
content: "¥";
font-size: 0.35rem;
}
.payTheBill > a {
padding: 0 0.7rem;
line-height: 1.5rem;
background: yellow;
position: absolute;
right: 0;
top: 0;
}
部分修改处截图:
2.4、js
第一处添加(红色部分为添加的)
if (data["is_all_select"]) {
$(".all_select").find("span").find("span").html("√");
} else {
$(".all_select").find("span").find("span").html("");
}
第二处添加(红色部分为添加的)
$(".all_select").click(function () {
var $all_select = $(this);
var select_list = [];
var unselect_list = [];
$(".confirm").each(function () {
var $confirm = $(this);
var cartid = $confirm.parents("li").attr("cartid");
if ($confirm.find("span").find("span").html().trim()) {
select_list.push(cartid);
} else {
unselect_list.push(cartid);
}
});
if (unselect_list.length > 0) {
$.getJSON("/axf/allselect", {"cart_list": unselect_list.join("#")}, function (data) {
console.log(data);
if (data["status"] === 200) {
// 所有的商品变成选中状态
$(".confirm").find("span").find("span").html('√');
// ”全选“变成选中状态
$all_select.find("span").find("span").html('√');
}
});
} else {
if (select_list.length > 0) {
$.getJSON("/axf/allselect", {"cart_list": select_list.join("#")}, function (data) {
console.log(data);
if (data["status"] === 200) {
// 所有的商品变成选中状态
$(".confirm").find("span").find("span").html('');
// ”全选“变成选中状态
$all_select.find("span").find("span").html('');
}
});
}
}
});
2.5、视图
1、(红色部分为添加的)
2、(红色部分为添加的)
def all_select(request):
cart_list = request.GET.get('cart_list')
cart_list = cart_list.split("#")
carts = Cart.objects.filter(id__in=cart_list)
for cart in carts:
cart.C_is_select = not cart.C_is_select
cart.save()
print(cart_list)
data = {
"status": 200,
}
return JsonResponse(data=data)
2.6、路由
下一篇: 基于QT实现Mp3播放器(代码部分)