A
A
Anton Parkhomenko2019-11-22 10:32:46
MODX
Anton Parkhomenko, 2019-11-22 10:32:46

How to enable filtering in a container?

Good day to all.
I do resource filtering in Modx Revo.
Ajax filter code

$(function() {

    //MODx pdoResources Ajax Filter
    //Filter Settings
    var fadeSpeed             = 200, // Fade Animation Speed
        ajaxCountSelector     = '.ajax-count', // CSS Selector of Items Counter
        ajaxContainerSelector = '.ajax-container', // CSS Selector of Ajax Container
        ajaxItemSelector      = '.ajax-item', // CSS Selector of Ajax Item
        ajaxFormSelector      = '.ajax-form', // CSS Selector of Ajax Filter Form
        ajaxFormButtonStart   = '.ajax-start', // CSS Selector of Button Start Filtering
        ajaxFormButtonReset   = '.ajax-reset', // CSS Selector of Button Reset Ajax Form
        sortDownText          = 'По убыванию',
        sortUpText            = 'По возрастанию';

    function ajaxCount() {
        if($('.ajax-filter-count').length) {
            var count = $('.ajax-filter-count').data('count');
            $(ajaxCountSelector).text(count);
        } else {
            $(ajaxCountSelector).text($(ajaxItemSelector).length);
        }
    }ajaxCount();

    function ajaxMainFunction() {
        $.ajax({
            data: $(ajaxFormSelector).serialize()
        }).done(function(response) {
            var $response = $(response);
            $(ajaxContainerSelector).fadeOut(fadeSpeed);
            setTimeout(function() {
                $(ajaxContainerSelector).html($response.find(ajaxContainerSelector).html()).fadeIn(fadeSpeed);
                ajaxCount();
            }, fadeSpeed);
        });
    }

    $(ajaxContainerSelector).on('click', '.ajax-more', function(e) {
        e.preventDefault();
        var offset = $(ajaxItemSelector).length;
        $.ajax({
            data: $(ajaxFormSelector).serialize()+'&offset='+offset
        }).done(function(response) {
            $('.ajax-more').remove();
            var $response = $(response);
            $response.find(ajaxItemSelector).hide();
            $(ajaxContainerSelector).append($response.find(ajaxContainerSelector).html());
            $(ajaxItemSelector).fadeIn();
        });
    })

    $(ajaxFormButtonStart).click(function(e) {
        e.preventDefault();
        ajaxMainFunction();
    })

    $(ajaxFormButtonReset).click(function(e) {
        e.preventDefault();
        $(ajaxFormSelector).trigger('reset');
        $('input[name=sortby]').val('pagetitle');
        $('input[name=sortdir]').val('asc');
        setTimeout(function() {
            $('[data-sort-by]').data('sort-dir', 'asc').toggleClass('button-sort-asc').text(sortUpText);
        }, fadeSpeed);
        ajaxMainFunction();
        ajaxCount();
    })

    $(''+ajaxFormSelector+' input').change(function() {
        ajaxMainFunction();
    })

    $('[data-sort-by]').data('sort-dir', 'asc').click(function() {
        var ths = $(this);
        $('input[name=sortby]').val($(this).data('sort-by'));
        $('input[name=sortdir]').val($(this).data('sort-dir'));
        setTimeout(function() {
            $('[data-sort-by]').not(this).toggleClass('button-sort-asc').text(sortUpText);
            ths.data('sort-dir') == 'asc' ? ths.data('sort-dir', 'desc').text(sortDownText) : ths.data('sort-dir', 'asc').text(sortUpText);
            $(this).toggleClass('button-sort-asc');
        }, fadeSpeed);
        ajaxMainFunction();
    });

});

And the PHP snippet itself
<?php
//Filter Fields Settings
$filter = array();

//Radio, Select & Text Fields Type
if($_GET['floor']) {
    $filter[] = 'floor='.$_GET['floor'];
}

//Two Text Fields From To
if($_GET['area_from']) {
    $filter[] = 'area>='.$_GET['area_from'];
}
if($_GET['area_to']) {
    $filter[] = 'area<='.$_GET['area_to'];
}

//Checkbox Type
if($_GET['garage']) {
    $filter[] = 'garage=1';
}

//End Settings

//Sort
if($_GET['sortby']) {
    $sortby = $_GET['sortby'];
} else {
    $sortby = 'pagetitle';
}
if($_GET['sortdir']) {
    $sortdir = $_GET['sortdir'];
} else {
    $sortdir = 'asc';
}
//End Sort

//Offset
$offset = 0;
if($_GET['offset']){
    $offset = $_GET['offset'];
}

if($filter) {
    $where = $modx->toJSON(array($filter));
} else {
    $where = '';
}

$params_count = array(
    'parents' => $parents,
    'limit' => 0,
    'tpl' => '@INLINE ,',
    'select' => 'id',
    'includeTVs' => $fields,
    'showHidden' => '1',
    'where' => $where
);

$count = $modx->runSnippet('pdoResources',$params_count);
$count = count(explode(',',$count))-1;
$modx->setPlaceholder('count',$count);

$params = array(
    'parents' => $parents,
    'limit' => $limit,
    'offset' => $offset,
    'tpl' => $tpl,
    'select' => 'id,pagetitle,introtext,content',
    'includeTVs' => $fields,
    'showHidden' => '1',
    'sortby' => $sortby,
    'sortdir' => $sortdir,
    'where' => $where
);

$more = $count - $offset - $limit;
$lim = $more > $limit ? $limit : $more;

$button = '';
if($more > 0){
    $button = '<div class="ajax-filter-count" data-count="'.$count.'"><a href="#" class="ajax-more">Загрузить еще '.$lim.' из '.$more.'</a></div>';
}

return $modx->runSnippet('pdoResources',$params).$button;

Everything works great, but there is one thing: filtering does not work if the resource on which the materials are displayed is turned into a container. Please tell me where to dig?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question