Skip to content

Instantly share code, notes, and snippets.

@thclark
Created January 11, 2019 08:33
Show Gist options
  • Save thclark/13794cf4ba5cea3c472dcad9f4039262 to your computer and use it in GitHub Desktop.
Save thclark/13794cf4ba5cea3c472dcad9f4039262 to your computer and use it in GitHub Desktop.
Jijnja2 Block for rendering a table with django-datatables-view
{% block view_scripts %}
<script type="text/javascript">
var MANIFEST_TABLE_URL = '{{ reverse('manifests:manifest-files-table', kwargs={'manifest_id': object.manifest.id if object.manifest else 'none'}) }}';
</script>
<script>
var table2 = $('#manifestfilestable').DataTable({
language: dt_language, // global variable defined in html
order: [[ 0, "desc" ]],
lengthMenu: [[10, 25, 50, 100, 200], [10, 25, 50, 100, 200]],
columnDefs: [
{orderable: true,
searchable: true,
className: "center",
targets: [0, 1, 2, 3, 4],
defaultContent: "-"
}
],
responsive: true,
colReorder: true,
scrollX: true,
pagingType: "full_numbers",
searching: true,
processing: true,
serverSide: true,
stateSave: true,
ajax: MANIFEST_TABLE_URL
});
new $.fn.dataTable.Buttons( table2, {
buttons: [
'columnsToggle'
]
} );
// Put the column visibility buttons into a collapsible element
table2.buttons(1).container().appendTo( $('#columnToggleButtons2') );
table2.AdjustColumnSizing(true);
</script>
<script type="text/javascript">
$(document).ready(function() {
activaTab('manage');
$('.dataTables_scrollBody').css('margin-bottom', '20px').css('border-bottom', '2px solid #ddd');
{# When datatables are initialised in hidden eelements, their columns aren't sized correctly, as the container has an undefined size. Trigger a width update when the tab is shown #}
$('a[data-toggle="tab"]').on( 'shown.bs.tab', function (e) {
$.fn.dataTable.tables( {visible: true, api: true} ).columns.adjust();
} );
})
</script>
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment