You are here

expandable-rows-table.html.twig in JSON:API Extras 8.3

Same filename and directory in other branches
  1. 8.2 templates/expandable-rows-table.html.twig

Theme override to display a table. The main difference between this template and the original table template is that here we are printing the last three columns of each row inside a secondary, collapsible row.

Available variables:

  • attributes: HTML attributes to apply to the <table> tag.
  • caption: A localized string for the <caption> tag.
  • colgroups: Column groups. Each group contains the following properties:
  • header: Table header cells. Each cell contains the following properties:
    • tag: The HTML tag name to use; either 'th' or 'td'.
    • attributes: HTML attributes to apply to the tag.
    • content: A localized string for the title of the column.
    • field: Field name (required for column sorting).
    • sort: Default sort order for this column ("asc" or "desc").
  • sticky: A flag indicating whether to use a "sticky" table header.
  • rows: Table rows. Each row contains the following properties:
    • attributes: HTML attributes to apply to the <tr> tag.
    • data: Table cells.
    • no_striping: A flag indicating that the row should receive no 'even / odd' styling. Defaults to FALSE.
    • cells: Table cells of the row. Each cell contains the following keys:
      • tag: The HTML tag name to use; either 'th' or 'td'.
      • attributes: Any HTML attributes, such as "colspan", to apply to the table cell.
      • content: The string to display in the table cell.
      • active_table_sort: A boolean indicating whether the cell is the active table sort.
  • footer: Table footer rows, in the same format as the rows variable.
  • empty: The message to display in an extra row if table does not have any rows.
  • no_striping: A boolean indicating that the row should receive no striping.
  • header_columns: The number of columns in the header.
  • toggler_text: Text of the link that shows/hides the colapsible row

File

templates/expandable-rows-table.html.twig
View source
  1. {#
  2. /**
  3. * @file
  4. * Theme override to display a table.
  5. * The main difference between this template and the original table template is that here
  6. * we are printing the last three columns of each row inside a secondary, collapsible row.
  7. *
  8. * Available variables:
  9. * - attributes: HTML attributes to apply to the <table> tag.
  10. * - caption: A localized string for the <caption> tag.
  11. * - colgroups: Column groups. Each group contains the following properties:
  12. * - attributes: HTML attributes to apply to the <col> tag.
  13. * Note: Drupal currently supports only one table header row, see
  14. * https://www.drupal.org/node/893530 and
  15. * http://api.drupal.org/api/drupal/includes!theme.inc/function/theme_table/7#comment-5109.
  16. * - header: Table header cells. Each cell contains the following properties:
  17. * - tag: The HTML tag name to use; either 'th' or 'td'.
  18. * - attributes: HTML attributes to apply to the tag.
  19. * - content: A localized string for the title of the column.
  20. * - field: Field name (required for column sorting).
  21. * - sort: Default sort order for this column ("asc" or "desc").
  22. * - sticky: A flag indicating whether to use a "sticky" table header.
  23. * - rows: Table rows. Each row contains the following properties:
  24. * - attributes: HTML attributes to apply to the <tr> tag.
  25. * - data: Table cells.
  26. * - no_striping: A flag indicating that the row should receive no
  27. * 'even / odd' styling. Defaults to FALSE.
  28. * - cells: Table cells of the row. Each cell contains the following keys:
  29. * - tag: The HTML tag name to use; either 'th' or 'td'.
  30. * - attributes: Any HTML attributes, such as "colspan", to apply to the
  31. * table cell.
  32. * - content: The string to display in the table cell.
  33. * - active_table_sort: A boolean indicating whether the cell is the active
  34. table sort.
  35. * - footer: Table footer rows, in the same format as the rows variable.
  36. * - empty: The message to display in an extra row if table does not have
  37. * any rows.
  38. * - no_striping: A boolean indicating that the row should receive no striping.
  39. * - header_columns: The number of columns in the header.
  40. * - toggler_text: Text of the link that shows/hides the colapsible row
  41. *
  42. * @see template_preprocess_table()
  43. */
  44. #}
  45. <table{{ attributes }}>
  46. {% if caption %}
  47. <caption>{{ caption }}</caption>
  48. {% endif %}
  49. {% for colgroup in colgroups %}
  50. {% if colgroup.cols %}
  51. <colgroup{{ colgroup.attributes }}>
  52. {% for col in colgroup.cols %}
  53. <col{{ col.attributes }} />
  54. {% endfor %}
  55. </colgroup>
  56. {% else %}
  57. <colgroup{{ colgroup.attributes }} />
  58. {% endif %}
  59. {% endfor %}
  60. {% if header %}
  61. <thead>
  62. <tr>
  63. {% for cell in header %}
  64. {%
  65. set cell_classes = [
  66. cell.active_table_sort ? 'is-active',
  67. ]
  68. %}
  69. <{{ cell.tag }}{{ cell.attributes.addClass(cell_classes) }}>
  70. {{- cell.content -}}
  71. </{{ cell.tag }}>
  72. {% endfor %}
  73. </tr>
  74. </thead>
  75. {% endif %}
  76. {% if rows %}
  77. <tbody>
  78. {% for row in rows %}
  79. {%
  80. set row_classes = [
  81. not no_striping ? cycle(['odd', 'even'], loop.index0),
  82. ]
  83. %}
  84. <tr{{ row.attributes.addClass(row_classes) }}>
  85. {% for cell in row.cells %}
  86. {% if loop.index < row.cells|length - 3 %}
  87. <{{ cell.tag }}{{ cell.attributes }}>
  88. {{- cell.content -}}
  89. </{{ cell.tag }}>
  90. {% elseif loop.index == row.cells|length - 3 %}
  91. <{{ cell.tag }}{{ cell.attributes }}>
  92. <a class="toggle-expanded content-collapsed button button--small" data-open="adv-opt-{{ loop.parent.loop.index }}">
  93. <span></span>
  94. {{- cell.content -}}
  95. </a>
  96. </{{ cell.tag }}>
  97. {% endif %}
  98. {% endfor %}
  99. </tr>
  100. <tr {{ row.attributes.addClass(row_classes).addClass(row_classes).addClass('advanced-opts') }} id="adv-opt-{{ loop.index }}">
  101. {% for cell in row.cells %}
  102. {% if loop.index >= row.cells|length - 2 %}
  103. <{{ cell.tag }}{{ cell.attributes }}>
  104. {{- cell.content -}}
  105. </{{ cell.tag }}>
  106. {% endif %}
  107. {% endfor %}
  108. </tr>
  109. {% endfor %}
  110. </tbody>
  111. {% elseif empty %}
  112. <tbody>
  113. <tr class="odd">
  114. <td colspan="{{ header_columns }}" class="empty message">{{ empty }}</td>
  115. </tr>
  116. </tbody>
  117. {% endif %}
  118. {% if footer %}
  119. <tfoot>
  120. {% for row in footer %}
  121. <tr{{ row.attributes }}>
  122. {% for cell in row.cells %}
  123. <{{ cell.tag }}{{ cell.attributes }}>
  124. {{- cell.content -}}
  125. </{{ cell.tag }}>
  126. {% endfor %}
  127. </tr>
  128. {% endfor %}
  129. </tfoot>
  130. {% endif %}
  131. </table>