Skip to content

Commit

Permalink
Tabled item (#143)
Browse files Browse the repository at this point in the history
* add table view

* use datatable

* fix typo

* feat: table layout and styling

* fix csv parsing with papaparse

---------

Co-authored-by: mtwente <[email protected]>
  • Loading branch information
koilebeit and mtwente authored Jul 18, 2024
1 parent 422eb70 commit c3dd8b0
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 2 deletions.
2 changes: 2 additions & 0 deletions _includes/item/child/compound-item-modal-gallery.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ <h3 class="h5">{{ page.title }}
</div>
{% elsif child.display_template == 'panorama' %}
{% include item/child/panorama.html %}
{% elsif child.display_template == 'record' AND child.format == 'text/csv' %}
{% include item/child/table.html %}
{% else %}
{% include item/child/item-thumb.html %}
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion _includes/item/child/download-buttons.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<a href="{{ year | strip | prepend: '/timeline.html#y' | relative_url }}" class="btn btn-outline-primary">{{ site.data.translations['_includes']['item']['child']['download-buttons.html']['timeline'][site.lang] | default: "View on Timeline"}}</a>{% endif %}
{% if child.latitude and child.longitude %}
<a href="{{ '/map.html?location=' | append: page.latitude | append: ',' | append: page.longitude | append: '&marker=' | append: page.objectid | relative_url }}" class="btn btn-outline-primary">{{ site.data.translations['_includes']['item']['child']['download-buttons.html']['map'][site.lang] | default: "View on Map" }}</a>{% endif %}{% if child.object_location %}<a target="_blank" rel="noopener" href="{{ child.object_location | relative_url }}" class="btn btn-outline-primary">
{% if child.display_template == 'video' and child.object_location contains 'vimeo' %}{{ site.data.translations['_includes']['item']['child']['download-buttons.html']['vimeo'][site.lang] | default: "View on Vimeo" }}{% elsif child.display_template == 'video' and child.object_location contains 'youtu' %}{{ site.data.translations['_includes']['item']['child']['download-buttons.html']['youtube'][site.lang] | default: "View on YouTube" }}e{% elsif child.display_template == 'record'%}{{ site.data.translations['_includes']['item']['child']['download-buttons.html']['link-to-object'][site.lang] | default: "Link to Object" }}{% else %}Download {{ child.format | split: '/' | last | default: page.display_template | upcase }}{% endif %}
{% if child.display_template == 'video' and child.object_location contains 'vimeo' %}{{ site.data.translations['_includes']['item']['child']['download-buttons.html']['vimeo'][site.lang] | default: "View on Vimeo" }}{% elsif child.display_template == 'video' and child.object_location contains 'youtu' %}{{ site.data.translations['_includes']['item']['child']['download-buttons.html']['youtube'][site.lang] | default: "View on YouTube" }}{% else %}Download {{ child.format | split: '/' | last | default: page.display_template | upcase }}{% endif %}
</a>{% endif %}
<a class="btn btn-report d-flex align-items-center" href="mailto:{{ site.report-email }}?subject={{ site.data.translations['_includes']['item']['download-buttons.html']['subject'][site.lang] | default: 'Report faulty metadata for'}} {{ child.title | escape }}&body={{ site.data.translations['_includes']['item']['download-buttons.html']['body'][site.lang] | default: 'Please describe the issue with the metadata for'}} {{ child.title | escape }}.%0D%0A%0D%0A- URL: {{ site.url | append: page.url | url_encode }}#{{ child.objectid }}%0D%0A- ID: {{ child.objectid | escape }}">{{ site.data.translations['_includes']['item']['download-buttons.html']['report'][site.lang] | default: "Report faulty metadata" }}</a>

Expand Down
75 changes: 75 additions & 0 deletions _includes/item/child/table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{% comment %}

{% endcomment %}

{% assign data = child.object_location %}
{% assign lang = site.lang %}

<link rel="stylesheet" type="text/css" href="/assets/lib/datatables/datatables.min.css">
<script type="text/javascript" language="javascript" src="{{ '/assets/lib/datatables/datatables.min.js' | relative_url }}"></script>
<script type="text/javascript" language="javascript" src="{{ '/assets/lib/papaparse/papaparse.min.js' | relative_url }}"></script>

<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
var obj_url = '{{ data | default: 'null' }}';

if (obj_url !== 'null') {
fetch(obj_url)
.then(response => response.text())
.then(data => {
Papa.parse(data, {
header: true,
skipEmptyLines: true,
transform: function(value, field) {
// Ensure undefined values are replaced with empty strings
return value === undefined ? "" : value;
},
complete: function(results) {
var build = '<table id="csvTable" class="table table-striped datatable-border" style="width:100%">\n';
var rows = results.data;

if (rows.length > 0) {
build += "<thead><tr>";
var headers = Object.keys(rows[0]);
headers.forEach(header => {
build += "<th scope='col'>" + header + "</th>";
});
build += "</tr></thead><tbody>";

rows.forEach(row => {
build += "<tr>";
headers.forEach(header => {
build += "<td>" + (row[header] !== null && row[header] !== undefined ? row[header] : "") + "</td>";
});
build += "</tr>";
});

build += "</tbody></table>";
document.getElementById('wrap').innerHTML = build;

// Initialize DataTables
$('#csvTable').DataTable({
{% if lang != "en" %}language: { url: '{{ "/assets/lib/datatables/" | append: lang | append: ".json" | relative_url }}' },{% endif %}
"deferRender": true,
dom: '<<"col-12"l>>t<"row"<"col-md-6"><"col-md-6"p><"col-12">><"footer-container"if>',
paging: false,
ordering: true,
scrollX: true,
scrollCollapse: true,
scrollY: '400px'
});
} else {
console.error('No data found or data format is incorrect.');
}
},
error: function(error) {
console.error('Error parsing CSV:', error);
}
});
})
.catch(error => console.error('Error fetching data:', error));
}
});
</script>

<div id="wrap" class="bg-{{ include.background | default: '#ffe880' }} border datatable-border rounded"></div>
45 changes: 44 additions & 1 deletion _sass/_custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,49 @@ a {
border-bottom-right-radius: 0;
}

.datatable-border {
border-color: #ffe880 !important;
border-width: 6.5px !important;
border-top: none !important;
}

.datatable-border th,
.datatable-border td {
border: none; /* for inner cell borders */
}

#wrap {
max-height: 600px;
overflow-y: auto;
text-align: left;
background-color: #ffe880;
}

.dataTables_wrapper {
overflow-x: auto;
}

#csvTable {
background-color: white;
}

.dataTables_scrollFoot {
position: sticky;
bottom: 0;
background-color: white;
z-index: 999;
display: flex;
justify-content: space-between;
align-items: center;
margin: 0px !important;
}

.footer-container {
display: flex;
justify-content: space-between;
align-items: baseline;
}

#search button:hover svg {
fill: #ffe880;
}
Expand All @@ -159,4 +202,4 @@ a {
&:hover {
fill: #ffe880;
}
}
}
Loading

0 comments on commit c3dd8b0

Please sign in to comment.