Can't access global variable from inside startRender() function

Can't access global variable from inside startRender() function

hovhov Posts: 15Questions: 2Answers: 0

Hello!

I am having a problem with accessing global variables inside my rowGroup().startRender() function. I couldn't find any post in forum that has came across this problem.
My code:

<script>
  
  $(document).ready(function(){
     var sum = {};
     var groupColumn = 'columnName2';
     rowGroup: {
        startRender: function(rows, group, level) {
           if (!(group in sum)) {
                            sum[group] = rows
                                .rows({ search: 'applied' })
                                .data()
                                .filter(function (value, index) {
                                    return (value[groupColumn] == group) ? true : false;
                                })
                                .pluck('columnName')
                                .reduce(function (a, b) {
                                    return a + b * 1;
                                }, 0);
           }
        }
     }
  })
</script>

In this example, function recognizes groupColumn variable which I have declared but dictionary (sum) is undefined.

Thanks in advance!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765
    Answer ✓

    Seems to work here:
    http://live.datatables.net/dafefime/2/edit

    Please provide a test case or update mine to show the problem.

    Kevin

  • hovhov Posts: 15Questions: 2Answers: 0

    Sorry, my mistake.
    I initialized variable with the same name afterwards in the code. That's why it was undefined.

    Appreciate the help!

This discussion has been closed.