Isotope自定义函数无法访问HTML属性。

huangapple go评论87阅读模式
英文:

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(&#39;load&#39;, function(){

    var filtersFunctions = {
        minplayers: function(){
            var itemValue = $(this).attr(&#39;data-minplayers&#39;);
            var filterValue = $(&#39;.isotope-filter-minplayers.active&#39;).attr(&#39;data-players&#39;);

            return parseInt(itemValue, 10) &gt;= parseInt(filterValue, 10);
        },
        maxplayers: function(){
            var itemValue = $(this).attr(&#39;data-maxplayers&#39;);
            var filterValue = $(&#39;.isotope-filter-maxplayers.active&#39;).attr(&#39;data-players&#39;);

            return parseInt(itemValue, 10) &lt;= parseInt(filterValue, 10);
        }
    };

    var filters = {};

    var $grid = $(&#39;.isotope-grid&#39;).isotope({
        itemSelector: &#39;.isotope-card&#39;,
        layoutMode: &#39;masonry&#39;
    });

    $(&#39;#isotope-filters&#39;).on(&#39;click&#39;, &#39;.isotope-filter&#39;, function(){

        var $this = $(this);
        var $buttonGroup = $this.parents(&#39;.isotope-group&#39;);
        var filterGroup = $buttonGroup.attr(&#39;data-filter-group&#39;);

        if($this.hasClass(&#39;active&#39;)){
            delete filters[filterGroup];
            $this.removeClass(&#39;active&#39;);
        }
        else{
            $buttonGroup.find(&#39;.isotope-filter&#39;).removeClass(&#39;active&#39;);
            $this.addClass(&#39;active&#39;);
            filters[filterGroup] = $this.attr(&#39;data-filter&#39;);
        }

        var filterValue = filtersFunctions[filters[filterGroup]] || concatValues(filters);

        $grid.isotope({filter: filterValue});
    });
});

function concatValues(obj){

    var value = &#39;&#39;;

    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(&#39;data-minplayers&#39;) or $(this).attr(&#39;data-maxplayers&#39;)) 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 :

&lt;div id=&quot;isotope-filters&quot;&gt;
    &lt;div class=&quot;card-group shadow&quot;&gt;
        &lt;div class=&quot;card&quot;&gt;
            &lt;div class=&quot;card-body isotope-group&quot; data-filter-group=&quot;types&quot;&gt;
                &lt;h5 class=&quot;card-title&quot;&gt;Types de jeux&lt;/h5&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-primary btn-sm isotope-filter&quot; data-filter=&quot;.type-null&quot;&gt;
                    Jeux sans types
                &lt;/button&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-primary btn-sm isotope-filter&quot; data-filter=&quot;.type-1&quot;&gt;
                    Type 1
                &lt;/button&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-primary btn-sm isotope-filter&quot; data-filter=&quot;.type-2&quot;&gt;
                    Type 2
                &lt;/button&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-primary btn-sm isotope-filter&quot; data-filter=&quot;.type-3&quot;&gt;
                    Type 3
                &lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class=&quot;card&quot;&gt;
            &lt;div class=&quot;card-body isotope-group&quot; data-filter-group=&quot;langs&quot;&gt;
                &lt;h5 class=&quot;card-title&quot;&gt;Langues&lt;/h5&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-info btn-sm isotope-filter&quot; data-filter=&quot;.lang-fr&quot;&gt;
                    FR
                &lt;/button&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-info btn-sm isotope-filter&quot; data-filter=&quot;.lang-en&quot;&gt;
                    EN
                &lt;/button&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-info btn-sm isotope-filter&quot; data-filter=&quot;.lang-pt&quot;&gt;
                    PT
                &lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class=&quot;card&quot;&gt;
            &lt;div class=&quot;card-body isotope-group&quot; data-filter-group=&quot;minplayers&quot;&gt;
                &lt;h5 class=&quot;card-title&quot;&gt;Joueurs min.&lt;/h5&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-warning btn-sm isotope-filter isotope-filter-minplayers&quot; data-filter=&quot;minplayers&quot; data-players=&quot;1&quot;&gt;
                    1
                &lt;/button&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-warning btn-sm isotope-filter isotope-filter-minplayers&quot; data-filter=&quot;minplayers&quot; data-players=&quot;2&quot;&gt;
                    2
                &lt;/button&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-warning btn-sm isotope-filter isotope-filter-minplayers&quot; data-filter=&quot;minplayers&quot; data-players=&quot;3&quot;&gt;
                    3
                &lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class=&quot;card&quot;&gt;
            &lt;div class=&quot;card-body isotope-group&quot; data-filter-group=&quot;maxplayers&quot;&gt;
                &lt;h5 class=&quot;card-title&quot;&gt;Joueurs max.&lt;/h5&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-danger btn-sm isotope-filter isotope-filter-maxplayers&quot; data-filter=&quot;maxplayers&quot; data-players=&quot;4&quot;&gt;
                    4
                &lt;/button&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-danger btn-sm isotope-filter isotope-filter-maxplayers&quot; data-filter=&quot;maxplayers&quot; data-players=&quot;5&quot;&gt;
                    5
                &lt;/button&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-danger btn-sm isotope-filter isotope-filter-maxplayers&quot; data-filter=&quot;maxplayers&quot; data-players=&quot;6&quot;&gt;
                    6
                &lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

And here is an example of my game cards :

&lt;div class=&quot;row isotope-grid&quot;&gt;
    &lt;div class=&quot;isotope-card lang-fr type-1&quot; data-minplayers=&quot;2&quot; data-maxplayers=&quot;5&quot;&gt;
        &lt;div class=&quot;card h-100 mb-3 shadow&quot;&gt;
            &lt;img src=&quot;https://picsum.photos/500&quot; class=&quot;card-img rounded-bottom-0 object-fit-cover&quot; alt=&quot;Ad&#233;la&#239;de Allain-Hoarau&quot;&gt;
            &lt;div class=&quot;card-body&quot;&gt;
                &lt;h5 class=&quot;card-title&quot;&gt;GAME TITLE&lt;/h5&gt;
                &lt;p class=&quot;card-text&quot;&gt;GAME DESC&lt;/p&gt;
                &lt;div class=&quot;card-text mb-2&quot;&gt;
                    &lt;div class=&quot;btn-group btn-group-sm&quot;&gt;
                        &lt;a href=&quot;http://lbl.test/games/1&quot; class=&quot;btn btn-success&quot;&gt;D&#233;tails&lt;/a&gt;
                        &lt;a href=&quot;http://lbl.test/games/1&quot; target=&quot;_blank&quot; class=&quot;btn btn-success&quot;&gt;
                            &lt;i class=&quot;fa-solid fa-arrow-up-right-from-square&quot;&gt;&lt;/i&gt;
                        &lt;/a&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&quot;card-footer&quot;&gt;
                &lt;small class=&quot;text-body-secondary&quot;&gt;Ajout&#233; le 06/06/2023 &#224; 10:31&lt;/small&gt;&lt;br&gt;
                &lt;small class=&quot;text-body-secondary&quot;&gt;Modifi&#233; le 06/06/2023 &#224; 10:31&lt;/small&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;isotope-card lang-en type-2&quot; data-minplayers=&quot;3&quot; data-maxplayers=&quot;4&quot;&gt;
        &lt;div class=&quot;card h-100 mb-3 shadow&quot;&gt;
            &lt;img src=&quot;https://picsum.photos/500&quot; class=&quot;card-img rounded-bottom-0 object-fit-cover&quot; alt=&quot;Ad&#233;la&#239;de Allain-Hoarau&quot;&gt;
            &lt;div class=&quot;card-body&quot;&gt;
                &lt;h5 class=&quot;card-title&quot;&gt;GAME TITLE&lt;/h5&gt;
                &lt;p class=&quot;card-text&quot;&gt;GAME DESC&lt;/p&gt;
                &lt;div class=&quot;card-text mb-2&quot;&gt;
                    &lt;div class=&quot;btn-group btn-group-sm&quot;&gt;
                        &lt;a href=&quot;http://lbl.test/games/1&quot; class=&quot;btn btn-success&quot;&gt;D&#233;tails&lt;/a&gt;
                        &lt;a href=&quot;http://lbl.test/games/1&quot; target=&quot;_blank&quot; class=&quot;btn btn-success&quot;&gt;
                            &lt;i class=&quot;fa-solid fa-arrow-up-right-from-square&quot;&gt;&lt;/i&gt;
                        &lt;/a&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&quot;card-footer&quot;&gt;
                &lt;small class=&quot;text-body-secondary&quot;&gt;Ajout&#233; le 06/06/2023 &#224; 10:31&lt;/small&gt;&lt;br&gt;
                &lt;small class=&quot;text-body-secondary&quot;&gt;Modifi&#233; le 06/06/2023 &#224; 10:31&lt;/small&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

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(&#39;data-minplayers&#39;);

            // Valeur du filtre
            var filterValue = $(&#39;.isotope-filter-minplayers.active&#39;).attr(&#39;data-players&#39;);

            // Retourne le r&#233;sultat du test
            return parseInt(itemValue, 10) &gt;= parseInt(filterValue, 10);
        },
        maxplayers: function(itemElem){

            // Valeur du jeu
            var itemValue = $(itemElem).attr(&#39;data-maxplayers&#39;);

            // Valeur du filtre
            var filterValue = $(&#39;.isotope-filter-maxplayers.active&#39;).attr(&#39;data-players&#39;);

            // Retourne le r&#233;sultat du test
            return parseInt(itemValue, 10) &lt;= parseInt(filterValue, 10);
        }
    };

It is maybe the way I include jQuery and Isotope using ViteJS which is bundled with Laravel.

huangapple
  • 本文由 发表于 2023年7月6日 20:26:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定