如果数据表中的第一列相同,如何将多列合并为一个?

时间:2019-09-23 20:42:35

标签: javascript datatables

我是使用数据表的新手, 我想将12月的第二行金额转换为第一行吗?我到目前为止所得到的:

enter image description here

所以datatable我想要的是前两行合并为一个,因为它们具有相同的分类帐,而我只有2行,如下所示: enter image description here

以及我到目前为止得到的代码:

$('#datatable').DataTable({
                    paging: false,
                    data: data,
                    ordering: false,
                    searching: false,
                    // columnDefs:[{className:"total_dr", targets:2}, {className:"total_cr", targets:3}],
                    columns: [
                        {
                            data: 'group', 
                            name: 'group', 
                            'render': function (data, type, row) {
                                // debugger;
                                var dt = row.group[0];
                                
                                return dt;
                            }
                        },
                        // {data: 'date', name: 'date'}, 
                        {
                            data: 'ledger',
                            name: 'ledger',
                            'render': function (data, type, row) {
                                
                                var dataLedger = row.ledger.code + ' - ' + row.ledger.name;
                                
                                return dataLedger;
                            }
                        },
                        {
                            data: 'amount', 
                            name: 'amount',
                            'render': function (data, type, row) {
                                var amount = '-';

                                if(row.date == '1'){
                                    var amount = row.amount;
                                }
                                
                                return amount;
                            }
                        },
                        {
                            data: 'amount', 
                            name: 'amount',
                            'render': function (data, type, row) {
                                var amount = '-';

                                if(row.date == '2'){
                                    var amount = row.amount;
                                }
                                
                                return amount;
                            }
                        },
                        {
                            data: 'amount', 
                            name: 'amount',
                            'render': function (data, type, row) {
                                var amount = '-';

                                if(row.date == '3'){
                                    var amount = row.amount;
                                }
                                
                                return amount;
                            }
                        },
                        {
                            data: 'amount', 
                            name: 'amount',
                            'render': function (data, type, row) {
                                var amount = '-';

                                if(row.date == '4'){
                                    var amount = row.amount;
                                }
                                
                                return amount;
                            }
                        },
                        {
                            data: 'amount', 
                            name: 'amount',
                            'render': function (data, type, row) {
                                var amount = '-';

                                if(row.date == '5'){
                                    var amount = row.amount;
                                }
                                
                                return amount;
                            }
                        },
                        {
                            data: 'amount', 
                            name: 'amount',
                            'render': function (data, type, row) {
                                var amount = '-';

                                if(row.date == '6'){
                                    var amount = row.amount;
                                }
                                
                                return amount;
                            }
                        },
                        {
                            data: 'amount', 
                            name: 'amount',
                            'render': function (data, type, row) {
                                var amount = '-';

                                if(row.date == '7'){
                                    var amount = row.amount;
                                }
                                
                                return amount;
                            }
                        },
                        {
                            data: 'amount', 
                            name: 'amount',
                            'render': function (data, type, row) {
                                var amount = '-';

                                if(row.date == '8'){
                                    var amount = row.amount;
                                }
                                
                                return amount;
                            }
                        },
                        {
                            data: 'amount', 
                            name: 'amount',
                            'render': function (data, type, row) {
                                var amount = '-';

                                if(row.date == '9'){
                                    var amount = row.amount;
                                }
                                
                                return amount;
                            }
                        },
                        {
                            data: 'amount', 
                            name: 'amount',
                            'render': function (data, type, row) {
                                var amount = '-';

                                if(row.date == '10'){
                                    var amount = row.amount;
                                }
                                
                                return amount;
                            }
                        },
                        {
                            data: 'amount', 
                            name: 'amount',
                            'render': function (data, type, row) {
                                var amount = '-';

                                if(row.date == '11'){
                                    var amount = row.amount;
                                }
                                
                                return amount;
                            }
                        },
                        {
                            data: 'amount', 
                            name: 'amount',
                            'render': function (data, type, row) {
                                var amount = '-';

                                if(row.date == '12'){
                                    var amount = row.amount;
                                }
                                
                                return amount;
                            }
                        }
                    ],
                    drawCallback: function ( settings ) {
                            var api = this.api();
                            var rows = api.rows( {page:'all'} ).nodes();
                            var last=null;
                            
                            // 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=[];
                            api.column(0, {page:'all'} ).data().each( function ( group, i ) {
                                console.log(group[0]);
                                group_assoc=group[0].replace(' ',"_");
                                
                                if(typeof total[group_assoc] != 'undefined'){
                                    total[group_assoc]=total[group_assoc]+intVal(api.column(12).data()[i]);
                                }else{
                                    total[group_assoc]=intVal(api.column(12).data()[i]);
                                }

                                if ( last !== group[0] ) {
                                    $(rows).eq( i ).before(
                                        // '<tr class="group"><td colspan="2">'+group[0]+'</td><td class="'+group_assoc+'"></td></tr>'
                                        '<tr class="group"><td colspan="2">'+group[0]+'</td></tr>'
                                    );
                    
                                    last = group[0];
                                }
                            });
                        for(var key in total) {
                            $("."+key).html(total[key]).number( true, 2 );
                        }
                    }
                });

请帮助我...我已经卡住了..谢谢

0 个答案:

没有答案