table/htmx
< >

return function () {
    $self = url('/@object/@action/' . urlencode('table/htmx'));
    $mode = in_array($_GET['mode'] ?? '', ['more', 'infinite'], true) ? $_GET['mode'] : 'pages';
    $db = null;
    if (extension_loaded('pdo_sqlite')) {
        $db = clone db();
        $db->DB_TYPE = 'pdo_sqlite';
        $db->DB_PREFIX = '';
        $db->LOG_ALL_QUERIES = false;
        $db->LOG_SLOW_QUERIES = false;
        $db->ERROR_AUTO_REPAIR = false;
        $class = main()->load_class_file('db_driver_pdo_sqlite', 'classes/db/');
        $db->db = new $class(['name' => ':memory:']);
        $db->_connected = true;
        $db->_tried_to_connect = true;
        $pdo = $db->db->db_connect_id;
        $pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, colour TEXT, price REAL, sold INTEGER, add_date INTEGER)');
        $insert = $pdo->prepare('INSERT INTO products (id, name, colour, price, sold, add_date) VALUES (?, ?, ?, ?, ?, ?)');
        foreach (_class('docs')->_sample('products')->load() as $row) {
            $insert->execute([(int) $row['id'], (string) $row['name'], (string) $row['colour'], (float) $row['price'], (int) $row['sold'], (int) $row['add_date']]);
        }
    }
    // the table: the same call answers the page and, for an htmx request aimed at it, only its own fragment (render() ends the request)
    $table = $db ? table('SELECT id, name, colour, price, sold, add_date AS added FROM products', [
            'db' => $db,
            'filter' => [],
            'filter_params' => ['name' => 'like', 'colour' => 'like', '__default_order' => 'ORDER BY id ASC'],
            'pager_records_on_page' => 5,
            'no_total' => 1,
            'condensed' => 1,
            'htmx' => [
                'id' => 'docs-htmx-products',
                // sort keys a visitor may pick; anything else in ?sort= is ignored
                'sort' => ['name', 'colour', 'price', 'sold', 'added' => 'add_date'],
                // equal values (many products sold 0) keep one order across pages
                'tiebreak' => 'id',
                'filter' => ['name', 'colour'],
                'pager' => $mode,
            ],
            'no_records_html' => '<div class="alert alert-info">Nothing matches this filter.</div>',
        ])
        ->text('name')
        ->text('colour')
        ->text('price')
        ->text('sold')
        ->func('added', fn ($v) => $v ? date('Y-m-d', (int) $v) : '-')
        : '<div class="alert alert-warning">This demo needs the pdo_sqlite extension.</div>';

    $intro = '<p class="docs-demo-intro">table2 in htmx mode: <code>table($sql, [\'htmx\' => [...]])</code> or <code>->htmx()</code>.
        The column headers sort (only the keys of the <code>sort</code> option, mapped to columns, ASC or DESC: the query never takes
        the request text), the fields above filter while you type (the <code>filter_params</code> conditions of the table) and the
        pager swaps only the table; the address bar follows, so Back and a reload show the same state. Without JavaScript the headers
        are plain links and the filter a GET form. An htmx request aimed at the table gets only its fragment. The data is an SQLite
        copy in memory of the demo products; nothing is stored. Pager: '
        . implode(' | ', array_map(fn ($m) => $m === $mode ? '<b>' . $m . '</b>' : '<a href="' . $self . ($m === 'pages' ? '' : '?mode=' . $m) . '">' . $m . '</a>', ['pages', 'more', 'infinite']))
        . '. Classes: <code>yf_table2</code>, <code>yf_table2_htmx</code>, <code>yf_htmx</code>; asset <code>htmx</code>.</p>';
    return $intro . $table;
};

table2 in htmx mode: table($sql, ['htmx' => [...]]) or ->htmx(). The column headers sort (only the keys of the sort option, mapped to columns, ASC or DESC: the query never takes the request text), the fields above filter while you type (the filter_params conditions of the table) and the pager swaps only the table; the address bar follows, so Back and a reload show the same state. Without JavaScript the headers are plain links and the filter a GET form. An htmx request aimed at the table gets only its fragment. The data is an SQLite copy in memory of the demo products; nothing is stored. Pager: pages | more | infinite. Classes: yf_table2, yf_table2_htmx, yf_htmx; asset htmx.

Name Colour Price Sold Added
Flash T-shirt, white white 35 90 2013-09-27
Mix Tape T-shirt, black black 45 826 2013-09-29
Winner T-shirt, grey grey 35 787 2013-10-02
Hill T-shirt, white white 35 518 2013-10-01
Roseheart T-shirt, black black 35 0 -
Load more