Calculating and displaying % in column of a generated sum

Calculating and displaying % in column of a generated sum

dennisbarteldennisbartel Posts: 5Questions: 0Answers: 0

Is it possible to have a column calculate the percentage of a sum that was generated by using .data().sum() in a drawCallback:. This changes upon draw / filter. For Example the sum of 10 rows would be 10000 and if one of the rows had 2000 for data the percent column would read 20%. This percentage would change depending on the number of records. Currently using dataTables 1.10.8.

Replies

  • glendersonglenderson Posts: 231Questions: 11Answers: 29

    Yes, it is possible.

    But, I would suggest using it in the footerCallBack instead of the drawCallBack as the footer seems to be the better place to determine the displayed contents.

    Please see this fiddle, choose A, B or C in the search box to filter only the displayed results.

    http://jsfiddle.net/glenderson/of1xo61o/

  • allanallan Posts: 61,692Questions: 1Answers: 10,101 Site admin

    You might also be interested in this example and modifying that to suit your needs.

    Allan

  • dennisbarteldennisbartel Posts: 5Questions: 0Answers: 0

    Thank you glenderson i was able to solve it using the code you provided. As I have data coming in through ajax i made a few tweaks to get it to work. Data undefined for the column with defaultcontent: " ",

       { data: "*******" },
       { data: ")()()())()(" },
       { data: undefined ,  "defaultContent": " "  },
    
    

    and for populating the column per row defined the data from the JSON.

      var d = this.data();
      d[4] = (d[")()()())()("] / pageTotal * 100).toFixed(2) +"%";
      this.data(d);
    

    Thank you glenderson for the solution, I really appreciate your help. Thank you Allan.

This discussion has been closed.