ckanext-scheming
ckanext-scheming copied to clipboard
Table view snippet for repeating fields.
PR #203 adds support for repeating composite fields. The following is a very simple patch that adds support for parent-level display snippets of repeating and composite fields so containers other than the default dl/dd can be used. In this case, it adds a table.html snippet that will render all of its children in a table. Columns with a display_snippet: null will be excluded, and the display snippet will be used to render the cell content like normal.

It should be extended to use the helper for checking for empty fields to prevent rendering a table with no content.
diff --git a/ckanext/scheming/templates/scheming/display_snippets/table.html b/ckanext/scheming/templates/scheming/display_snippets/table.html
new file mode 100644
index 0000000..eeebf06
--- /dev/null
+++ b/ckanext/scheming/templates/scheming/display_snippets/table.html
@@ -0,0 +1,33 @@
+<table class="table table-striped">
+ <thead>
+ <tr>
+ {% for subfield in field.subfields %}
+ {% if subfield.display_snippet %}
+ <th nowrap>{{ h.scheming_language_text(subfield.label) }}</th>
+ {% endif %}
+ {% endfor %}
+ </tr>
+ </thead>
+ <tbody>
+ {% for group in data %}
+ <tr>
+ {% for subfield in field.subfields %}
+ {% set options = subfield.display_snippet_options|default({}) %}
+ {% if group[subfield.field_name] %}
+ {% if options.row_header %}<th scope="row">{% else %}<td>{% endif %}
+ {%- snippet 'scheming/snippets/display_field.html',
+ field=subfield,
+ data=group,
+ errors=errors,
+ entity_type=entity_type,
+ object_type=object_type
+ -%}
+ {% if options.row_header %}</th>{% else %}</td>{% endif %}
+ {% else %}
+ <td></td>
+ {% endif %}
+ {% endfor %}
+ </tr>
+ {% endfor %}
+ </tbody>
+</table>
diff --git a/ckanext/scheming/templates/scheming/snippets/display_field.html b/ckanext/scheming/templates/scheming/snippets/display_field.html
index 9061e00..b9e86ee 100644
--- a/ckanext/scheming/templates/scheming/snippets/display_field.html
+++ b/ckanext/scheming/templates/scheming/snippets/display_field.html
@@ -1,8 +1,21 @@
{#- master snippet for all scheming display fields -#}
{#- render the field the user requested, or use a default field -#}
{%- if field.subfields %}
- {% set group_data = h.scheming_composite_load(data[field.field_name]) %}
+ {% set group_data = h.scheming_composite_load(data[field.field_name]) %}
+ {% if field.display_snippet %}
+ {%- set display_snippet = field.display_snippet -%}
+ {%- if '/' not in display_snippet -%}
+ {%- set display_snippet = 'scheming/display_snippets/' + display_snippet -%}
+ {%- endif -%}
+ {%- snippet display_snippet,
+ field=field,
+ data=group_data,
+ errors=errors,
+ entity_type=entity_type,
+ object_type=object_type
+ -%}
+ {% else %}
{% for group in group_data %}
<dl class="composite-repeating-list">
{%- for subfield in field.subfields %}
@@ -23,6 +36,7 @@
{% endfor -%}
</dl>
{% endfor %}
+ {% endif %}
{% else %}
{%- set display_snippet = field.display_snippet -%}