hi, my datatable expand is slow how to improve that?

hi, my datatable expand is slow how to improve that?

farhan07farhan07 Posts: 7Questions: 1Answers: 0
edited November 2023 in Free community support
$(".expand-all").click(function(e) {
            e.stopImmediatePropagation();
            var name;
            collapsedGroups = [];
            $(".dataTables_processing").show("fast", function() {
                var _dataSrc = tableNew.rowGroup().dataSrc();
                var tableArray = [];
                var dataSrcArray = Object.keys(_dataSrc);
                var aggregationIndex = 0;
                _dataSrc.forEach(function(_dataSrcIndex) {
                    tableArray.push(tableNew.column(_dataSrcIndex).data())
                })
                //WE DON'T NEED TO EXCECUTE THE CHILDS, SO WE USE THE BELOW VARIABLE TO AVOID THE TIME CONSUME
                for(j=0; j<tableArray.length; j++){
                    aggregationIndex = aggregationIndex +1;
                    //WE NEED TO ITERATE ONLY THE PARENT AND THE LAST CHILD
                    if(aggregationIndex == 1){
                        tableArray[j].unique().each( function ( name, index ) {
                            //WHEN THERE IS MULTIPLE AGGREGATION WE HAVE TO SHOW THE PARENT ROW
                            if(j>0){
                                for(k=0; k< tableArray[0].unique().length;k++){
                                    var parent = tableArray[0][k];
                                    collapsedGroups[parent+name] = false;
                                }
                            }else{
                                collapsedGroups[name] = false; 
                            }
                        } );
                    }
                }
                
                nonNumericColumns(true,table_id,tableNew,groupColumn);
                
                //$('#collapse-all').attr('id','collapse-partial');
                $('.collapse-all').addClass('collapse-partial');
                $('.collapse-all').removeClass('collapse-partial');
                tableNew.draw(false);
                console.log("Enter3");
                /*tableNew.draw(false); */
                $(".dataTables_processing").hide();   
            }); 
        });

