数据表:动态列和大量数据

时间:2016-12-01 09:32:56

标签: javascript java jquery ajax datatables

我正在使用带有ajax数据请求(json格式)的datatables和动态列加载。这是我的实际代码:

var tableData;
var tableName='#iuprTable';

$(document).ready(function (){

    jQuery.ajax({
        type : "GET",
        url : "table/"+document.getElementById("hiddenIdCar").value,
        headers: {'Content-Type': 'application/json'},  
        beforeSend: function() {
            waitingModal.showPleaseWait();
        },
        success: function(data,status,xhr){
            //No exception occurred
            if(data.success){
                tableData = data.result;
                $.each(tableData.columns, function (k, colObj) {
                    var str = '<th data-html="true" data-toggle="tooltip" title="<b>NAME</b>:'+ colObj.parName + '<br><b>DESCRIPTION</b>:' + colObj.description + '<br><b>NOTE</b>: ' + colObj.note + '"></i>' + colObj.parName + '</th>';
                    $(str).appendTo(tableName+'>thead>tr');
                });
            } else {
                waitingModal.hidePleaseWait();
                notifyMessage(data.result, 'error');
            }
        },
        error: function(xhr,status,e){
            window.location.href = "/500";
        }
    }).complete(function() {
        //initialize datatable
        if ( ! $.fn.DataTable.isDataTable( tableName ) ) {
            var iuprTable = $(tableName).DataTable({
                scrollCollapse: true,
                scrollX:        true,
                scrollY:        '60vh',
                paging:         false,
                "data": tableData.data,
                "columns": tableData.columns,
                fixedColumns:   {
                    leftColumns: 4
                },
                "initComplete": function(settings){
                    //TOOLTIP cell
                    $(tableName+ ' tbody td').each( function (k, cellObj){
                        this.setAttribute( 'title', cellObj.innerText );
                        this.setAttribute( 'data-toggle', "tooltip" );
                    });
                    $('[data-toggle="tooltip"]').tooltip({
                        container: 'body'
                    }); 
                    //add timeout because otherwise user could see a too fast waiting modal
                    setTimeout(function(){
                        waitingModal.hidePleaseWait();
                    }, 1000);       
                } 
            });
        }
        else {
            iuprTable.ajax.url("table/"+document.getElementById("hiddenIdCar").value).load();
        }
    }); 
});

function notifyMessage(textMessage, typeMessage){
    $.bootstrapGrowl(textMessage, {
        type: typeMessage, // (null, 'info', 'error', 'success')
    });
}

由于我可能有300行和300列,因此使用此数字页面会变慢。我读到了服务器端处理,我想知道如何提高性能。 我找到了2个解决方案:

1)服务器端处理:向服务器询问仅显示的行

2)客户端:json返回所有行,但只有部分行显示在特定时间,当用户滚动数据时更新(初始加载后页面更快)。

第一个解决方案比客户端处理最复杂,因为我应该更改Web服务。你对我有什么建议吗?我该如何实施这些解决方案?感谢

1 个答案:

答案 0 :(得分:0)

使用服务器端分页,限制和偏移量为每页50个

使用相应的引荐列索引您的数据库,这将确保提高性能..

如果可能,也可以使用缓存查询,如memcached等

相关问题