parseInt为CSS属性返回NaN以获取总值

时间:2015-07-14 20:33:28

标签: javascript php html mysql css

所以基本上我试图将样式插入到从php文件加载数据的表中(php文件从mysql中提取数据)并且JavaScript将数据显示在表中,并计算总数/平均值。数据显示。

我的PHP文件包含我的表格数据。

command_register =  "".join(["{0:04b}".format(int(c,16)) for c in str(command_register)])

我的包含html / css的tpl文件还包含从PHP加载数据的JavaScript

   $data .= "['$offer_name','$clicks', '$leads', '$$revenue', '$crate%', '$$avgCPA', '$$epc'],";

以下是结果,它正常工作并计算正确的数据

    {literal}
    <script type="text/javascript">

    $(document).ready(function(){

     $('#dataGrid').dataTable( {
    "fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {

        var clicks = 0;
        var downloads = 0;
        var conv = 0;
        var avgCPA = 0;
        var epc = 0;
        var revenue = 0;
        var conv_ = 0;

       for ( var i=0 ; i<aaData.length ; i++ )
        {
            clicks += parseInt(aaData[i][1]);
            downloads += parseInt(aaData[i][2]);
            conv += parseFloat(aaData[i][4]);
            avgCPA += parseFloat(aaData[i][5].substring(1));
            epc += parseFloat(aaData[i][6].substring(1));
            revenue += parseFloat(aaData[i][3].substring(1));
        }

        revenue = Math.round(revenue*100)/100;
        conv_ = (clicks == 0) ? 0 : (downloads/clicks)*100;
        //conv = conv_.toString().match(/^\d+(?:\.\d{0,2})?/);
        conv = Math.round(conv_);
        avgCPA = (downloads == 0) ? 0 : Math.round((revenue/downloads)*100)/100;
        epc = (clicks == 0) ? 0 : Math.round((revenue/clicks)*100)/100;

        var nCells = nRow.getElementsByTagName('th');
        nCells[1].innerHTML = clicks;
        nCells[2].innerHTML = downloads;
        nCells[3].innerHTML = '$'+revenue;
        nCells[4].innerHTML = conv+'%';
        nCells[5].innerHTML =  '$'+avgCPA;
        nCells[6].innerHTML =  '$'+epc;
    },

   {/literal}
   "aaData": [{$data}],

   {literal}          

       });

    });

    </script>
    {/literal}

但是当我尝试添加样式(我已经尝试过内联样式和使用css)到php代码

        Total/Average   10      10      $48.9       100%     $4.89     $4.89

表正在加载数据就好了意味着样式正在运行但是java脚本返回NaN以换取总计/平均值

    $data .= "[';$offer_name','<span class=\"tabclicks\">$clicks</span>', '<span class=\"tableads\">$leads</span>', '<span class=\"tabrevenue\">$$revenue</span>', '<span class=\"tabconv\">$crate%</span>', '<span class=\"tabcpa\">$$avgCPA</span>', '<span class=\"tabepc\">$$epc</span>'],";

1 个答案:

答案 0 :(得分:1)

在数字周围添加更多文本(html)后,无法将其解析为Int。您应该保持数据清洁,并且可以直接将类名添加到nCells元素。

nCells[1].className = "tabclicks";
// etc...