Datentabellen berechnen-Feldes basierend auf der Zusammenfassung eines anderen Feldes

Habe ich Daten, die in der definitiven version wird kommen vom server, aber für jetzt, es ist statisch und sieht so aus:

[
 {"name": "John","c1": 12,"c2": 10,"c3": 5},
 {"name": "Jack","c1": 10,"c2": 20,"c3": 15},
 {"name": "Bill","c1": 8,"c2": 30,"c3": 15}
]

Ich bin initialisieren DataTable wie folgt:

var iTotal = [0, 0, 0];

var oTable1 = $('#example1').dataTable({
    "table-layout": "fixed",
    "oLanguage": {
        "sZeroRecords": "No data"
    },
    "fnPreDrawCallback": function(oSettings) {
        iTotal = [0, 0, 0];
        for (var i = 0; i < oSettings.aoData.length; i++) {
            iTotal[0] += oSettings.aoData[i]._aData.c1;
            iTotal[1] += oSettings.aoData[i]._aData.c2;
            iTotal[2] += oSettings.aoData[i]._aData.c3;
        }
        //set percentage value
        for (i = 0; i < oSettings.aoData.length; i++) {
            oSettings.aoData[i]._aData.perc = (oSettings.aoData[i]._aData.c1 / iTotal[0] * 100).toFixed(2) + '%';
        }
        console.log(oSettings.aoData); //here data is set correct 
    },
    "fnFooterCallback": function(nRow, aaData, iStart, iEnd, aiDisplay) {
        var nCells = nRow.getElementsByTagName('th');
        nCells[1].innerHTML = iTotal[0];

        //check if column[2] is visible??!!how
        //nCells[2].innerHTML=iTotal[1];

        var secondRow = $(nRow).next()[0]; //see this
        var ndCells = secondRow.getElementsByTagName('th');
        ndCells[1].innerHTML = aaData.length > 0 ? (iTotal[0] / aaData.length).toFixed(2) : 0;
    },
    "bPaginate": false,
    "bLengthChange": false,
    "bFilter": false,
    "bSort": true,
    "bInfo": false,
    "bAutoWidth": false,
    "aaSorting": [[0, "asc"]],

    "aaData": [{"name": "John","c1": 12,"c2": 10,"c3": 5},
               {"name": "Jack","c1": 10,"c2": 20,"c3": 15},
               {"name": "Bill","c1": 8,"c2": 30,"c3": 15}
              ],
    "aoColumns": [{
        "mData": "name"},
    {
        "mData": "c1"},
    {
        "mData": "c2"},
    {
        "mData": "c3"},
    {
        "mData": null,
        "bVisible": false,
        "mRender": function(data, type, full) {
            return (full.c1 / iTotal[0]);
        }},
    {
        "mData": null,
        "sClass": "center",
        "mRender": function(data, type, full) {
            return '<img title="Remove"  class="deleteMe" src="http://openfire-websockets.googlecode.com/svn-history/r2/trunk/plugin/web/images/delete-16x16.gif" style="cursor: pointer">';
        }}]
});

in fnPreDrawCallback ich bin versucht zu berechnen Wert von perc-Feld und verwenden der Konsole.anmelden ich bin in der Lage, um zu sehen, korrekten Daten, aber irgendwie wenn ich versuche die Anzeige perc in der richtigen Spalte bekomme ich keine Daten.

Hier ist mein Beispielcode: http://jsfiddle.net/Misiu/kS9bj/1/

Wie soll ich update mein code zum berechnen mein Anteil Spalte "on fly"

InformationsquelleAutor Misiu | 2012-09-24
Schreibe einen Kommentar