Ir al contenido

Una lista paginada ​

Traducción automática

Esta página se tradujo automáticamente del inglés y todavía no la revisó un hablante nativo; las correcciones son bienvenidas en GitHub. Si algo no coincide, vale el original en inglés.

Tarea: mostrar 23 publicaciones de diez en diez, con enlaces a las otras páginas.

yaml
# Recipe: a long list, one page at a time, with the links to the others.
paths:
  components: ./components
  migrations: ./migrations

datasources:
  db:
    driver: sqlite
    database: ./data/blog.db
sql
CREATE TABLE posts (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title TEXT NOT NULL
);

-- 23 posts: "Post 1" … "Post 23".
WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 23)
INSERT INTO posts (title) SELECT 'Post ' || i FROM n;

paginate="true" hace que la consulta devuelva una página — la de ?page= en la URL, o la primera — y completa posts_result.pagination con los totales. <ui:pager for="posts"> dibuja anterior, los números de página y siguiente, conservando los demás parámetros de la URL.

xml
<q:component name="Posts">
  <!-- paginate: the page is the URL's ?page= (page 1 without it). -->
  <q:query name="posts" datasource="db" paginate="true" page_size="10">
    SELECT id, title FROM posts ORDER BY id DESC
  </q:query>

  <ui:window title="Posts">
    <ui:text>
      {posts_result.pagination.startRecord}–{posts_result.pagination.endRecord}
      of {posts_result.pagination.totalRecords}
    </ui:text>
    <ui:list source="{posts}" as="p">
      <ui:item><ui:text>{p.title}</ui:text></ui:item>
    </ui:list>
    <!-- Previous, the page numbers, next — keeping the URL's other parameters. -->
    <ui:pager for="posts" />
  </ui:window>
</q:component>

Un page que no es un número positivo es la página 1, nunca un error:

xml
<q:test name="the first page" page="/">
  <test:visit />
  <test:expect text="1–10 of 23" />
  <test:expect text="Post 23" />
  <test:expect text="Post 14" />
  <test:expect no-text="Post 13" />
</q:test>

<q:test name="the last page" page="/">
  <test:visit page="3" />
  <test:expect text="21–23 of 23" />
  <test:expect text="Post 1" />
  <test:expect no-text="Post 4" />
</q:test>

<q:test name="a page that is not a number is page 1" page="/">
  <test:visit page="abc" />
  <test:expect text="1–10 of 23" />
</q:test>
text
tests/posts.test.q
  PASS  the first page
  PASS  the last page
  PASS  a page that is not a number is page 1
3 passed, 0 failed

Ver DB-2, DB-9 y UI-11.

Licencia MIT · Hecho con VitePress