jQuery datatables额外的服务器数据

时间:2014-04-29 17:38:21

标签: jquery-datatables

当我进行ajax调用时,我需要从服务器(所有页面的一列的总和)中获取其他数据。我试过从fnDrawCallBack获取这些数据但似乎无法访问它。

在我的codeigniter模型中,我正在设置数据,当我检查chrome开发人员工具中的响应时,它会很好。

    foreach ($rResult->result_array() as $aRow) {

        $row = array();

        foreach ($aColumns as $col) {
            $row[] = $aRow[$col];

        }
        $iTotalPrice += $aRow['price'];
        $output['aaData'][] = $row;
    }

    $output['iTotalPrice'] = $iTotalPrice;
    return $output;

我的js看起来像这样:

$('#data').dataTable({
    "sPaginationType": "bootstrap"
    , "bJQueryUI": false
    , "bAutoWidth": false
    , "bDestroy": true
    , "fnServerParams": function(aoData) {
        aoData.push({"name": "aStatuses", "value": checkedStatuses
        }, {"name": "aDeliveryStatuses", "value": checkedDeliveryStatuses
        })
    }
    , "bStateSave": false
    , "bProcessing": true
    , "bServerSide": true
    , "sServerMethod": "GET"
    , "sAjaxSource": "cars/datatable"
    , "iDisplayLength": 10
    , "bLengthChange": false
            //,"aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
    , "aaSorting": [[0, 'desc']]
    , "aoColumns": [
        {"bVisible": true, "bSearchable": true, "bSortable": true},
        {"bVisible": true, "bSearchable": true, "bSortable": true},
        {"bVisible": true, "bSearchable": true, "bSortable": true},
        {"bVisible": true, "bSearchable": true, "bSortable": true},
        {"bVisible": true, "bSearchable": false, "bSortable": false},
        {"bVisible": true, "bSearchable": false, "bSortable": true},
        {"bVisible": true, "bSearchable": false, "bSortable": false}
    ]
    , "fnDrawCallback": function(oSettings) {
        $('span').popover();

    }
    , "fnInitComplete": function(oSettings, json) {

    }
});

所以我理想的是,在回调中设置span的内容,就像 $('#mySpan').html(iTotalPrice);

但是,我似乎无法掌握数据。

感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:2)

好吧,我最终想到了这一点,任何人都有同样的问题。

我使用的是fnServerData而不是fnServerParams

'fnServerData' : function(sSource, aoData, fnCallback ){
   aoData.push({"name": "aStatuses", "value": checkedStatuses
        });
        $.getJSON( sSource, aoData, function (json) { 
    console.log(json);      
            /* Do whatever additional processing you want on the callback, then tell DataTables */
    //$('#totalValue').html('Total: £'+json.iTotalPrice);       
            fnCallback(json)
        } );

}