ASP.NET 核心 MVC 在产品页面详细信息上应用同位素过滤器
ASP.NET Core MVC apply isotope filter on product page details
我在我的 .Net 核心项目上动态应用了同位素过滤器。我成功地做到了,但发生了一个小错误。选择产品的特定类别位置后,不会排在第一位。下面看我的两张图
All Product
After Filter check the position of that image not changed(its need to come first)
我的代码如下
Index.cshtml
@model BornoMala.Models.ViewModels.ProductDetailsVM
<section id="portfolio" class="portfolio">
<div class="container">
<div class="row" data-aos="fade-up">
<div class="col-lg-12 d-flex justify-content-center">
<ul id="portfolio-flters">
<li data-filter="all" class="filter-active filter-button">All</li>
@foreach (var obj in Model.Categories)
{
<li class="filter-button" data-filter="@obj.Name.Replace(' ','_')">@obj.Name</li>
}
</ul>
</div>
</div>
<div class="row portfolio-container" data-aos="fade-up">
@foreach (var prod in Model.Products)
{
<div class="col-lg-4 col-md-6 portfolio-item filter @Model.Category.Name.Replace('
','_')">
<div class="portfolio-img"><img src="@Model.ImageUrl" class="img-fluid" alt=""></div>
<div class="portfolio-info">
<h4>@Model.Title</h4>
<p>By <b>@Model.Title</b></p>
</div>
</div>
}
</div>
</div>
</section>
Isotope.js
$(window).on('load', function () {
var portfolioIsotope = $('.portfolio-container').isotope({
itemSelector: '.portfolio-item',
});
});
$(document).ready(function () {
$(".filter-button").click(function () {
$("#portfolio-flters li").removeClass('filter-active');
$(this).addClass('filter-active');
var value = $(this).attr('data-filter');
if (value == "all") {
$('.portfolio-item').show('3000');
}
else {
$(".portfolio-item").not('.' + value).hide('2000');
$('.portfolio-item').filter('.' + value).show('2000');
}
});
});
如果我从 isotope.js 中删除 ..portfolio-container class on load window 然后详细信息页面容器不灵活检查我的另一个图像(但过滤器工作正常)..
Image card not flexible
有两种方法:
1,只将window的onload部分改成:
$('.portfolio-container').isotope({
itemSelector: '.element-item'
});
然后它会显示给第一个,但是在我的演示中移动需要一些时间。
2、尝试另一种使用同位素的方法:
注意'portfolio-flters'中的data-filter内容,可能和你的不一样。这条路
原理是当你点击不同的过滤器按钮时每次调用同位素
(不同的数据过滤器)。
编辑代码:
Mind data-filter=".@obj.Name.Replace(' ','_')">@obj.Name</li>
数据库中存储的数据
是数字,但您可以添加'.'在'@'之前,所以可以同位素识别。
<div class="container">
<div class="row" data-aos="fade-up">
<div class="col-lg-12 d-flex justify-content-center">
<ul id="portfolio-flters">
<li data-filter="*" class="filter-active filter-button">All</li>
@foreach (var obj in Model.Categories)
{
<li class="filter-button" data-filter=".@obj.Name.Replace(' ','_')">@obj.Name</li>
}
</ul>
</div>
</div>
<div class="row portfolio-container" data-aos="fade-up">
@foreach (var product in Model.Products)
{
<partial name="_IndividualProductCard" model="product" />
}
</div>
@section Scripts
{
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.js"></script>
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.min.js"></script>
<script>
$mygrid = $('.portfolio-container').isotope({
itemSelector: '.portfolio-item'
});
$('#portfolio-flters').on('click', 'li', function () {
var filterValue = $(this).attr('data-filter');
$mygrid.isotope({ filter: filterValue });
});
</script>
}
数据库中的数据:
结果:
@Tisa
我按照你说的那样遵循你的代码
Index.cshtml
<section class="portfolio">
<div class="container">
<div class="row" data-aos="fade-up">
<div class="col-lg-12 d-flex justify-content-center">
<ul id="portfolio-flters">
<li data-filter="*" class="filter-active filter-button">All</li>
@foreach (var obj in Model.Categories)
{
<li class="filter-button" data-filter="@obj.Name.Replace('
','_')">@obj.Name</li>
}
</ul>
</div>
</div>
<div class="row portfolio-container" data-aos="fade-up">
@foreach (var product in Model.Products)
{
<partial name="_IndividualProductCard" model="product" />
}
</div>
</div>
</section>
_IndividualProductCard.cshtml
@model BornoMala.Models.Product
<div class="col-lg-4 col-md-6 portfolio-item filter @Model.Category.Name.Replace(' ','_')">
<div class="portfolio-img ">
<img src="@Model.ImageUrl" width="100" height="50" />
</div>
</div>
@section Scripts
{
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.js"></script>
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.min.js"></script>
<script>
$mygrid = $('.portfolio-container').isotope({
itemSelector: '.portfolio-item'
});
$('#portfolio-flters').on('click', 'li', function () {
var filterValue = $(this).attr('data-filter');
$mygrid.isotope({ filter: filterValue });
});
</script>
}
产品型号
namespace project.Models
{
public class Product
{
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
[Required]
public string Artist { get; set; }
[Required]
[Range(1,1000)]
public double Price { get; set; }
public string ImageUrl { get; set; }
[Required]
public int CategoryId { get; set; }
[ForeignKey("CategoryId")]
public Category Category { get; set; }
[Required]
public int StyleId { get; set; }
[ForeignKey("StyleId")]
public Style Style { get; set; }
}
}
productdetailsVM
namespace project.Models.ViewModels
{
public class ProductDetailsVM
{
public IEnumerable<Product> Products { get; set; }
public IEnumerable<Category> Categories { get; set; }
}
}
但是所有类别图片显示其他没有图片显示
blank image
我已经用 jquery 修复了过滤,但问题是图片位置没有改变,因为“$mygrid”没有调用所以如何调用那个
@section Scripts{
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.js"></script>
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.min.js"></script>
<script>
$mygrid = $('.portfolio-container').isotope({
itemSelector: '.portfolio-item'
});
$(".filter-button").click(function(){
var value = $(this).attr('data-filter');
if(value == "all")
{
$('.filter').show('0010');
}
else
{
$(".filter").not('.'+value).hide('0010');
$('.filter').filter('.'+value).show('0010');
}
});
</script>
我在我的 .Net 核心项目上动态应用了同位素过滤器。我成功地做到了,但发生了一个小错误。选择产品的特定类别位置后,不会排在第一位。下面看我的两张图
All Product
After Filter check the position of that image not changed(its need to come first)
我的代码如下 Index.cshtml
@model BornoMala.Models.ViewModels.ProductDetailsVM
<section id="portfolio" class="portfolio">
<div class="container">
<div class="row" data-aos="fade-up">
<div class="col-lg-12 d-flex justify-content-center">
<ul id="portfolio-flters">
<li data-filter="all" class="filter-active filter-button">All</li>
@foreach (var obj in Model.Categories)
{
<li class="filter-button" data-filter="@obj.Name.Replace(' ','_')">@obj.Name</li>
}
</ul>
</div>
</div>
<div class="row portfolio-container" data-aos="fade-up">
@foreach (var prod in Model.Products)
{
<div class="col-lg-4 col-md-6 portfolio-item filter @Model.Category.Name.Replace('
','_')">
<div class="portfolio-img"><img src="@Model.ImageUrl" class="img-fluid" alt=""></div>
<div class="portfolio-info">
<h4>@Model.Title</h4>
<p>By <b>@Model.Title</b></p>
</div>
</div>
}
</div>
</div>
</section>
Isotope.js
$(window).on('load', function () {
var portfolioIsotope = $('.portfolio-container').isotope({
itemSelector: '.portfolio-item',
});
});
$(document).ready(function () {
$(".filter-button").click(function () {
$("#portfolio-flters li").removeClass('filter-active');
$(this).addClass('filter-active');
var value = $(this).attr('data-filter');
if (value == "all") {
$('.portfolio-item').show('3000');
}
else {
$(".portfolio-item").not('.' + value).hide('2000');
$('.portfolio-item').filter('.' + value).show('2000');
}
});
});
如果我从 isotope.js 中删除 ..portfolio-container class on load window 然后详细信息页面容器不灵活检查我的另一个图像(但过滤器工作正常)..
Image card not flexible
有两种方法:
1,只将window的onload部分改成:
$('.portfolio-container').isotope({
itemSelector: '.element-item'
});
然后它会显示给第一个,但是在我的演示中移动需要一些时间。
2、尝试另一种使用同位素的方法:
注意'portfolio-flters'中的data-filter内容,可能和你的不一样。这条路
原理是当你点击不同的过滤器按钮时每次调用同位素
(不同的数据过滤器)。
编辑代码:
Mind data-filter=".@obj.Name.Replace(' ','_')">@obj.Name</li>
数据库中存储的数据
是数字,但您可以添加'.'在'@'之前,所以可以同位素识别。
<div class="container">
<div class="row" data-aos="fade-up">
<div class="col-lg-12 d-flex justify-content-center">
<ul id="portfolio-flters">
<li data-filter="*" class="filter-active filter-button">All</li>
@foreach (var obj in Model.Categories)
{
<li class="filter-button" data-filter=".@obj.Name.Replace(' ','_')">@obj.Name</li>
}
</ul>
</div>
</div>
<div class="row portfolio-container" data-aos="fade-up">
@foreach (var product in Model.Products)
{
<partial name="_IndividualProductCard" model="product" />
}
</div>
@section Scripts
{
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.js"></script>
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.min.js"></script>
<script>
$mygrid = $('.portfolio-container').isotope({
itemSelector: '.portfolio-item'
});
$('#portfolio-flters').on('click', 'li', function () {
var filterValue = $(this).attr('data-filter');
$mygrid.isotope({ filter: filterValue });
});
</script>
}
数据库中的数据:
结果:
@Tisa 我按照你说的那样遵循你的代码 Index.cshtml
<section class="portfolio">
<div class="container">
<div class="row" data-aos="fade-up">
<div class="col-lg-12 d-flex justify-content-center">
<ul id="portfolio-flters">
<li data-filter="*" class="filter-active filter-button">All</li>
@foreach (var obj in Model.Categories)
{
<li class="filter-button" data-filter="@obj.Name.Replace('
','_')">@obj.Name</li>
}
</ul>
</div>
</div>
<div class="row portfolio-container" data-aos="fade-up">
@foreach (var product in Model.Products)
{
<partial name="_IndividualProductCard" model="product" />
}
</div>
</div>
</section>
_IndividualProductCard.cshtml
@model BornoMala.Models.Product
<div class="col-lg-4 col-md-6 portfolio-item filter @Model.Category.Name.Replace(' ','_')">
<div class="portfolio-img ">
<img src="@Model.ImageUrl" width="100" height="50" />
</div>
</div>
@section Scripts
{
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.js"></script>
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.min.js"></script>
<script>
$mygrid = $('.portfolio-container').isotope({
itemSelector: '.portfolio-item'
});
$('#portfolio-flters').on('click', 'li', function () {
var filterValue = $(this).attr('data-filter');
$mygrid.isotope({ filter: filterValue });
});
</script>
}
产品型号
namespace project.Models
{
public class Product
{
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
[Required]
public string Artist { get; set; }
[Required]
[Range(1,1000)]
public double Price { get; set; }
public string ImageUrl { get; set; }
[Required]
public int CategoryId { get; set; }
[ForeignKey("CategoryId")]
public Category Category { get; set; }
[Required]
public int StyleId { get; set; }
[ForeignKey("StyleId")]
public Style Style { get; set; }
}
}
productdetailsVM
namespace project.Models.ViewModels
{
public class ProductDetailsVM
{
public IEnumerable<Product> Products { get; set; }
public IEnumerable<Category> Categories { get; set; }
}
}
但是所有类别图片显示其他没有图片显示 blank image
我已经用 jquery 修复了过滤,但问题是图片位置没有改变,因为“$mygrid”没有调用所以如何调用那个
@section Scripts{
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.js"></script>
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.min.js"></script>
<script>
$mygrid = $('.portfolio-container').isotope({
itemSelector: '.portfolio-item'
});
$(".filter-button").click(function(){
var value = $(this).attr('data-filter');
if(value == "all")
{
$('.filter').show('0010');
}
else
{
$(".filter").not('.'+value).hide('0010');
$('.filter').filter('.'+value).show('0010');
}
});
</script>