英文:
Isotope custom function can't access HTML attribut
问题
我正在创建一个网站来管理我的桌游俱乐部,目前正在制作一个页面来展示我们为会员提供的所有游戏。
在这个页面上,有一个带有筛选按钮的部分,当点击时,将根据用户的选择显示或隐藏游戏。
目前有游戏类型的按钮,游戏语言的按钮,我正在制作根据最少或最多玩家数量进行筛选的按钮。
我正在使用Isotope来处理这些筛选,在文档中,如果你想使用一个自定义函数,可以将其创建为一个Javascript对象。
以下是我的当前Javascript代码:
$(window).on('load', function(){
var filtersFunctions = {
minplayers: function(){
var itemValue = $(this).attr('data-minplayers');
var filterValue = $('.isotope-filter-minplayers.active').attr('data-players');
return parseInt(itemValue, 10) >= parseInt(filterValue, 10);
},
maxplayers: function(){
var itemValue = $(this).attr('data-maxplayers');
var filterValue = $('.isotope-filter-maxplayers.active').attr('data-players');
return parseInt(itemValue, 10) <= parseInt(filterValue, 10);
}
};
var filters = {};
var $grid = $('.isotope-grid').isotope({
itemSelector: '.isotope-card',
layoutMode: 'masonry'
});
$('#isotope-filters').on('click', '.isotope-filter', function(){
var $this = $(this);
var $buttonGroup = $this.parents('.isotope-group');
var filterGroup = $buttonGroup.attr('data-filter-group');
if($this.hasClass('active')){
delete filters[filterGroup];
$this.removeClass('active');
}
else{
$buttonGroup.find('.isotope-filter').removeClass('active');
$this.addClass('active');
filters[filterGroup] = $this.attr('data-filter');
}
var filterValue = filtersFunctions[filters[filterGroup]] || concatValues(filters);
$grid.isotope({filter: filterValue});
});
});
function concatValues(obj){
var value = '';
for(var prop in obj){
value += obj[prop];
}
return value;
}
现在是我的问题所在。
当点击按钮时,筛选工作正常,如果我点击最少或最多玩家筛选按钮之一,我知道自定义函数(minplayers: function(){}
或maxplayers: function(){}
)被调用并使用。
这就是问题所在。尽管函数被调用,但我的代码似乎无法获取HTML属性的值($(this).attr('data-minplayers')
或$(this).attr('data-maxplayers')
),这些属性可以让我知道游戏的最少或最多玩家值。
每次执行我的函数并console.log(itemValue)
时,在控制台中显示给我的值是undefined
。
我使用了Isotope文档,甚至尝试使用了他们的示例,但对我来说从未奏效。我不得不从头开始做。
以下是我的视图中筛选按钮的示例:
<div id="isotope-filters">
<div class="card-group shadow">
<div class="card">
<div class="card-body isotope-group" data-filter-group="types">
<h5 class="card-title">Types de jeux</h5>
<button type="button" class="btn btn-outline-primary btn-sm isotope-filter" data-filter=".type-null">
Jeux sans types
</button>
<!-- 其他按钮... -->
</div>
</div>
<!-- 其他卡片组... -->
</div>
</div>
以下是我的游戏卡片的示例:
<div class="row isotope-grid">
<div class="isotope-card lang-fr type-1" data-minplayers="2" data-maxplayers="5">
<!-- 游戏卡片内容... -->
</div>
<div class="isotope-card lang-en type-2" data-minplayers="3" data-maxplayers="4">
<!-- 游戏卡片内容... -->
</div>
<!-- 其他游戏卡片... -->
</div>
我应该怎么做才能使我的函数在执行时使用游戏卡片中的HTML属性?
感谢您的时间和耐心。我希望我表达得足够清楚和容易理解。
最诚挚的问候,
SatanicGeek
- Laravel 10.8
- Bootstrap 5.3
- jQuery 3.7.0
- Isotope-layout 3.0.6
- Masonry-layout 4.2.2
英文:
I'm in the process of creating a website to manage my board games club and I'm currently working on a page displaying all the games we have available for our members.
On this page, there's a section with filter buttons which, when clicked, will display or hide games according to the user's choice.
Currently, there are buttons for game types, buttons for game languages, and I'm working on buttons for filtering by minimum or maximum number of players.
I'm using Isotope to process the filters and, in the documentation, if you want to use a custom function, you can create it as a Javascript object.
Here's my current Javascript :
$(window).on('load', function(){
var filtersFunctions = {
minplayers: function(){
var itemValue = $(this).attr('data-minplayers');
var filterValue = $('.isotope-filter-minplayers.active').attr('data-players');
return parseInt(itemValue, 10) >= parseInt(filterValue, 10);
},
maxplayers: function(){
var itemValue = $(this).attr('data-maxplayers');
var filterValue = $('.isotope-filter-maxplayers.active').attr('data-players');
return parseInt(itemValue, 10) <= parseInt(filterValue, 10);
}
};
var filters = {};
var $grid = $('.isotope-grid').isotope({
itemSelector: '.isotope-card',
layoutMode: 'masonry'
});
$('#isotope-filters').on('click', '.isotope-filter', function(){
var $this = $(this);
var $buttonGroup = $this.parents('.isotope-group');
var filterGroup = $buttonGroup.attr('data-filter-group');
if($this.hasClass('active')){
delete filters[filterGroup];
$this.removeClass('active');
}
else{
$buttonGroup.find('.isotope-filter').removeClass('active');
$this.addClass('active');
filters[filterGroup] = $this.attr('data-filter');
}
var filterValue = filtersFunctions[filters[filterGroup]] || concatValues(filters);
$grid.isotope({filter: filterValue});
});
});
function concatValues(obj){
var value = '';
for(var prop in obj){
value += obj[prop];
}
return value;
}
Now here's my issue.
When a button is clicked, the filtering is done properly and if I click on one of the minimum or maximum player filter buttons, I know that the custom function (minplayers: function(){}
or maxplayers: function(){}
) is called and used.
And that's where the problem lies. Although the function is called, it seems impossible for my code to retrieve the value of the HTML attribute ($(this).attr('data-minplayers')
or $(this).attr('data-maxplayers')
) that lets me know what the minimum or maximum player value is for a game.
Each time I execute my function and console.log(itemValue)
, the value showed to me in the console is undefined
.
I used the Isotope documentation and even tried to use their example but it never worked out for me. I had to do it all from scratch.
Here is an example of filter buttons in my view :
<div id="isotope-filters">
<div class="card-group shadow">
<div class="card">
<div class="card-body isotope-group" data-filter-group="types">
<h5 class="card-title">Types de jeux</h5>
<button type="button" class="btn btn-outline-primary btn-sm isotope-filter" data-filter=".type-null">
Jeux sans types
</button>
<button type="button" class="btn btn-outline-primary btn-sm isotope-filter" data-filter=".type-1">
Type 1
</button>
<button type="button" class="btn btn-outline-primary btn-sm isotope-filter" data-filter=".type-2">
Type 2
</button>
<button type="button" class="btn btn-outline-primary btn-sm isotope-filter" data-filter=".type-3">
Type 3
</button>
</div>
</div>
<div class="card">
<div class="card-body isotope-group" data-filter-group="langs">
<h5 class="card-title">Langues</h5>
<button type="button" class="btn btn-outline-info btn-sm isotope-filter" data-filter=".lang-fr">
FR
</button>
<button type="button" class="btn btn-outline-info btn-sm isotope-filter" data-filter=".lang-en">
EN
</button>
<button type="button" class="btn btn-outline-info btn-sm isotope-filter" data-filter=".lang-pt">
PT
</button>
</div>
</div>
<div class="card">
<div class="card-body isotope-group" data-filter-group="minplayers">
<h5 class="card-title">Joueurs min.</h5>
<button type="button" class="btn btn-outline-warning btn-sm isotope-filter isotope-filter-minplayers" data-filter="minplayers" data-players="1">
1
</button>
<button type="button" class="btn btn-outline-warning btn-sm isotope-filter isotope-filter-minplayers" data-filter="minplayers" data-players="2">
2
</button>
<button type="button" class="btn btn-outline-warning btn-sm isotope-filter isotope-filter-minplayers" data-filter="minplayers" data-players="3">
3
</button>
</div>
</div>
<div class="card">
<div class="card-body isotope-group" data-filter-group="maxplayers">
<h5 class="card-title">Joueurs max.</h5>
<button type="button" class="btn btn-outline-danger btn-sm isotope-filter isotope-filter-maxplayers" data-filter="maxplayers" data-players="4">
4
</button>
<button type="button" class="btn btn-outline-danger btn-sm isotope-filter isotope-filter-maxplayers" data-filter="maxplayers" data-players="5">
5
</button>
<button type="button" class="btn btn-outline-danger btn-sm isotope-filter isotope-filter-maxplayers" data-filter="maxplayers" data-players="6">
6
</button>
</div>
</div>
</div>
</div>
And here is an example of my game cards :
<div class="row isotope-grid">
<div class="isotope-card lang-fr type-1" data-minplayers="2" data-maxplayers="5">
<div class="card h-100 mb-3 shadow">
<img src="https://picsum.photos/500" class="card-img rounded-bottom-0 object-fit-cover" alt="Adélaïde Allain-Hoarau">
<div class="card-body">
<h5 class="card-title">GAME TITLE</h5>
<p class="card-text">GAME DESC</p>
<div class="card-text mb-2">
<div class="btn-group btn-group-sm">
<a href="http://lbl.test/games/1" class="btn btn-success">Détails</a>
<a href="http://lbl.test/games/1" target="_blank" class="btn btn-success">
<i class="fa-solid fa-arrow-up-right-from-square"></i>
</a>
</div>
</div>
</div>
<div class="card-footer">
<small class="text-body-secondary">Ajouté le 06/06/2023 à 10:31</small><br>
<small class="text-body-secondary">Modifié le 06/06/2023 à 10:31</small>
</div>
</div>
</div>
<div class="isotope-card lang-en type-2" data-minplayers="3" data-maxplayers="4">
<div class="card h-100 mb-3 shadow">
<img src="https://picsum.photos/500" class="card-img rounded-bottom-0 object-fit-cover" alt="Adélaïde Allain-Hoarau">
<div class="card-body">
<h5 class="card-title">GAME TITLE</h5>
<p class="card-text">GAME DESC</p>
<div class="card-text mb-2">
<div class="btn-group btn-group-sm">
<a href="http://lbl.test/games/1" class="btn btn-success">Détails</a>
<a href="http://lbl.test/games/1" target="_blank" class="btn btn-success">
<i class="fa-solid fa-arrow-up-right-from-square"></i>
</a>
</div>
</div>
</div>
<div class="card-footer">
<small class="text-body-secondary">Ajouté le 06/06/2023 à 10:31</small><br>
<small class="text-body-secondary">Modifié le 06/06/2023 à 10:31</small>
</div>
</div>
</div>
</div>
What do I have to do so that my functions can use the HTML attributes in the game cards when it's executed ?
Thank you for your time and patience. I hope I've made myself clear and understandable enough.
Best regards,
SatanicGeek
- Laravel 10.8
- Bootstrap 5.3
- jQuery 3.7.0
- Isotope-layout 3.0.6
- Masonry-layout 4.2.2
答案1
得分: 0
谢谢Pointy,你的解决方案有效。我必须将元素作为参数传递。
以下是新的代码:
var filtersFunctions = {
minplayers: function(itemElem){
// Valeur du jeu
var itemValue = $(itemElem).attr('data-minplayers');
// Valeur du filtre
var filterValue = $('.isotope-filter-minplayers.active').attr('data-players');
// Retourne le résultat du test
return parseInt(itemValue, 10) >= parseInt(filterValue, 10);
},
maxplayers: function(itemElem){
// Valeur du jeu
var itemValue = $(itemElem).attr('data-maxplayers');
// Valeur du filtre
var filterValue = $('.isotope-filter-maxplayers.active').attr('data-players');
// Retourne le résultat du test
return parseInt(itemValue, 10) <= parseInt(filterValue, 10);
}
};
也许是我在使用捆绑在 Laravel 中的 ViteJS 时包含 jQuery 和 Isotope 的方式。
英文:
Thank you Pointy, your solution is working. I had to pass the element as an argument.
Here's the new code :
var filtersFunctions = {
minplayers: function(itemElem){
// Valeur du jeu
var itemValue = $(itemElem).attr('data-minplayers');
// Valeur du filtre
var filterValue = $('.isotope-filter-minplayers.active').attr('data-players');
// Retourne le résultat du test
return parseInt(itemValue, 10) >= parseInt(filterValue, 10);
},
maxplayers: function(itemElem){
// Valeur du jeu
var itemValue = $(itemElem).attr('data-maxplayers');
// Valeur du filtre
var filterValue = $('.isotope-filter-maxplayers.active').attr('data-players');
// Retourne le résultat du test
return parseInt(itemValue, 10) <= parseInt(filterValue, 10);
}
};
It is maybe the way I include jQuery and Isotope using ViteJS which is bundled with Laravel.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论