Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在初始页面加载混合上显示筛选项目的计数_Javascript_Jquery_Filter_Mixitup - Fatal编程技术网

Javascript 在初始页面加载混合上显示筛选项目的计数

Javascript 在初始页面加载混合上显示筛选项目的计数,javascript,jquery,filter,mixitup,Javascript,Jquery,Filter,Mixitup,我使用Mixitup3对项目进行排序和筛选,并希望在初始页面加载时显示每个筛选类别中的项目数。我已经尝试了SO()中的示例,但是它的mixitup2在我的案例中不起作用 我希望显示每个控件筛选器中的已筛选项计数,如下所示: <div class="controls btn-toolbar d-flex justify-content-between mb-2" role="toolbar" aria-label="Toolbar with button groups">

我使用Mixitup3对项目进行排序和筛选,并希望在初始页面加载时显示每个筛选类别中的项目数。我已经尝试了SO()中的示例,但是它的mixitup2在我的案例中不起作用

我希望显示每个控件筛选器中的已筛选项计数,如下所示:

    <div class="controls btn-toolbar d-flex justify-content-between mb-2" role="toolbar" aria-label="Toolbar with button groups">
        <div class="btn-group" role="group" aria-label="First group">
            <button type="button" data-mixitup-control class="control btn btn-secondary badge-pill" data-filter=".filter-1">Filter 1
                <span class="badge badge-light ml-1" id="count1">{Show Items Count}</span>
            </button>
            <button type="button" data-mixitup-control class="control btn btn-secondary badge-pill" data-filter=".filter-2">Filter 2
                <span class="badge badge-light ml-1" id="count2">{Show Items Count}</span>
            </button>
        </div>
    </div>

从其他示例中,我了解到我需要使用
state.totalShow
,从MixItUp文档中,我看到了
加载
功能,但我没有找到满足我需要的解决方案。

我找到了一个解决方案,该解决方案可能不是以最高效和最佳实践的方式编写的,但它是有效的对我来说

首先,我通过

var state = mixer.getState();
然后我遍历state.targets对象,在ClassList.value中搜索匹配的选择器并将它们相加。然后将相加的值解析为span元素,如下所示:

    var mixer = mixitup(containerEl, {
        animation: {
            effects: 'fade translateZ(-100px) stagger(100ms)',
            easing: 'cubic-bezier(0.645, 0.045, 0.355, 1)',
            staggerSequence: function(i) {
                return i % 3;
            },
            duration: 300,
            applyPerspective: false
        },
        selectors: {
            control: '[data-mixitup-control]'
        }
    });
var count1 = 0;
var count2 = 0;

$.each( state.targets, function( index, value ) {
    if (this.classList.value.match('filter-1')) {
        count1 += this.classList.value.match('filter-1').length;
        $('#count1').html(count1);
    }
});

$.each( state.targets, function( index, value ) {
    if (this.classList.value.match('filter-2')) {
        count2 += this.classList.value.match('filter-2').length;
        $('#count2').html(count2);
    }
});

我提出了另一个解决方案,这是一个非常简单但有效的方案。我试图在页面加载时使用过滤器计算数字或返回结果。过滤器由url中的哈希设置……在本例中为“出售”或“租赁”。我对.sale或.lease的页面加载进行了筛选,我能够使用var showing=$('.sale').length对这些类进行计数,然后将其用作我的计数

var count1 = 0;
var count2 = 0;

$.each( state.targets, function( index, value ) {
    if (this.classList.value.match('filter-1')) {
        count1 += this.classList.value.match('filter-1').length;
        $('#count1').html(count1);
    }
});

$.each( state.targets, function( index, value ) {
    if (this.classList.value.match('filter-2')) {
        count2 += this.classList.value.match('filter-2').length;
        $('#count2').html(count2);
    }
});