function nonNumericColumns(visibility,table_id,table,groupColumn){
        var flagTable;
        //var columns = $('#'+table_id).dataTable().dataTableSettings[0].aoColumns;
        var columns = table.settings()[0].aoColumns;
        var className = "";
        var column = "";

        $.each(columns, function(i,v) { 
            className = v.nTh.className;
            column = v.idx;
            if(!visibility){
                if ((className.indexOf("numeric") < 0) && (v.sTitle != '')) {
                    table.columns( column ).visible( false );
                }
            }else{
                column = v.idx;
                table.columns( column ).visible( true );
            }
        });
        
        //AGGREGATE COLUMNS NOT NEEDED TO SHOW
          if(visibility){
             table.columns( groupColumn ).visible( false );
         }
          //CALL PREHIDDEN WHEN WE EXPAND VIEWS - VISIBILITY TRUE MEANS EXPANDING VIEW
         if(visibility){
             preHidden(table,table_id,true);
         }

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    This section of the FAQ should help, it discusses various techniques to improve performance,

    Colin

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    If it doesn't help to resolve the issue, please link to a test case showing the issue.

    Allan

  • farhan07farhan07 Posts: 7Questions: 1Answers: 0

    sorry allan i not sure to create test case . but i am sure below code to execute number of table rows time so pls optimized that one.
    rowGroup: {
    startRender: function ( rows, group, level) {
    var all;
    if (level === 0) {
    top = group;
    all = group;
    } else {
    // if parent collapsed, nothing to do
    if (!!collapsedGroups[top]) {
    return;
    }
    //THIS IS TO AVOID THE GROUP EMPTY VALUE WHILE COLLAPSE
    if(group==''){
    all = top + '-';
    }else{
    all = top + group;
    }
    }
    var collapsed = !!collapsedGroups[all];
    //var collapsed = true;

                    rows.nodes().each(function (r) {
                        r.style.display = collapsed ? 'none' : '';
                    });
                    var x = 0;
                    var classColumn = "";
                    var checkUncountable = false;
                    var xTd = 1;
                    //loop to get group's total
                    var group_values = new Array();
                    var group_column_values = new Array();
                    //var end_loop = $('#reorder-new').DataTable().columns(':visible').count();
                    var end_loop = $('#'+table_id).DataTable().columns().count();
                    var tableNew = $('#'+table_id).DataTable();
                    var gpIndex = tableNew.column('#gross-profit').index();
                    var jobsIndex = tableNew.column('#jobs').index();
                    //if we have GP, decrease the loop end
                    if(gpIndex != '' && gpIndex != 'undefined') {
                        end_loop = end_loop-1;
                    }
                    //GET THE AGGREAGATE COLUMNS
                    var updatedGroupColumn = groupColumn.toString();
                    //console.log("updatedGroupColumn = "+updatedGroupColumn);
                    var groupColumnArray = updatedGroupColumn.split(',');
                    //ADD THE GROUP IN EACH ITERATION TO CHECK THE PAST GROUP
                    if(level == 0){
                        recentLevelZero.push(group);
                    }else if(level == 1){
                        recentLevelOne.push(group);
                    }else if(level == 2){
                        recentLevelTwo.push(group);
                    }else if(level == 3){
                        recentLevelThree.push(group);
                    }else if(level == 4){
                        recentLevelFour.push(group);
                    }else if(level == 5){
                        recentLevelFive.push(group);
                    }
                    rows.rows({page:'all',search: 'applied'}).data().filter(function(value, index) {
                        x=0;
                        var flagTotal = 0;
    
                        //FIND THE TOTAL OF FIRST LEVEL AGGREAGATION
    
                        if(level == 0){
                            //COMPARE THE CURRENT VALUE WITH THE CURRENT GROUP
                            if(value[groupColumnArray[level]] == group){
                                flagTotal = 1;
                            }
                        //FIND THE TOTAL OF SECOND LEVEL AGGREAGATION
                        }else if(level == 1){
                            //COMPARE THE CURRENT VALUE WITH THE CURRENT GROUP AND ALSO COMPARE THE LEVEL ZERO VALUE WITH CURRENT VALUE
                            if((value[groupColumnArray[level]] == group) && (value[groupColumnArray[level-1]] == recentLevelZero[recentLevelZero.length-1])){
                                flagTotal = 1; 
                            }
                        //FIND THE TOTAL OF THIRD LEVEL AGGREAGATION
                        }else if(level == 2){
                            //COMPARE THE CURRENT VALUE WITH THE CURRENT GROUP AND ALSO COMPARE THE LEVEL ZERO VALUE WITH CURRENT VALUE AND ALSO COMPARE THE LEVEL ONE VALUE WITH CURRENT VALUE 
                            if((value[groupColumnArray[level]] == group) && (value[groupColumnArray[level-1]] == recentLevelOne[recentLevelOne.length-1])&& (value[groupColumnArray[level-2]] == recentLevelZero[recentLevelZero.length-1])){
                                flagTotal = 1;
                            }
                        //FIND THE TOTAL OF FOURTH LEVEL AGGREAGATION
                        }else if(level == 3){
                            //COMPARE THE CURRENT VALUE WITH THE CURRENT GROUP AND ALSO COMPARE THE LEVEL ZERO VALUE WITH CURRENT VALUE AND ALSO COMPARE THE LEVEL ONE VALUE WITH CURRENT VALUE 
                            if((value[groupColumnArray[level]] == group) && (value[groupColumnArray[level-1]] == recentLevelTwo[recentLevelTwo.length-1]) && (value[groupColumnArray[level-2]] == recentLevelOne[recentLevelOne.length-1])&& (value[groupColumnArray[level-3]] == recentLevelZero[recentLevelZero.length-1])){
                                flagTotal = 1;
                            }
                        //FIND THE TOTAL OF FIFTH LEVEL AGGREAGATION
                        }else if(level == 4){
                            //COMPARE THE CURRENT VALUE WITH THE CURRENT GROUP AND ALSO COMPARE THE LEVEL ZERO VALUE WITH CURRENT VALUE AND ALSO COMPARE THE LEVEL ONE VALUE WITH CURRENT VALUE 
                            if((value[groupColumnArray[level]] == group)&& (value[groupColumnArray[level-1]] == recentLevelThree[recentLevelThree.length-1]) && (value[groupColumnArray[level-2]] == recentLevelTwo[recentLevelTwo.length-1]) && (value[groupColumnArray[level-3]] == recentLevelOne[recentLevelOne.length-1])&& (value[groupColumnArray[level-4]] == recentLevelZero[recentLevelZero.length-1])){
                                flagTotal = 1;
                            }
                        //FIND THE TOTAL OF SIXTH LEVEL AGGREAGATION
                        }else if(level == 5){
                            //COMPARE THE CURRENT VALUE WITH THE CURRENT GROUP AND ALSO COMPARE THE LEVEL ZERO VALUE WITH CURRENT VALUE AND ALSO COMPARE THE LEVEL ONE VALUE WITH CURRENT VALUE 
                            if((value[groupColumnArray[level]] == group) && (value[groupColumnArray[level-1]] == recentLevelFour[recentLevelFour.length-1]) && (value[groupColumnArray[level-2]] == recentLevelThree[recentLevelThree.length-1]) && (value[groupColumnArray[level-3]] == recentLevelTwo[recentLevelTwo.length-1]) && (value[groupColumnArray[level-4]] == recentLevelOne[recentLevelOne.length-1])&& (value[groupColumnArray[level-5]] == recentLevelZero[recentLevelZero.length-1])){
                                flagTotal = 1;
                            }
                        }
    
                         if(flagTotal == 1){
                            while(x <= end_loop) {
                                var val = value[x].replaceAll(".",""); 
                                val = val.replaceAll(",","."); 
                                if(val.length == '1') {
                                    val = val.replaceAll("-","0"); 
                                }
                                //CHECK IF UNCOUNTABLE COLUMN
                                checkUncountable = false;
                                //var columns = $('#'+table_id).dataTable().dataTableSettings[0].aoColumns;
                                tableNew = $('#'+table_id).DataTable();
                                var columns = tableNew.settings()[0].aoColumns;
                                $.each(columns, function(i,v) { 
                                    if(v.sTitle.toLowerCase().indexOf("duplicate") < 0 && v.nTh.className.includes('uncountable') && v.bVisible == true && x==i){
                                        checkUncountable = true;
                                    }
                                });
                                if(isNaN(val) || checkUncountable){
                                    group_values[x]="";
                                }else{
                                    if (isNaN(group_values[x])) {
                                        group_values[x] = parseFloat(val);
                                    }else{
                                        group_values[x] = parseFloat(group_values[x])+ parseFloat(val);
                                    }
                                }
                                x++;
                            }
                        }
                    });
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    If you can't link to your page showing the issue, you can find how to use external services to host it for you here.

    Allan

  • farhan07farhan07 Posts: 7Questions: 1Answers: 0

    I am asking simply how to sum rowgroup of all pages of datatable?

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Apologies, that wasn't clear to me. The rows parameter contains a reference to the rows in the group, so you can sum the data from there. There is an example of how to do that here.

    Allan

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    If performance is the issue, I would need a link to a page showing the issue so I can profile it.

  • farhan07farhan07 Posts: 7Questions: 1Answers: 0

    Thank you allan aggregation working faster .but collapse group and expand group is slow its take 15 to 18 sec. i found one issue click collapse group table header is collapsed faster but unwanted row hide later
    function nonNumericColumns(visibility, table_id, table, groupColumn) {
    var columns = table.settings()[0].aoColumns;

    $.each(columns, function(i, v) {
    
        var className = v.nTh.className;
    var column = v.idx;
        if (!visibility && className.indexOf("numeric") < 0 && v.sTitle !== '') {
            table.columns(column).visible(false);
        } else if (visibility) {
            table.columns(column).visible(true);
        }
    });
    
    if (visibility) {
        table.columns(groupColumn).visible(false);
        preHidden(table, table_id, true);
    }
    
    }
    

    function preHidden(table,table_id,draw) {
    //var columns = $('#'+table_id).dataTable().dataTableSettings[0].aoColumns;
    var columns = table.settings()[0].aoColumns ;

        var printColumns = "";
        var _active;
        var className = "";
        var column = "";
        $.each(columns, function(i,v) { 
            className = v.nTh.className;
            if((className.indexOf("pre-hidden") >= 0)) {
                //console.log(table_id+"|| "+v.idx+" || ");
                //console.log(table.column( v.idx ));
                column = v.idx;
                column = table.column( v.idx );
                // Toggle the visibility
                column.visible( ! column.visible() );
            }
        });
        if(draw){
            table.draw();
        }
    }
    

    $(".collapse-all").click(function(e) {
    e.stopImmediatePropagation();
    //console.log('collapse-all');
    //PARAMETER TO CONTROL THE SORTING WHEN EXPAND
    fullyCollapsed = 1;
    nonNumericColumns(false,table_id,tableNew,groupColumn);
    var name;
    collapsedGroups = [];
    var tableArray = [];
    var _dataSrc = tableNew.rowGroup().dataSrc();
    var dataSrcArray = Object.keys(_dataSrc);
    var aggregationIndex = 0;

            // $('#'+table_id).hide();
            // $('.dataTables_filter').hide();
            // $('.dataTables_length').hide();
            // $('.dataTables_paginate paging_simple_numbers').hide();
            $(".dataTables_processing").show("fast", function() {
                var _dataSrc = tableNew.rowGroup().dataSrc();
                _dataSrc.forEach(function(_dataSrcIndex) {
                    tableArray.push(tableNew.column(_dataSrcIndex).data())
                })
    
                //WE DON'T NEED TO EXCECUTE THE CHILDS, SO WE USE THE BELOW VARIABLE TO AVOID THE TIME CONSUME
                let excecute = '1';
    
                for(j=0; j<tableArray.length; j++){
                    aggregationIndex = aggregationIndex +1;
                    //WE NEED TO ITERATE ONLY THE PARENT AND THE LAST CHILD
                    if(aggregationIndex == 1){
                        tableArray[j].unique().each( function ( name, index ) {
                            //WHEN THERE IS MULTIPLE AGGREGATION WE HAVE TO SHOW THE PARENT ROW
                            if(j>0){
                                for(k=0; k< tableArray[0].unique().length;k++){
                                    var parent = tableArray[0][k];
                                    collapsedGroups[parent+name] = true;
                                }
                            }else{
                                collapsedGroups[name] = true; 
                            }
                        } );
                    }
                }
                //REMOVE THE PAGINATION WHILE COLLAPSE ALL
                tableNew.page.len(-1).draw();x
                $(".dataTables_processing").hide();   
                // $('#'+table_id).show();
                // $('.dataTables_filter').show();
                // $('.dataTables_length').show();
                // $('.dataTables_paginate paging_simple_numbers').show();
            }); 
        });
    

    above code for collapse pls give me the solution

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Please give me a test case.

    You can use JSFiddle, Stackblitz of something else to host a page if you can't link to your own. I cannot debug the code above in my head.

    Allan

  • koodhkoodh Posts: 1Questions: 0Answers: 0
    edited November 2

    Hi, Allan. I have the same issue here everything works fine, except when users try to expend a row the first time it works, then collapse it or expend another row stacks and in the console log we get an error like this

    jquery-3.7.0.min.js:2 Uncaught RangeError: Maximum call stack size exceeded
        at r (jquery-3.7.0.min.js:2:19447)
        at re (jquery-3.7.0.min.js:2:20505)
        at Function.I [as find] (jquery-3.7.0.min.js:2:7516)
        at ce.find (jquery-3.7.0.min.js:2:21953)
        at a._resizeAuto (datatables.min.js:87:9273)
        at HTMLTableElement.<anonymous> (datatables.min.js:87:2280)
        at HTMLTableElement.dispatch (jquery-3.7.0.min.js:2:39997)
        at HTMLTableElement.<anonymous> (jquery-3.7.0.min.js:2:37968)
        at Object.trigger (jquery-3.7.0.min.js:2:70063)
        at HTMLTableElement.<anonymous> (jquery-3.7.0.min.js:2:70665)
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    It's working OK in this example, so it must be something with your setup.

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Colin

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    @koodh - My guess is you are using an old version of DataTables - maybe 2.0.x something? Try updating to the latest 2.1.8 as I reply to this.

    Allan

Sign In or Register to comment.