当前位置: 动力学知识库 > 问答 > 编程问答 >

Jquery datatables totalling columns where the value is rendered as a hyerpilnk

问题描述:

I'm using the DataTables 1.10.11 with the .sum() plugin option.

I can get the sum to work and total if the column is a number. My problem is that a lot of the columns have been rendered as hyperlinks to allow a drill-down. When I sum on these, the result is Nan.

Anyone have any ideas how to work-around this.

The below code works on column 13, as this is not rendered. Columns 3-12 fail.

 $('#tblDebtors').dataTable({

"autoWidth":false

, "drawCallback": function() {

var api = this.api();

// Total over all pages

var total = api.column(13).data().sum();

$(api.column(13).footer()).html(fncFmtNbr(total,2,"Y","Y"));

}

, "footer": true

, "info":false

, "JQueryUI":true

, "ordering":true

, "paging":false

, "scrollY":"470px"

, "scrollX":"1485px"

, "scrollCollapse":true

, "columnDefs": [

{ className: "Left", "targets": [ 0,1 ] }

, { className: "Center", "targets": [ 2 ] }

, { className: "Right", "targets": [ 3,4,5,6,7,8,9,10,11,12,13 ] }

, {"targets": 3,

"data": null,

"render": function ( data, type, row ) {

return '<a id="href_Current_'+row[0]+'" href="#">'+row[3]+'</a>';

}

}

, {"targets": 4,

"data": null,

"render": function ( data, type, row ) {

return '<a id="href_Pd1_'+row[0]+'" href="#">'+row[4]+'</a>';

}

}

, {"targets": 5,

"data": null,

"render": function ( data, type, row ) {

return '<a id="href_Pd2_'+row[0]+'" href="#">'+row[5]+'</a>';

}

}

, {"targets": 6,

"data": null,

"render": function ( data, type, row ) {

return '<a id="href_Pd3_'+row[0]+'" href="#">'+row[6]+'</a>';

}

}

, {"targets": 7,

"data": null,

"render": function ( data, type, row ) {

return '<a id="href_Pd4_'+row[0]+'" href="#">'+row[7]+'</a>';

}

}

, {"targets": 8,

"data": null,

"render": function ( data, type, row ) {

return '<a id="href_Pd5_'+row[0]+'" href="#">'+row[8]+'</a>';

}

}

, {"targets": 9,

"data": null,

"render": function ( data, type, row ) {

return '<a id="href_Pd6_'+row[0]+'" href="#">'+row[9]+'</a>';

}

}

, {"targets": 10,

"data": null,

"render": function ( data, type, row ) {

return '<a id="href_Pd7_'+row[0]+'" href="#">'+row[10]+'</a>';

}

}

, {"targets": 11,

"data": null,

"render": function ( data, type, row ) {

return '<a id="href_Older_'+row[0]+'" href="#">'+row[11]+'</a>';

}

}

, {"targets": 12,

"data": null,

"render": function ( data, type, row ) {

return '<a id="href_Total_'+row[0]+'" href="#">'+row[12]+'</a>';

}

}

]

});

网友答案:

I'm guessing you using something like this, if so alter it like this for you use case:

jQuery.fn.dataTable.Api.register('sum()', function (){
    return this.flatten().reduce(function(a, b){
        if (typeof a === 'string') {
            a = a.text().replace(/[^\d.-]/g, '') * 1;
        }
        if (typeof b === 'string') {
            b = b.text().replace(/[^\d.-]/g, '') * 1;
        }
        return a + b;
    }, 0);
});

Please note, without a sample JSFiddle it's difficult to test this approach but basically we're after the text within the anchor. Rather than multiplying it by 1 we could parseInt it with a decimal radix or simple do a ~~...

Hope that helps

分享给朋友:
您可能感兴趣的文章:
随机阅读: