Alex

Alex

С нами с 22 февраля 2025; Место в рейтинге пользователей: #867
Alex
23 февраля 2025, 14:39
0
Возможно вариант неидеальный и неокончательный, но уже рабочий)
В шаблоне внес правки в скрипт
<script>
$(document).ready(function () {
    let parentId = [[*id]];

    updateProducts(1, true);

    $('.filter-checkbox').on('change', function () {
        updateProducts(1, true);
    });

    $('#products-container').on('click', '.pagination a', function(e) {
        e.preventDefault();
        let pageUrl = $(this).attr('href');
        let urlParams = new URLSearchParams(pageUrl.split('?')[1]);
        let pageParam = urlParams.get('page') || 1;
        updateProducts(pageParam, true);
    });

    function updateProducts(page = 1, replaceContent = true) {
        console.log('Page:', page);

        let filterData = $('#filter-form').serialize();

        $.ajax({
            url: '/assets/components/filter.php',
            type: 'GET',
            data: filterData + '&parent=' + parentId + '&page=' + page,
            beforeSend: function () {
                if (replaceContent) {
                    $('#products-container').html('<p>Загрузка...</p>');
                }
            },
            success: function (data) {
                console.log('Response:', data);
                if (replaceContent) {
                    $('#products-container').html(data);
                } else {
                    $('#products-container').append(data);
                }
            },
            error: function(xhr, status, error) {
                console.log('Error:', status, error);
            }
        });
    }
});
</script>
И в файле обработчике поменял вызов на

$page = isset($_GET['page']) ? max((int)$_GET['page'], 1) : 1;
$limit = 1;
$offset = ($page - 1) * $limit;
$total = count($productIds);
$pages = ceil($total / $limit);
$productIds = array_slice($productIds, $offset, $limit);

$output = $modx->runSnippet('Pagination', [
    'class' => 'msProduct',
    'render' => '!pdoResources',
    'resources' => implode(',', $productIds),
    'tpl' => 'tpl.msProducts.row',
    'includeTVs' => 'back-img,product-img,ice_properties_uk,weight',
    'tplEmpty' => '',
    'limit' => $limit,
    'loadModels' => 'ms2',
    'includeContent' => 1,
    'page' => $page,
    'maxPageListItems' => 5,
    'pagination' => 'one',
    'leftJoin' => json_encode([
        "msProductData" => [
            "class" => "msProductData",
            "on" => "msProduct.id = msProductData.id"
        ]
    ]),
    'select' => json_encode([
        "msProduct" => "id, pagetitle",
        "msProductData" => "price",
        "modx_ms2_products" => "opisanie_produkta,weight"
    ])
]);

$output .= '<div class="pagination">';
if ($page > 1) {
    $output .= '<a href="?page=' . ($page - 1) . '">« Предыдущая</a>';
}
for ($i = 1; $i <= $pages; $i++) {
    $output .= '<a href="?page=' . $i . '"' . ($i === $page ? ' class="active"' : '') . '>' . $i . '</a>';
}
if ($page < $pages) {
    $output .= '<a href="?page=' . ($page + 1) . '">Следующая »</a>';
}
$output .= '</div>';

echo $output;
exit;
Alex
23 февраля 2025, 03:16
0
Спасибо вам за то, что подсказали правильное направление — все получилось)