datatables auto load column

本文介绍了一个使用jQuery和Ajax实现的动态加载DataTable的方法。当用户选择不同的视图时,通过Ajax请求从服务器获取相应数据并更新DataTable。该过程涉及销毁旧表、设置新的列头、加载新数据等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

$('.button').click(function (event) {
        event.preventDefault();
 
        if ($('#GroupElement_Id option:selected').val() != '') {
 
            var viewname = $('#GroupElement_Id option:selected').val();
 
            //  if data-table has been initialized then destroy it
            if ($('#gem-table').hasClass('dataTable')) {
                var table = $('#gem-table').DataTable();
                //  remove all rows. important to call before destroy.
                table.clear();
                //  remove those enhancements and return the table to its original un-enhanced state
                //  call if create new tables based on new criteria with different initialisation settings
                table.destroy();
                //  empty in case the columns change
                $('#gem-table thead tr').empty();
                $('#gem-table tbody').empty();
                $('#gem-table tfoot tr').empty();
            }
 
            //  Ajax call that gets the data
            $.ajax({
                type: 'GET',
                contentType: 'application/json; charset=utf-8',
                url: apiBaseUmbraco + 'gem/gemdataserverside/',
                data:
                    {
                        'viewname': viewname,
                        'all': $('#all-gem-rows').is(':checked') ? 1 : 0
                    },
                dataType: 'JSON',
                beforeSend: function () {
                    $('.ajax-loader').show();
                },
                success: function (response) {
                    var tableColumns = jQuery.parseJSON(response).columns;
                    var columns = [];
                    var headers = '';
                    var footers = '';
 
                    //  build up table header, footer
                    //  and columns JSON array for the data-table
                    $.each(tableColumns, function (index, value) {
                        headers += '<th>' + value + '</th>';
                        footers += '<th>' + value + '</th>';
                        var headerObj = new Object();
                        headerObj['sName'] = value;
                        columns.push(headerObj);
                    });
 
                    //  insert header and footer into the table
                    $('#gem-table thead tr').html(headers);
                    $('#gem-table tfoot tr').html(footers);
 
                    $('#gem-table').dataTable({
                        'bServerSide': true,
                        'sAjaxSource': apiBaseUmbraco + 'gem/gemdataserverside/?viewname=' + viewname + '&all=' + ($('#all-gem-rows').is(':checked') ? '1' : '0'),
                        'bProcessing': true,
                        'aoColumns': columns,
                        dom: 'T<"clear">lfrtip',
                        'tableTools': {
                            'sSwfPath': '/swf/copy_csv_xls_pdf.swf'
                        },
                        'responsive': true,
                        'lengthMenu': [[25, 50, 100, -1], [25, 50, 100, 'All']],
                        'drawCallback': function (settings) {
                            $('.ajax-loader').hide();
                        },
                         
                    });
 
                    //  scroll down to data-table
                    $('html, body').animate({
                        scrollTop: $('#gem-table_wrapper').offset().top
                    }, 1000);
 
                },
                error: function (result) {
                    alert('Service call failed: ' + result.status + ' Type :' + result.statusText);
                }
            });
 
        } else {
 
        }
    });

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值