datatables_extension_fixed_header.js 3.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/* ------------------------------------------------------------------------------
*
*  # Fixed Header extension for Datatables
*
*  Specific JS code additions for datatable_extension_fixed_header.html page
*
*  Version: 1.1
*  Latest update: Jan 5, 2017
*
* ---------------------------------------------------------------------------- */

$(function() {


    // Table setup
    // ------------------------------

    // Setting datatable defaults
    $.extend( $.fn.dataTable.defaults, {
        autoWidth: false,
        columnDefs: [{ 
            orderable: false,
            targets: [ 5 ]
        }],
        dom: '<"datatable-header"fl><"datatable-scroll-wrap"t><"datatable-footer"ip>',
        language: {
            search: '<span>Filter:</span> _INPUT_',
            lengthMenu: '<span>Show:</span> _MENU_',
            paginate: { 'first': 'First', 'last': 'Last', 'next': '&rarr;', 'previous': '&larr;' }
        },
        drawCallback: function () {
            $(this).find('tbody tr').slice(-3).find('.dropdown, .btn-group').addClass('dropup');
        },
        preDrawCallback: function() {
            $(this).find('tbody tr').slice(-3).find('.dropdown, .btn-group').removeClass('dropup');
        }
    });


    // Basic initialization
    var table_basic = $('.datatable-header-basic').DataTable({
        fixedHeader: true
    });


    // Header and footer fixed
    var table_footer = $('.datatable-header-footer').DataTable({
        fixedHeader: {
            header: true,
            footer: true
        }
    });


    // Offset
    var table_offset = $('.datatable-header-offset').DataTable({
        fixedHeader: {
            header: true,
            
        }
    });

    // Init offset toggle
    var toggleType = document.querySelector('.toggle-offset');
    var toggleTypeInit = new Switchery(toggleType, { secondaryColor: '#FF7043'});

    // Toggle offset and fixed navbar
    toggleType.onchange = function() {
        if(toggleType.checked) {

            // Toggle necessary body and navbar classes
            $('body').children('.navbar').first().addClass('navbar-fixed-top');
            $('body').addClass('navbar-top');

            // Add offset to all
            table_basic.fixedHeader.headerOffset($('.navbar-fixed-top').height());
            table_footer.fixedHeader.headerOffset($('.navbar-fixed-top').height());
            table_reorder.fixedHeader.headerOffset($('.navbar-fixed-top').height());
            table_offset.fixedHeader.headerOffset($('.navbar-fixed-top').height());
        }
        else {

            // Toggle necessary body and navbar classes
            $('body').children('.navbar').first().removeClass('navbar-fixed-top');
            $('body').removeClass('navbar-top');

            // Remove offset from all
            table_basic.fixedHeader.headerOffset(0);
            table_footer.fixedHeader.headerOffset(0);
            table_reorder.fixedHeader.headerOffset(0);
            table_offset.fixedHeader.headerOffset(0);
        }
    };


    // ColReorder integration
    var table_reorder = $('.datatable-header-reorder').DataTable({
        fixedHeader: true,
        colReorder: true
    });


    // Adjust table header if sidebar toggler is clicked
    $('.sidebar-control').on('click', function() {
        table_basic.fixedHeader.adjust();
        table_footer.fixedHeader.adjust();
        table_offset.fixedHeader.adjust();
        table_reorder.fixedHeader.adjust();
    });


    // External table additions
    // ------------------------------

    // Add placeholder to the datatable filter option
    $('.dataTables_filter input[type=search]').attr('placeholder','Type to filter...');


    // Enable Select2 select for the length option
    $('.dataTables_length select').select2({
        minimumResultsForSearch: Infinity,
        width: 'auto'
    });
    
});