forked from CollectionBuilder/collectionbuilder-csv
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
5 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.