Javascript Datatables-api.column().data()对呈现列的调用

Javascript Datatables-api.column().data()对呈现列的调用,javascript,jquery,jquery-datatables,jquery-datatables-editor,Javascript,Jquery,Jquery Datatables,Jquery Datatables Editor,我的问题是,我试图在数据表的底部获得一个“总计”行。我得到了那一排,但我总共得到了0美元。如果我将api.column(4)更改为api.column(3),它会正确地添加该列,因此我知道它可以工作 我认为我的问题是如何获取列索引4(item total)的数据。我将data:设置为null,因为我通过将成本和数量相乘来呈现数据。因此,我不确定为什么api.column(4).data()无法获取呈现的信息 $(document).ready(function() { editor = new

我的问题是,我试图在数据表的底部获得一个“总计”行。我得到了那一排,但我总共得到了0美元。如果我将api.column(4)更改为api.column(3),它会正确地添加该列,因此我知道它可以工作

我认为我的问题是如何获取列索引4(item total)的数据。我将data:设置为null,因为我通过将成本和数量相乘来呈现数据。因此,我不确定为什么api.column(4).data()无法获取呈现的信息

$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
    processing: true,
    seriverSide: true,
    ajax: "DataTables-1.10.0/extensions/Editor-1.3.1/examples/php/test.php?PID=<? echo $PID ?>",
    table: "#pexpenses",
    fields: [  {
            label: "Type:",
            name: "tbl_pexpenses.type",
            type: "select"
        }, {
            label: "Cost:",
            name:  "tbl_pexpenses.cost"
        }, {
            label: "Quantity:",
            name:  "tbl_pexpenses.quantity"
        }, {
            label: "Description:",
            name:  "tbl_pexpenses.description"
        }, {

            label: "PEID:",
            name: "tbl_pexpenses.PEID",
            type: "hidden"
        }, {
            label: "PID:",
            name: "tbl_pexpenses.PID",
            def: '<? echo $PID; ?>',
            type: "hidden"
        }
    ]
} );


$('#pexpenses').DataTable( {
    dom: "Tfrtip",
    pageLength: -1,
    type: 'POST',
    paging: false,
    info: false,
    idSrc: "tbl_pexpenses.PEID",
    ajax: "DataTables-1.10.0/extensions/Editor-1.3.1/examples/php/test.php?PID=<? echo $PID ?>",
    columns: [
        { data: "tbl_types.type_desc" },
        { data: "tbl_pexpenses.description" },
        { data: "tbl_pexpenses.cost" },
        { data: "tbl_pexpenses.quantity" },
        { data: null,
                render: function ( data, type, row ) {
            return (data.tbl_pexpenses.cost*data.tbl_pexpenses.quantity);
             } }
    ],
        tableTools: {
        sRowSelect: "os",
        sSwfPath: "../DataTables-1.10.0/extensions/TableTools/swf/copy_csv_xls_pdf.swf",
        aButtons: [
            { sExtends: "editor_create", editor: editor },
            { sExtends: "editor_edit",   editor: editor },
            { sExtends: "editor_remove", editor: editor },
            "print",
            {
                "sExtends":    "collection",
                "sButtonText": "Save",
                "aButtons":    [ "csv", "xls", "pdf" ]}
        ]
    },
    "order": [[ 0, 'asc' ]],
    "drawCallback": function ( settings ) {
        var api = this.api();
        var rows = api.rows( {page:'current'} ).nodes();
        var last=null;

        api.column(0, {page:'current'} ).data().each( function ( group, i ) {
            if ( last !== group ) {
                $(rows).eq( i ).before(
                    '<tr class="grouping" ><td colspan="5">'+group+'</td></tr>'
                );

                last = group;
            }
            } );
    },  
    initComplete: function ( settings, json ) {
        editor.field( 'tbl_pexpenses.type' ).update( json.tbl_types );
    },
    footerCallback: function ( row, data, start, end, display ) {
        var api = this.api(), data;

        // Remove the formatting to get integer data for summation
        var intVal = function ( i ) {
            return typeof i === 'string' ?
                i.replace(/[\$,]/g, '')*1 :
                typeof i === 'number' ?
                    i : 0;
        };

        // Total over all pages
        data = api.column( 4 ).data();
        total = data.length ?
            data.reduce( function (a, b) {
                    return intVal(a) + intVal(b);
            } ) :
            0;

        // Update footer
        $( api.column( 4 ).footer() ).html(
            '$'+ total
        );
    }


} );



