# Ejemplo de código de Tabla. DESY. Sistema de diseño del Gobierno de Aragón. ## Componentes Índice de páginas Componentes # Tabla Datos Componente para ordenar valores relacionados en filas y columnas. # Table Mostrar parámetros ## Parámetros Nunjucks del componente: "Table" ```yaml params: - name: rows type: array required: true description: Array of table rows and cells. params: - name: text type: string required: true description: If `html` is set, this is not required. Text for cells in table rows. If `html` is provided, the `text` argument will be ignored. - name: html type: string required: true description: If `text` is set, this is not required. HTML for cells in table rows. If `html` is provided, the `text` argument will be ignore - name: classes type: string required: false description: Classes to add to the table row cell. - name: colspan type: integer required: false description: Specify how many columns a cell extends. - name: rowspan type: integer required: false description: Specify how many rows a cell extends. - name: attributes type: object required: false description: HTML attributes (for example data attributes) to add to the table cell. - name: head type: array required: false description: Array of table head cells. params: - name: text type: string required: false description: If `html` is set, this is not required. Text for table head cells. If `html` is provided, the `text` argument will be ignored. - name: html type: string required: false description: If `text` is set, this is not required. HTML for table head cells. If `html` is provided, the `text` argument will be ignore - name: classes type: string required: false description: Classes to add to the table head cell. - name: colspan type: integer required: false description: Specify how many columns a cell extends. - name: rowspan type: integer required: false description: Specify how many rows a cell extends. - name: attributes type: object required: false description: HTML attributes (for example data attributes) to add to the table cell. - name: foot type: array required: false description: Array of table tfoot cells. params: - name: text type: string required: false description: If `html` is set, this is not required. Text for table tfoot cells. If `html` is provided, the `text` argument will be ignored. - name: html type: string required: false description: If `text` is set, this is not required. HTML for table tfoot cells. If `html` is provided, the `text` argument will be ignore - name: classes type: string required: false description: Classes to add to the table tfoot cell. - name: colspan type: integer required: false description: Specify how many columns a cell extends. - name: rowspan type: integer required: false description: Specify how many rows a cell extends. - name: attributes type: object required: false description: HTML attributes (for example data attributes) to add to the table cell. - name: caption type: string required: false description: Caption text - name: captionClasses type: string required: false description: Classes for caption text size. Classes should correspond to the available typography heading classes. - name: firstCellIsHeader type: boolean required: false description: If set to true, first cell in table row will be a TH instead of a TD. - name: wrapper type: object required: false description: Options for a wrapper div outside the table params: - name: classes type: string required: false description: Classes to add to the wrapper (e.g. to show an horizontal scrollbar it there are many columns or in mobile) - name: attributes type: object required: false description: HTML attributes to add to the wrapper div. For accessibility it needs tabindex=0, role=section and aria-label or aria-labelledby. - name: classes type: string required: false description: Classes to add to the table container. - name: attributes type: object required: false description: HTML attributes (for example data attributes) to add to the table container. accessibilityCriteria: | ## Las tablas deben: 1. Debe tener al menos una celda de encabezado (TH) en las filas o columnas exteriores. 2. Si la tabla tiene más de un nivel de encabezado (si hay elementos th en dos filas o en dos columnas) las celdas de datos y de encabezado deben estar asociadas con los atributos id (en los th) /headers (en los td). 3. El título de la tabla debe estar marcado como 4. La primera fila de la tabla no puede tener todas las celdas unidas, o se interpreta que se está simulando un caption. 5. Después de un encabezado (h1, h2…) no puede ir directamente una tabla, o se entiende que se está usando el encabezado para lo que debería ser el 6. La tabla debe tener una descripción especialmente si la tabla es grande o compleja, tiene varios niveles de encabezados, tiene totales o enlaces, etc. La descripción intenta suplir la primera impresión que tienen de la tabla las personas que pueden verla, por ejemplo, vemos que en la última columna hay enlaces o en la última fila hay totales. Como en HTML 5 ya no está admitido el atributo summary en las tablas, lo que se hace es poner la descripción en un párrafo antes de la tabla y asociarlo a la tabla con aria-describedby (así se evita también el error 5). El caption y la descripción no deben ser iguales. 7. El validador del OAW dará error si hay tablas de una única columna y 3 o más filas con texto menor de 150 caracteres, pues considerará que se está simulando una lista. ``` ### Tabla por defecto [#](#tabla-por-defecto) Mostrar códigodel ejemplo: Tabla por defecto #### Contenido Nunjucks macro HTML ##### Nunjucks macro ```js {% from "components/table/_macro.table.njk" import componentTable %} {{ componentTable({ "caption": "Caption de la tabla", "captionClasses": "sr-only mb-base font-bold text-left text-lg", "head": [ { "text": "Mes de pago" }, { "text": "Primer pago", "classes": "lg:text-right" }, { "text": "Segundo pago", "classes": "lg:text-right" } ], "rows": [ [ { "text": "Enero" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" } ], [ { "text": "Febrero" }, { "text": "75€", "classes": "lg:text-right" }, { "text": "55€", "classes": "lg:text-right" } ], [ { "text": "Marzo" }, { "text": "165€", "classes": "lg:text-right" }, { "text": "125€", "classes": "lg:text-right" } ] ] }) }} ``` ##### HTML ```html
Caption de la tabla
Mes de pago Primer pago Segundo pago
Enero 85€ 95€
Febrero 75€ 55€
Marzo 165€ 125€
``` ### Tabla con tfoot [#](#tabla-con-tfoot) Mostrar códigodel ejemplo: Tabla con tfoot #### Contenido Nunjucks macro HTML ##### Nunjucks macro ```js {% from "components/table/_macro.table.njk" import componentTable %} {{ componentTable({ "caption": "Caption de la tabla", "captionClasses": "sr-only mb-base font-bold text-left text-lg", "head": [ { "text": "Mes de pago" }, { "text": "Primer pago", "classes": "lg:text-right" }, { "text": "Segundo pago", "classes": "lg:text-right" } ], "rows": [ [ { "text": "Enero" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" } ], [ { "text": "Febrero" }, { "text": "75€", "classes": "lg:text-right" }, { "text": "55€", "classes": "lg:text-right" } ], [ { "text": "Marzo" }, { "text": "165€", "classes": "lg:text-right" }, { "text": "125€", "classes": "lg:text-right" } ] ], "foot": [ { "text": "Suma", "classes": "uppercase" }, { "text": "325€", "classes": "lg:text-right" }, { "text": "275€", "classes": "lg:text-right" } ] }) }} ``` ##### HTML ```html
Caption de la tabla
Mes de pago Primer pago Segundo pago
Enero 85€ 95€
Febrero 75€ 55€
Marzo 165€ 125€
Suma 325€ 275€
``` ### Tabla con caption visible [#](#tabla-con-caption-visible) Este ejemplo con caption visible se considera tabla compleja a nivel de accesibilidad. Así que tendría que tener una descripción con un details asociado a la tabla con el atributo aria-describedby. Mostrar códigodel ejemplo: Tabla con caption visible #### Contenido Nunjucks macro HTML ##### Nunjucks macro ```js {% from "components/table/_macro.table.njk" import componentTable %} {{ componentTable({ "caption": "Caption 1: Meses y pagos", "captionClasses": "mb-base font-bold text-left text-lg", "firstCellIsHeader": true, "head": [ { "text": "Mes de pago" }, { "text": "Primer pago", "classes": "lg:text-right" }, { "text": "Segundo pago", "classes": "lg:text-right" } ], "rows": [ [ { "text": "Enero" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" } ], [ { "text": "Febrero" }, { "text": "75€", "classes": "lg:text-right" }, { "text": "55€", "classes": "lg:text-right" } ], [ { "text": "Marzo" }, { "text": "165€", "classes": "lg:text-right" }, { "text": "125€", "classes": "lg:text-right" } ] ] }) }} ``` ##### HTML ```html
Caption 1: Meses y pagos
Mes de pago Primer pago Segundo pago
Enero 85€ 95€
Febrero 75€ 55€
Marzo 165€ 125€
``` ### Tabla con wrapper [#](#tabla-con-wrapper) Usa el parámetro `wrapper` para rodear la tabla con un `
` y poder añadir clases, normalmente para hacer scroll horizontal en la tabla en anchuras pequeñas. Por accesibilidad, parámetro wrapper necesitará los atributos: tabindex=0, role=section y un aria-label o aria-labelledby. Mostrar códigodel ejemplo: Tabla con wrapper #### Contenido Nunjucks macro HTML ##### Nunjucks macro ```js {% from "components/table/_macro.table.njk" import componentTable %} {{ componentTable({ "caption": "Caption 1: Meses y pagos", "captionClasses": "sticky left-0 top-0 w-0 mb-base font-bold text-left text-lg lg:whitespace-nowrap", "firstCellIsHeader": true, "head": [ { "text": "Mes de pago", "classes": "lg:whitespace-nowrap" }, { "text": "Primer pago", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Segundo pago", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for A", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for B", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for C", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for D", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for E", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for F", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for G", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for H", "classes": "lg:whitespace-nowrap lg:text-right" } ], "rows": [ [ { "text": "Enero" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" } ], [ { "text": "Febrero" }, { "text": "75€", "classes": "lg:text-right" }, { "text": "55€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" } ], [ { "text": "Marzo" }, { "text": "165€", "classes": "lg:text-right" }, { "text": "125€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" } ] ], "wrapper": { "classes": "relative overflow-x-auto pb-base focus:outline-hidden focus:shadow-outline-black", "attributes": { "tabindex": "0", "role": "region", "aria-label": "Tabla con scroll del ejemplo Tabla con wrapper" } } }) }} ``` ##### HTML ```html
Caption 1: Meses y pagos
Mes de pago Primer pago Segundo pago Rate for A Rate for B Rate for C Rate for D Rate for E Rate for F Rate for G Rate for H
Enero 85€ 95€ 85€ 95€ 85€ 95€ 85€ 95€ 85€ 95€
Febrero 75€ 55€ 85€ 95€ 85€ 95€ 85€ 95€ 85€ 95€
Marzo 165€ 125€ 85€ 95€ 85€ 95€ 85€ 95€ 85€ 95€
``` ### Tabla no responsive [#](#tabla-no-responsive) Usa la clase modificadora `.c-table--no-responsive` junto al parámetro `wrapper` para tablas que usen rowspan o colspan o tablas que no queremos que muestren sus celdas apiladas en móvil. Mostrar códigodel ejemplo: Tabla no responsive #### Contenido Nunjucks macro HTML ##### Nunjucks macro ```js {% from "components/table/_macro.table.njk" import componentTable %} {{ componentTable({ "classes": "c-table--no-responsive w-full", "caption": "Caption 1: Meses y pagos", "captionClasses": "mb-base font-bold text-left text-lg", "firstCellIsHeader": true, "head": [ { "text": "Mes de pago", "classes": "lg:whitespace-nowrap" }, { "text": "Primer pago", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Segundo pago", "classes": "lg:whitespace-nowrap lg:text-right" } ], "rows": [ [ { "text": "Enero" }, { "text": "85€", "classes": "text-center", "rowspan": 2, "colspan": 2 } ], [ { "text": "Febrero" } ], [ { "text": "Marzo" }, { "text": "165€", "classes": "lg:text-right" }, { "text": "125€", "classes": "lg:text-right" } ] ], "wrapper": { "classes": "relative overflow-x-auto pb-base focus:outline-hidden focus:shadow-outline-black", "attributes": { "tabindex": "0", "role": "region", "aria-label": "Tabla con scroll del ejemplo Tabla no responsive" } } }) }} ``` ##### HTML ```html
Caption 1: Meses y pagos
Mes de pago Primer pago Segundo pago
Enero 85€
Febrero
Marzo 165€ 125€
``` ### Tabla con columnas sticky [#](#tabla-con-columnas-sticky) En este ejemplo la primera y última columna se mantienen fijas y el resto hacen scroll horizontal si la anchura es pequeña. Mostrar códigodel ejemplo: Tabla con columnas sticky #### Contenido Nunjucks macro HTML ##### Nunjucks macro ```js {% from "components/table/_macro.table.njk" import componentTable %} {{ componentTable({ "caption": "Caption 1: Meses y pagos", "captionClasses": "sticky top-0 left-0 w-0 mb-base font-bold text-left text-lg", "firstCellIsHeader": true, "head": [ { "text": "Mes de pago", "classes": "lg:whitespace-nowrap sticky left-0 w-60 bg-white" }, { "text": "Primer pago", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Segundo pago", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for A", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for B", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for C", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for D", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for E", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for F", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for G", "classes": "lg:whitespace-nowrap lg:text-right" }, { "text": "Rate for H", "classes": "lg:whitespace-nowrap lg:text-right sticky right-0 w-60 bg-white" } ], "rows": [ [ { "text": "Enero", "classes": "sticky left-0 w-60 bg-white" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right sticky right-0 w-60 bg-white" } ], [ { "text": "Febrero", "classes": "sticky left-0 w-60 bg-white" }, { "text": "75€", "classes": "lg:text-right" }, { "text": "55€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right sticky right-0 w-60 bg-white" } ], [ { "text": "Marzo", "classes": "sticky left-0 w-60 bg-white" }, { "text": "165€", "classes": "lg:text-right" }, { "text": "125€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right" }, { "text": "85€", "classes": "lg:text-right" }, { "text": "95€", "classes": "lg:text-right sticky right-0 w-60 bg-white" } ] ], "wrapper": { "classes": "overflow-x-auto pb-base focus:outline-hidden focus:shadow-outline-black", "attributes": { "tabindex": "0", "role": "region", "aria-label": "Tabla con scroll del ejemplo Tabla con columnas sticky" } }, "classes": "relative lg:whitespace-nowrap" }) }} ``` ##### HTML ```html
Caption 1: Meses y pagos
Mes de pago Primer pago Segundo pago Rate for A Rate for B Rate for C Rate for D Rate for E Rate for F Rate for G Rate for H
Enero 85€ 95€ 85€ 95€ 85€ 95€ 85€ 95€ 85€ 95€
Febrero 75€ 55€ 85€ 95€ 85€ 95€ 85€ 95€ 85€ 95€
Marzo 165€ 125€ 85€ 95€ 85€ 95€ 85€ 95€ 85€ 95€
```