[英]Price filter (min and max price) with a dropdown menu asp.net core
I'm trying to filter through prices using javascript and asp.net core, and I've come up with a heap of javascript which doesn't really work. 我正在尝试使用javascript和asp.net核心过滤价格,并且我提出了一堆实际上无法正常工作的javascript。 There has to be an easier way, using jquery or with c#?
必须有一个更简单的方法,使用jquery或c#?
If anyone has any suggestions it would be greatly appreciated! 如果有人有任何建议,将不胜感激!
Price Filter
<p>Sort By Price: </p>
<select id="sort" onChange="OnSelectedIndexChange()">
<option value="all">All</option>
<option value="1">$0 - $50</option>
<option value="2">$51 - $100</option>
<option value="3">$101 - $150</option>
<option value="4">$151 + </option>
</select>
<div class="row">
@{
foreach (var item in Model.Products)
{
if (item.CategoryId == Model.CategoryId)
{
<div class="card h-100" id="hamper">
<p id="price">$@item.Price</p>
</div>
}
}
}
</div>
</div>
<!-- Javascript -->
<script type="text/javascript">
var prices = [];
@foreach(var item in Model.Products)
{
@:prices.push(@item.Price)
}
function OnSelectedIndexChange() {
if (document.getElementById('sort').value == "all") {
document.getElementById("hamper").style.display = "block";
} else if (document.getElementById('sort').value == "1") {
for (var i = 0; i < prices.length; i++) {
if (prices[i] >= 0 && prices[i] <= 50 ) {
document.getElementById("hamper").style.display = "block";
} else {
document.getElementById("hamper").style.display = "none";
}
}
} else if (document.getElementById('sort').value == "2") {
for (var i = 0; i < prices.length; i++) {
if (prices[i] >= 51 && prices[i] <= 100) {
document.getElementById("hamper").style.display = "block";
} else {
document.getElementById("hamper").style.display = "none";
}
}
} else if (document.getElementById('sort').value == "3") {
for (var i = 0; i < prices.length; i++) {
if (prices[i] >= 101 && prices[i] <= 150) {
document.getElementById("hamper").style.display = "block";
} else {
document.getElementById("hamper").style.display = "none";
}
}
} else if (document.getElementById('sort').value == "4") {
for (var i = 0; i < prices.length; i++) {
if (prices[i] >= 150) {
document.getElementById("hamper").style.display = "block";
} else {
document.getElementById("hamper").style.display = "none";
}
}
}
}
</script>
There are multiple ways to handle it, in both client side and server side. 客户端和服务器端都有多种处理方法。
Here is a quick sample to do the filtering on client side. 这是在客户端进行过滤的快速示例。
First, add a data attribute to each option in the select to represent the corresponding price range. 首先,将数据属性添加到选择中的每个选项以代表相应的价格范围。 You may keep the value in this format
"lowerRange:upperRange"
您可以将值保留为
"lowerRange:upperRange"
格式
<select id="sort">
<option data-price="0:@Int32.MaxValue" value="all">All</option>
<option data-price="0:50" value="1">$0 - $50</option>
<option data-price="51:100" value="2">$51 - $100</option>
<option data-price="101:1000" value="3">$101 - 1000</option>
<option data-price="1001:@Int32.MaxValue" value="4">1001 + </option>
</select>
Now when you render each, card, give it a data attribute for storing the price ( data-productprice
) 现在,当渲染每张卡片时,为其提供一个用于存储价格的data属性(
data-productprice
)
<div class="row" id="my-products">
@foreach (var item in Model.Products.Where(a=>CategoryId==Model.CategoryId))
{
<div class="card h-100" data-productprice="@item.Price">
<span>@item.Name</span>
<span>@item.Price</span>
</div>
}
</div>
Now when user make a selection in the sort dropdown, get the data attribute of the selected option, read the lower and upper range, filter the card div's which has a productprice data attribute value which is falling in that range. 现在,当用户在排序下拉列表中进行选择时,获取所选选项的数据属性,读取上下限范围,过滤具有产品价格数据属性值在该范围内的卡片div。
$(function () {
var $cards = $("#my-products").find("[data-productprice]");
$("#sort").change(function () {
var t = $(this).find(':selected').data('price');
var a = t.split(':');
var l = parseFloat(a[0]);
var u = parseFloat(a[1]);
$.each($cards, function (a, b) {
var p = parseFloat($(b).data("productprice"));
if (p >= l && p <= u) {
$(b).show();
} else {
$(b).hide();
}
});
});
});
As i mentioned, this is just one way of doing it, you can do the filtering on server side as well (which is something i might prefer).In that approach, you make an ajax call in the change event where you will send the lower and upper range ( l
and u
) and let the server do a filtering based on the price range and return the partial view markup for only those items. 正如我提到的那样,这只是一种实现方式,您也可以在服务器端进行过滤(我可能更喜欢这种方式)。在这种方法中,您在change事件中进行了ajax调用,您将在其中发送上下限范围(
l
和u
),并让服务器根据价格范围进行过滤,并仅返回这些商品的部分视图标记。 When the ajax response comes back, you will replace the HTML of the my-products div. 当ajax响应返回时,您将替换my-products div的HTML。
$(function () {
$("#sort").change(function () {
var t = $(this).find(':selected').data('price');
var a = t.split(':');
var l = parseFloat(a[0]);
var u = parseFloat(a[1]);
var urlForFilteredResults = "/Products/List?priceFrom="+l+"&priceTo="+r;
// Add other filter criteria as needed
$("#my-products").load(urlForFilteredResults);
});
});
Assuming your List
action method accepts the params and return a partial view result 假设您的
List
操作方法接受参数并返回部分视图结果
I see in your code you are setting the same id value ( id="hamper"
) for all the cards inside the loop. 我在您的代码中看到,您正在为循环内的所有卡设置相同的id值(
id="hamper"
)。 That is invalid HTML. 那是无效的HTML。 Your Id's should be unique in a document.
您的ID在文档中应该是唯一的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.