// Order by the grouping
    $('#example tbody').on( 'click', 'tr.group', function () {
        var currentOrder = table.order()[0];
        if ( currentOrder[0] === 0 && currentOrder[0] === 'asc' ) {
            table.order( [ 0, 'desc' ] ).draw();
        }
        else {
            table.order( [ 0, 'asc' ] ).draw();
        }
    } );





<table id="pexpenses" class="display" cellspacing="0" width="100%">
<thead>
    <tr>
        <th>Type</th>
        <th>Description</th>
        <th>Cost</th>
        <th>Quantity</th>
        <th>Item Total</th>
    </tr>
</thead>

<tfoot>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
            <th style="text-align:right">Total:</th>
            <th></th>
        </tr>
    <tr>
        <th>Type</th>
        <th>Description</th>
        <th>Cost</th>
        <th>Quantity</th>
        <th>Item Total</th>
    </tr>
</tfoot>
$(文档).ready(函数(){
editor=new$.fn.dataTable.editor({
处理:对,
是的,
ajax:“DataTables-1.10.0/extensions/Editor-1.3.1/examples/php/test.php?PID=“,
表:“pexpenses”,
字段:[{
标签:“类型:”,
名称:“tbl_pexpenses.type”,
类型:“选择”
}, {
标签:“成本:”,
名称:“tbl_Peexpenses.成本”
}, {
标签:“数量:”,
名称:“待定费用。数量”
}, {
标签:“说明:”,
名称:“tbl_Peexpenses.description”
}, {
标签:“PEID:”,
名称:“tbl_pexpenses.PEID”,
类型:“隐藏”
}, {
标签:“PID:”,
名称:“tbl_pexpenses.PID”,
定义:“”,
类型:“隐藏”
}
]
} );
$('pexpenses')。数据表({
dom:“Tfrtip”,
页面长度:-1,
键入:“POST”,
分页:false,
信息:错,
idSrc:“tbl_peexpenses.PEID”,
ajax:“DataTables-1.10.0/extensions/Editor-1.3.1/examples/php/test.php?PID=“,
栏目:[
{data:“tbl_types.type_desc”},
{数据:“tbl_peexpenses.description”},
{数据:“tbl_peexpenses.cost”},
{数据:“tbl_peexpenses.quantity”},
{数据:空,
呈现:函数(数据、类型、行){
返回(数据.待处理费用.成本*数据.待处理费用.数量);
} }
],
表格工具:{
sRowSelect:“操作系统”,
sSwfPath:“../DataTables-1.10.0/extensions/TableTools/swf/copy\u csv\u xls\u pdf.swf”,
阿布顿:[
{sExtends:“editor_create”,editor:editor},
{sExtends:“editor_edit”,editor:editor},
{sExtends:“editor_remove”,editor:editor},
“打印”,
{
“性倾向”:“收藏”,
“sbuttonext”:“保存”,
“阿布顿”:[“csv”、“xls”、“pdf”]]
]
},
“订单”:[[0,'asc']],
“drawCallback”:函数(设置){
var api=this.api();
var rows=api.rows({page:'current'}).nodes();
var last=null;
列(0,{page:'current'}).data().each(函数(组,i){
如果(最后!==组){
$(行).eq(i).之前(
''+组+''
);
last=组;
}
} );
},  
initComplete:函数(设置,json){
editor.field('tbl_peexpenses.type').update(json.tbl_types);
},
footerCallback:函数(行、数据、开始、结束、显示){
var api=this.api(),数据;
//删除格式以获取求和的整数数据
var intVal=函数(i){
返回类型i==='string'?
i、 替换(/[\$,]/g,)*1:
i的类型=='编号'?
i:0;
};
//总页数
data=api.column(4.data();
总计=数据长度?
数据减少(功能(a,b){
返回intVal(a)+intVal(b);
} ) :
0;
//更新页脚
$(api.column(4.footer()).html(
“$”+总计
);
}
} );
//按分组排序
$(#示例tbody')。在('click','tr.group',function(){
var currentOrder=table.order()[0];
if(currentOrder[0]==0&¤tOrder[0]===asc'){
表.order([0,'desc']).draw();
}
否则{
表.order([0,'asc']).draw();
}
} );
类型
描述
成本
量
项目总数
总数:
类型
描述
成本
量
项目总数

})

我必须缓存该列以获取正确的数据。现在计算正确。

对于需要:

var table = $('#abc').DataTable({
    "order": [[ 0, "desc" ]],
    "ajax": {
        "url": "",
        "dataSrc": "" 
    },
    "columns": [            
        { "data": "date" },
        { "data": "total1" },
        { "data": "total2" },
        {  data:  function(row, type, set){ return row.total1 - row.total2}}, 
        {  data:  function(row, type, set){ return row.total1*0.02}}
    ],
    "footerCallback": function ( row, data, start, end, display ) {
        var api = this.api(), data; 
        // Total over all pages
        total_column1 = api.column(1, {page: 'current'}).data().reduce( function (a, b) {return a + b;}, 1 );
        total_column2 = api.column(2, {page: 'current'}).data().reduce( function (a, b) {return a + b;}, 0 );
        total_column3 = api.column(3, {page: 'current'}).data().reduce( function (a, b) {return a + b;}, 0 );
        total_column4 = api.column(4, {page: 'current'}).data().reduce( function (a, b) {return a + b;}, 0 );
        $(api.column(1).footer()).html(total_column1);
        $(api.column(2).footer()).html(total_column2);
        $(api.column(3).footer()).html(total_column3);
        $(api.column(4).footer()).html(total_column4);
    },      
    "columnDefs": [
        {"className": "dt-center",  "targets": "_all" }
    ]
});

接受自己的答案是可以的,将来可能会帮助其他人(除了将问题标记为已回答之外)@davidkonrad是的,对不起。我忘了回来接受答案,因为我不得不等一天。谢谢你提醒我。
var table = $('#abc').DataTable({
    "order": [[ 0, "desc" ]],
    "ajax": {
        "url": "",
        "dataSrc": "" 
    },
    "columns": [            
        { "data": "date" },
        { "data": "total1" },
        { "data": "total2" },
        {  data:  function(row, type, set){ return row.total1 - row.total2}}, 
        {  data:  function(row, type, set){ return row.total1*0.02}}
    ],
    "footerCallback": function ( row, data, start, end, display ) {
        var api = this.api(), data; 
        // Total over all pages
        total_column1 = api.column(1, {page: 'current'}).data().reduce( function (a, b) {return a + b;}, 1 );
        total_column2 = api.column(2, {page: 'current'}).data().reduce( function (a, b) {return a + b;}, 0 );
        total_column3 = api.column(3, {page: 'current'}).data().reduce( function (a, b) {return a + b;}, 0 );
        total_column4 = api.column(4, {page: 'current'}).data().reduce( function (a, b) {return a + b;}, 0 );
        $(api.column(1).footer()).html(total_column1);
        $(api.column(2).footer()).html(total_column2);
        $(api.column(3).footer()).html(total_column3);
        $(api.column(4).footer()).html(total_column4);
    },      
    "columnDefs": [
        {"className": "dt-center",  "targets": "_all" }
    ]
});