最小库存单元

<h2>Detail</h2>

<table>
<tr>
<td>
<img id="img1" src="" alt="" width="300" height="400"/>
</td>
<td>
<table>
<tr>
<td id="td1"> </td>
</tr>
<tr>
<td id="td2"> </td>
</tr>
<tr>
<td id="td3"> </td>
</tr>
<tr>
<td id="td4">

</td>
</tr>
</table>
</td>
</tr>
</table>

 

<script>
var sku = [];
var OldPrice = "";
var currentSku = {};

function load() {
var id = @ViewBag.Id;
$.ajax({
url: 'http://localhost:56334/api/Default/GetONe/'+id,
type: 'get',
dataType: 'json',
success: function (data) {
$("#img1").attr("src", data.Imgs[0].ImageUrl);
$("#td1").text(data.Goods.Name);
$("#td2").text(data.Goods.Price);
OldPrice = data.Goods.Price;
sku = data.Sku;

$(data.Attr).each(function () {
$("#td3").append( this.AName +":"+this.Value +"<br/>");
});


$(data.SKUAttr).each(function () {
var str = "";
var check = "checked";
var values = $(this.AValue).each(function () {
str += "<input " + check + " type='radio' name='AA" + this.AttributeId + "' value='" + this.Value + "' onclick='changePrice()'> " + this.Value
check = "";
});
$("#td4").append(
' <div>'+ this.AName+
' :'+ str+
'</div>')
});
}
});


}
load();

function changePrice() {
var price = "";

var s = [];
$(":radio:checked").each(function () {
s.push( $(this).val());
});

$(sku).each(function () {
var sign = 1;
for (var i = 0; i < s.length; i++) {
if (this.AttrStr.indexOf(s[i]) == -1) {
sign = 0;
}
}
if (sign == 1) {
price = this.Price + "";
currentSku = this;
}
});

if (price != "")
$("#td2").text(price);
else
$("#td2").text(OldPrice);
}
</script>

----------------------------------------------------------------------------------------------

<h2>Index</h2>

<div id="dv">
<table style="float:left">
<tr>
<td>
<img src="" width="80" height="120" />
</td>
<tr />

<tr>

<td>
<span></span>
<span></span>
</td>
</tr>
</table>
</div>
<div style="clear:both"></div>

<script>
function load()
{
$.ajax({
url: 'http://localhost:56334/api/Default/GetAll',
type: 'get',
dataType: 'json',
success: function (data) {
$("#dv").empty();
$(data).each(function () {
$("#dv").append(
'<table style="float:left">'+
' <tr>'+
' <td>'+
' <a href="/Default/Detail/'+this.Goods.Id+'"> <img src="'+this.Img+'" width="80" height="120" /></a>'+
' </td></tr><tr>'+
' <td>'+
' <span>'+this.Goods.Name+'</span>'+
' <span>'+this.Goods.Price+'</span>'+
' </td>'+
' </tr>'+
'</table>'
);
});
}
});
}
load();
</script>

--------------------------------------------------------------------------------------------------------------------

MyShopDB db = new MyShopDB();
public object GetAll()
{
var list = db.Goods_SPU.ToList();
var data = list.Select(p => new { Goods = p, Img = db.Image.Where(k => k.SPUID == p.Id ).FirstOrDefault().ImageUrl });
return data;
}

public object GetOne(int Id)
{
// 获取商品
Goods_SPU goods = db.Goods_SPU.Find(Id);
// 获取商品的SPU属性
var goods_Attribute = (from S in db.SPUAttribute
join A in db.Attribute
on S.AID equals A.Id
where A.AttributeType == 0
select new
{
Id = S.SPID, // 商品ID
AId = S.AID, // 属性ID
AType = A.InputType ,// 属性的输入类型
AName = A.AttributeName, //属性名
AValue = db.AttributeValue.Where(p => p.AttributeId == S.AID).ToList(),// 属性值列表
Value = S.Value // 实际值
}).ToList();
// 获取商品的SKU属性
var goods_SKU_Attribute = (from S in db.SPUAttribute
join A in db.Attribute
on S.AID equals A.Id
where A.AttributeType == 1
select new
{
Id = S.SPID, // 商品ID
AId = S.AID, // 属性ID
AType = A.InputType,// 属性的输入类型
AName = A.AttributeName, //属性名
AValue = db.AttributeValue.Where(p => p.AttributeId == S.AID).ToList(),// 属性值列表
Value = S.Value // 实际值
}).ToList();

var imgs = db.Image.Where(p => p.SPUID == Id).ToList();
var sku = db.Goods_SKU.Where(p => p.SPUID == Id).ToList();

return new {
Goods=goods, // 商品本身
Attr=goods_Attribute, // 商品属性
SKUAttr = goods_SKU_Attribute, // 商品库存属性
Imgs =imgs, // 商品图片
Sku=sku // 商品规格列表
};
}

-------------------------------------------------

posted @ 2020-07-26 19:45  G蠢蠢  阅读(112)  评论(0编辑  收藏  举报