return function () {
    $self = url('/@object/@action/' . urlencode('html/pagination'));
    $pager = _class('docs')->_sample('pagination');
    $rows = _class('docs')->_sample('products')->load();
    $p = $pager->params($_GET, count($rows));
    $query = $pager->query($p);
    // the pager appends &page=N; the demo id holds a slash, so the base url is given (the default ./?object=&action=&id= would not do)
    $url_path = $self . '?' . $query;

    $intro = '<p class="docs-demo-intro">' . t('The framework pager: <code>common()->divide_pages()</code> takes an SQL query, a query builder or an array and returns the rows (or the <code>LIMIT</code>) of the current page with the page links; <code>table2</code> pages with it too (<code>pager_records_on_page</code>, see the <a href="%url">table demo</a>). Here it pages the %count sample products in memory. <b>slide</b> keeps the current page in the middle of the visible numbers, <b>blocks</b> shows fixed blocks with jumps to the previous / next block. The options come from fixed lists and the page number is clamped, so <code>?page=999</code> shows the last page; the links work without JavaScript. Classes: <code>yf_divide_pages</code>, <code>yf_common</code>, <code>yf_html</code> (<code>pagination()</code>).', [
        '%url' => url('/@object/@action/' . urlencode('table/products')),
        '%count' => count($rows),
    ]) . '</p>';

    $options = function ($name, array $values, $selected, $label) {
        $html = '<label>' . $label . ' <select name="' . $name . '" class="form-control form-select input-sm form-select-sm">';
        foreach ($values as $value => $text) {
            $html .= '<option value="' . _prepare_html((string) $value, 0) . '"' . ((string) $value === (string) $selected ? ' selected' : '') . '>' . _prepare_html((string) $text, 0) . '</option>';
        }
        return $html . '</select></label>';
    };
    $form = '<form method="get" action="' . $self . '" class="docs-demo-pager-form">'
        . $options('per_page', array_combine($pager->PER_PAGE, $pager->PER_PAGE), $p['per_page'], t('Per page'))
        . $options('type', $pager->TYPES, $p['type'], t('Type'))
        . $options('block', array_combine($pager->BLOCKS, $pager->BLOCKS), $p['block'], t('Pages per block'))
        . $options('size', $pager->SIZES, $p['size'], t('Size'))
        . $options('align', $pager->ALIGNS, $p['align'], t('Align'))
        . '<button type="submit" class="btn btn-primary btn-sm">' . t('Apply') . '</button>'
        . '<a class="btn btn-default btn-outline-secondary btn-sm" href="' . $self . '">' . t('Reset') . '</a></form>';

    [$items, $pages_html, $total, $first, $total_pages] = $pager->divide($rows, $p, $url_path);
    $trs = '';
    $n = $first;
    foreach ((array) $items as $row) {
        $trs .= '<tr><td>' . (++$n) . '</td><td>' . _prepare_html((string) $row['name'], 0) . '</td><td>' . _prepare_html((string) $row['colour'], 0) . '</td>'
            . '<td class="text-right text-end">' . _prepare_html((string) $row['price'], 0) . '</td></tr>';
    }
    $last = min($total, $first + $p['per_page']);
    $table = '<div class="table-responsive"><table class="table table-condensed table-sm table-striped docs-demo-pager-rows"><thead><tr><th>#</th><th>' . t('Name') . '</th><th>' . t('Colour') . '</th><th class="text-right text-end">' . t('Price') . '</th></tr></thead>'
        . '<tbody>' . $trs . '</tbody></table></div>';
    $summary = '<p class="text-muted">' . t('Products %from–%to of %total, page %page of %pages', ['%from' => $first + 1, '%to' => $last, '%total' => (int) $total, '%page' => $p['page'], '%pages' => (int) $total_pages]) . '</p>';

    [$data, $extra] = $pager->widget($p, (int) $total_pages, $url_path);
    $widget = '<h4>' . t('The bare widget: html()->pagination()') . '</h4>'
        . '<p>' . t('The same state drawn by <code>html()->pagination($links, $extra)</code>: you pass page => link pairs plus <code>prev</code> / <code>next</code>, and the options <code>current</code>, <code>disabled</code>, <code>size</code> and <code>align</code>; it computes nothing itself. Under <a href="%bs5">?bs=5</a> it renders Bootstrap 5 markup (<code>yf_html_bs5</code>), under <a href="%bs3">?bs=3</a> Bootstrap 3.', [
            '%bs5' => htmlspecialchars(_class('docs')->_bs_switch_url('5'), ENT_QUOTES),
            '%bs3' => htmlspecialchars(_class('docs')->_bs_switch_url('3'), ENT_QUOTES),
        ]) . '</p>'
        . html()->pagination($data, $extra);

    css('.docs-demo-pager-form { display:flex; flex-wrap:wrap; align-items:flex-end; gap:8px 12px; margin-bottom:15px; }
        .docs-demo-pager-form label { font-weight:normal; margin:0; } .docs-demo-pager-form .form-control { display:inline-block; width:auto; }
        .cssfw-bs5 .docs-demo-pager-form select.form-select { padding-right:2rem; }
        .docs-demo-pager-rows { margin-bottom:5px; } pre.docs-demo-pager-code code { white-space:pre-wrap; }');
    return $intro . $form
        . '<h4>common()->divide_pages()</h4>' . $table . $summary . $pages_html
        . '<pre class="docs-demo-pager-code"><code class="php">' . _prepare_html($pager->code($p), 0) . '</code></pre>'
        . $widget;
};

Пейджер фреймворку: common()->divide_pages() приймає SQL-запит, конструктор запитів або масив і повертає рядки (або LIMIT) поточної сторінки разом із посиланнями на сторінки; table2 теж ділить на сторінки через нього (pager_records_on_page, див. демо таблиці). Тут він ділить на сторінки 20 товарів-прикладів у пам'яті. slide тримає поточну сторінку посередині видимих номерів, blocks показує фіксовані блоки з переходами до попереднього / наступного блоку. Параметри беруться з фіксованих списків, а номер сторінки обмежується, тож ?page=999 показує останню сторінку; посилання працюють без JavaScript. Класи: yf_divide_pages, yf_common, yf_html (pagination()).

Скинути

common()->divide_pages()

#НазваКолірЦіна
10Paint Bucket T-shirt, purplepurple37.00
11Flash T-shirt, whitewhite35.00
12Mix Tape T-shirt, blackblack45.00

Товари 10–12 з 20, сторінка 4 з 7

[$items, $pages_html, $total, $first, $total_pages] = common()->divide_pages(
    $rows, $url, 'slide', 3, 0, '', 0,
    [
        'requested_page' => $page,
        'pages_per_block' => 5,
        'add_get_vars' => 0,
    ]
);

Сам віджет: html()->pagination()

Той самий стан, намальований через html()->pagination($links, $extra): ви передаєте пари сторінка => посилання плюс prev / next і параметри current, disabled, size та align; сам він нічого не обчислює. Під ?bs=5 він рендерить розмітку Bootstrap 5 (yf_html_bs5), під ?bs=3 — Bootstrap 3.