Skip to content

Commit

Permalink
move datatables js to only where it's needed
Browse files Browse the repository at this point in the history
and fix an error in non-development environments, where the datatables lib was not loaded
  • Loading branch information
peterstadler committed Aug 24, 2022
1 parent 366f0ee commit 21b3e17
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
31 changes: 0 additions & 31 deletions resources/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -1128,37 +1128,6 @@ $('.copy-to-clipboard').on('click', function() {
$('#newID-result span').tooltip('show');
});

$('#datatables')
.on('xhr.dt', function ( e, settings, json, xhr ) {
//console.log(xhr.getAllResponseHeaders());
json.recordsTotal = xhr.getResponseHeader('totalRecordCount')
json.recordsFiltered = xhr.getResponseHeader('filteredrecordcount')
} )
.DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
url: $('#datatables').attr('data-api-base') + "/repositories/items?siglum=D-B",
cache: true,
dataSrc: '',
data: function(d) {
//console.log(d)
return {
limit: d.length,
offset: d.start +1,
orderby: d.columns[d.order[0].column].name,
orderdir: d.order[0].dir
}
}
},
"columns": [
{ "data": "docID", "name": "docID", "orderable": true, "render": function(data, type, row, meta) { return '<a href="https://dev.weber-gesamtausgabe.de/' + data + '">' + data + '</a>'; } },
{ "data": "related_entities", "name": "author", "orderable": false, "render": function(data, type, row, meta) { var authors = []; data.forEach(function(item) { if(item.rel==='author') { authors.push( item.name ) }}); return authors.join('; ') } },
{ "data": "date", "name": "sortdate", "orderable": true },
{ "data": "title", "name": "title", "orderable": true },
{ "data": "idno", "name": "idno", "orderable": true }
]
} );

/*
* Initialise line wrap toggle for XML previews
Expand Down
47 changes: 46 additions & 1 deletion templates/dev/datatables.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,49 @@ <h1 class="document">Datatables</h1>
</tfoot>
</table>
</div>
</div>
<script>
function defer(method) {
if (window.jQuery) {
method();
} else {
setTimeout(function() { defer(method) }, 50);
}
}

function initDataTables() {
$('#datatables')
.on('xhr.dt', function ( e, settings, json, xhr ) {
//console.log(xhr.getAllResponseHeaders());
json.recordsTotal = xhr.getResponseHeader('totalRecordCount')
json.recordsFiltered = xhr.getResponseHeader('filteredrecordcount')
} )
.DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
url: $('#datatables').attr('data-api-base') + "/repositories/items?siglum=D-B",
cache: true,
dataSrc: '',
data: function(d) {
//console.log(d)
return {
limit: d.length,
offset: d.start +1,
orderby: d.columns[d.order[0].column].name,
orderdir: d.order[0].dir
}
}
},
"columns": [
{ "data": "docID", "name": "docID", "orderable": true, "render": function(data, type, row, meta) { return '<a href="https://dev.weber-gesamtausgabe.de/' + data + '">' + data + '</a>'; } },
{ "data": "related_entities", "name": "author", "orderable": false, "render": function(data, type, row, meta) { var authors = []; data.forEach(function(item) { if(item.rel==='author') { authors.push( item.name ) }}); return authors.join('; ') } },
{ "data": "date", "name": "sortdate", "orderable": true },
{ "data": "title", "name": "title", "orderable": true },
{ "data": "idno", "name": "idno", "orderable": true }
]
} )
}

defer(initDataTables);
</script>
</div>

0 comments on commit 21b3e17

Please sign in to comment.