Skip to content

List view ​

A list of items with a pager: articles, people, products, notifications. Each item shows fields of its row: a title (a link if you want), a description, short meta texts, a picture and actions. The user can page, search and sort, client-side or server-side.

Listview demo. Data is embedded and nothing is saved.
php
<?php

use Caelrik\Ui\Components\Action;
use Caelrik\Ui\Components\ListView;

// The demo answers requests in the browser: nothing is saved.
return ListView::make('reading')
    ->label('Reading list')
    ->title('title')
    ->link('?article={id}')
    ->description('summary')
    ->meta('author', 'year')
    ->searchable()
    ->sorts(['-year' => 'Newest first', 'year' => 'Oldest first', 'title' => 'Title'])
    ->pageSize(4)
    ->actions(Action::request('save', 'Save for later', '/demo-api/reading/{id}'))
    ->rows([
        ['id' => 1, 'title' => 'Notes on the Analytical Engine', 'author' => 'Ada Lovelace', 'year' => 1843,
            'summary' => 'The first published program, and what a machine might compose.'],
        ['id' => 2, 'title' => 'On Computable Numbers', 'author' => 'Alan Turing', 'year' => 1936,
            'summary' => 'Which problems a machine can decide, and which it cannot.'],
        ['id' => 3, 'title' => 'A Mathematical Theory of Communication', 'author' => 'Claude Shannon', 'year' => 1948,
            'summary' => 'Information, measured in bits.'],
        ['id' => 4, 'title' => 'The Education of a Computer', 'author' => 'Grace Hopper', 'year' => 1952,
            'summary' => 'Programs that write programs: the compiler.'],
        ['id' => 5, 'title' => 'Go To Statement Considered Harmful', 'author' => 'Edsger Dijkstra', 'year' => 1968,
            'summary' => 'A letter on structured programming.'],
        ['id' => 6, 'title' => 'Programming with Abstract Data Types', 'author' => 'Barbara Liskov', 'year' => 1974,
            'summary' => 'Data and its operations, together.'],
    ]);

Client-side or server-side ​

  • Client-side: pass the rows with rows(). PHP writes every item, and the browser pages, searches and sorts them. Without JavaScript, every item is shown.
  • Server-side: pass an endpoint with url(), and answer it with respond() using the same list (see Data endpoints). The browser asks for one page at a time: ?page=2&per_page=10&search=ada&sort=-published.
php
$list = ListView::make('articles')
    ->label('Articles')
    ->url('/articles/data')
    ->title('title')
    ->link('/articles/{id}')
    ->description('summary')
    ->meta('author', 'published')
    ->searchable()
    ->sorts(['-published' => 'Newest first', 'title' => 'Title']);

// GET /articles/data
echo json_encode($list->respond(new EloquentDataSource(Article::query()), $request->query()));

respond() sorts only by the list's sorts, searches only its title, description and meta fields, and sends only the fields the list shows and its actions read.

Options ​

MethodUse
label($label)The list's accessible name, e.g. 'Articles'. It also names the pager ("Pages of Articles").
title($field)The field that titles each item, as a heading. It also names the item's actions ("Delete Notes on the Engine"). Required.
link('/articles/{id}')Makes the title a link; {field} takes the row's value, URL-encoded.
description($field)A paragraph under the title.
meta($field, ...)Short texts under it, e.g. the author and the date.
image($field, $altField = null)A picture from a URL field: relative, http or https only. Without $altField, it is decorative.
headingLevel(3)The titles' heading level, 2 to 6: one below the heading above the list.
layout('list'), layout('cards', 3)One item per line, or cards: one column on phones, two on small screens, $columns on large ones.
rows(iterable $rows)Client-side items.
url($url)Server-side mode.
pageSize(10)Items per page.
searchable()A search box over the titles, descriptions and meta texts.
sorts(['-published' => 'Newest first', 'title' => 'Title'])The orders to choose from, the first by default; - sorts descending.
actions(Action ...$actions)Actions for each item, including ones that open a form.
emptyState($title, $description = null, $action = null)What the list shows without items.
labels([...])Translates search, sort, pages (with {label}), previous, next, page (with {page}), showing (with {from}, {to}, {total}), empty, noMatches, reset, loading and failed.

Accessibility ​

  • The list is a <ul> named by label(), and each item's title is a heading, so screen reader users can jump from item to item.
  • The pager is a <nav> named "Pages of Articles", with Previous, Next and page buttons. The current page has aria-current="page"; Previous and Next are disabled at the ends. Its buttons are 36px tall.
  • Changing page moves focus to the list, so reading starts at its first item, and the status says which items are shown ("5–8 of 23").
  • Searching and sorting start again from page 1. When nothing matches, the list says so, with a Clear search button that returns focus to the search box.
  • Pictures take their text alternative from a field, or are decorative.

Security ​

  • Titles, descriptions, meta texts and picture alternatives are escaped. Link values are URL-encoded.
  • Picture URLs other than relative, http or https are left out, so a javascript: URL cannot run.
  • respond() sends only the fields the list uses; other columns of your rows never reach the browser.

Released under the MIT License.