我对MVC5中的排序价格脚本有疑问
这是一个视图
@model IEnumerable<WebApplication.Models.Product>
@foreach (var Model in Model){
<div class="col-md-3">
<div class="product-men">
<div class="men-pro-item simpleCart_shelfItem">
<div class="men-thumb-item">
<img src="~/Images/@Model.Product_Image" alt="" class="pro-image-front">
<img src="~/Images/@Model.Product_Image" alt="" class="pro-image-back">
<div class="men-cart-pro">
<div class="inner-men-cart-pro">
<a href="single.html" class="link-product-add-cart">Quick View</a>
</div>
</div>
</div>
<div class="item-info-product">
<h4><a href="">@Model.Product_Name</a></h4>
<div class="info-product-price">
<span class="item_price price">$@Model.Product_Discount</span>
@if (Model.Product_Discount == Model.Product_Price)
{
}
else
{
<del>$@Model.Product_Price</del>}
</div>
<div class="snipcart-details top_brand_home_details item_add single-item hvr-outline-out button2">
<form action="#" method="post">
<fieldset>
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="add" value="1" />
<input type="hidden" name="business" value=" " />
<input type="hidden" name="item_name" value="Formal Blue Shirt" />
<input type="hidden" name="amount" value="30.99" />
<input type="hidden" name="discount_amount" value="1.00" />
<input type="hidden" name="currency_code" value="USD" />
<input type="hidden" name="return" value=" " />
<input type="hidden" name="cancel_return" value=" " />
<input type="submit" name="submit" value="Add to cart" class="button" />
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>}
段:
var ascending = false;
$('.tab-content').on('click', '.sortByPrice', function() {
var sorted = $('.men-pro-item').sort(function(a, b) {
return (ascending ==
(convertToNumber($(a).find('.price').html()) <
convertToNumber($(b).find('.price').html()))) ? 1 : -1;
});
ascending = ascending ? false : true;
$('.product-men').html(sorted);
});
var convertToNumber = function(value) {
return parseFloat(value.replace('$', ''));
}
&#13;
<!-- Need to add the html data before click, here -->
<!-- Button click sort -->
<div class="tab-content">
<h6>Sort By</h6>
<div id="filters">
<p>
<a class="sortByPrice" href="#">Sort by Price</a>
</p>
</div>
<div class="clearfix"></div>
</div>
&#13;
这个问题当我点击按价格排序时,这个javascript工作得很好,但重复数据。因为foreach。但我不能删除这个foreach。如果有,请帮我修复此代码或新代码。不是表
答案 0 :(得分:0)
我玩了你的代码,终于找到了斯蒂芬在他的评论中谈论的相同解决方案(我在完成后就看到了这一点)。
这是一个工作片段,我在其中添加了一些值来模拟它:
var ascending = false;
$('.tab-content').on('click', '.sortByPrice', function() {
var sorted = $('.men-pro-item').sort(function(a, b) {
return (ascending ==
(convertToNumber($(a).find('.price').html()) <
convertToNumber($(b).find('.price').html()))) ? 1 : -1;
});
ascending = ascending ? false : true;
$('#products').html(sorted); // Modified
});
var convertToNumber = function(value) {
return parseFloat(value.replace('$', ''));
}
h4 {
margin: 0;
}
.item-info-product * {
display: inline-block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="products"><!-- Added -->
<div class="col-md-3">
<div class="product-men">
<div class="men-pro-item simpleCart_shelfItem">
<div class="item-info-product">
<h4><a href="">Product_Name</a></h4>
<div class="info-product-price">
<span class="item_price price">$30.00</span>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-3">
<div class="product-men">
<div class="men-pro-item simpleCart_shelfItem">
<div class="item-info-product">
<h4><a href="">Product_Name</a></h4>
<div class="info-product-price">
<span class="item_price price">$25.00</span>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-3">
<div class="product-men">
<div class="men-pro-item simpleCart_shelfItem">
<div class="item-info-product">
<h4><a href="">Product_Name</a></h4>
<div class="info-product-price">
<span class="item_price price">$35.00</span>
</div>
</div>
</div>
</div>
</div>
</div> <!-- Added -->
<!-- Button click sort -->
<div class="tab-content">
<h6>Sort By:</h6>
<div id="filters">
<p><a class="sortByPrice" href="#">Price</a></p>
</div>
<div class="clearfix"></div>
</div>
我希望它有所帮助。