Reputation: 51
I have a problem with the script of sort price in MVC5
This is a View
@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">[email protected]_Discount</span>
@if (Model.Product_Discount == Model.Product_Price)
{
}
else
{
<del>[email protected]_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>}
Snippet:
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('$', ''));
}
<!-- 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>
This problem when I clicked Sort by Price, this javascript work well, but duplicate data. Because of foreach. But I cannot remove this foreach. Help me fix this code or new code if you have. Not table
Upvotes: 2
Views: 1566
Reputation: 10081
I played with your code and finally came to the same solution as Stephen was talking about in his comments (I saw that right after I finished).
Here is a working snippet where I added some values to simulate it:
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>
I hope it helps.
Upvotes: 2