Fetching data from database but not showing the calculation applied on it?

Fetching data from database but not showing the calculation applied on it?

ahsannajamahsannajam Posts: 2Questions: 2Answers: 0

//i m fetching data from database using php i tested that php file converted data in json format and in this file code i can see the data in alert also but it is not showing data in table with applied calculation. I tested this calculation with dummy dataset, calculation is also working fine, only if i fetch from database it shows data but not applies the calculation.

$(document).ready(function () {
var url = 'Distributor.php';
$("#date").change(function () {
var date = $("#date").val();
var dist = $("#dist_nam").find(':selected').val();
console.log(dist + ' ' + date);
get(dist, date);
});
function showDate(str, strD) {
$.get(url + '?q=' + str + '&d=' + strD, function (data, status) {
console.log("Data: " + data + "\nStatus: " + status);
alert(data);
});
}
});
function get(dist, date)
{
$("#submit").click(function () {
var datepicker = date;
var Dis = dist;
var data = 'Date=' + datepicker + '&Name=' + Dis;
if (datepicker == '' || Dis == '')
{
alert("Please Fill All Fields");
} else
{
$.ajax({
type: "POST",
url: "Distributor.php",
data: data,
cache: false,
success: function (result) {
alert(result);
var data = JSON.parse(result);
alert(data);
//i can see data there in alert
$.each(data, function (i, it) {
console.log(it);
it.cbTotal = it.nsp * it.closing_balance;
it.csTotal = it.nsp * it.current_sales;
it.csMTotal = Math.round((1.5) * it.csTotal);
it.cbSTotal = it.csMTotal - it.cbTotal;
});

                $('#example').DataTable({
                    'data': data,
                    "order": [1],
                    "columns": [
                        // rest of the columns
                        {
                            title: "distributor_name"
                        }, {
                            title: "order_date"
                        }, {
                            title: "product_name"
                        }, {
                            title: "nsp"
                        }, {
                            title: "current_sales"
                        }, {
                            title: "closing_balance",
                            "render": function (data) {
                                return cbM = data;
                            }}, {
                            title: "cbTotal",
                            "visible": false,
                            "searchable": false
                        }, {
                            title: "csTotal",
                            "visible": false,
                            "searchable": false
                        }, {
                            title: "csMTotal",
                            "visible": false,
                            "searchable": false
                        }, {
                            title: "current_sales",
                            "render": function (data) {
                                return csM = Math.round(data * 1.5);
                            }}, {
                            title: "cbSTotal"
                           ,
                            "visible": false,
                            "searchable": false
                        }, {
                            title: "status",
                            "render": function (data) {
                                var abc = csM - cbM;
                                return  Math.round(abc);
                            }
                        }
                    ],
                    "footerCallback": function (tfoot, data, input, start, end, display) {
                        var api = this.api();
                        var p = api.column(6).data().reduce(function (a, b) {
                            return a + b;
                        }, 0);
                        $(api.column(5).footer()).html(p);
                        $("#cbtotal").val(p);

                        var q = api.column(7).data().reduce(function (a, b) {
                            return a + b;
                        }, 0);
                        $(api.column(4).footer()).html(q);
                        $("#cstotal").val(q);

                        var r = api.column(8).data().reduce(function (a, b) {
                            return a + b;
                        }, 0);
                        $(api.column(9).footer()).html(r);
                        $("#csMtotal").val(r);

                        var s = api.column(10).data().reduce(function (a, b) {
                            return  Math.abs(a + b);
                        }, 0);
                        $(api.column(10).footer()).html(s);
                        $("#cbStotal").val(s);

                        var t = api.column(10).data().reduce(function (a, b) {
                            return  Math.abs(a + b);
                        }, 0);
                        $(api.column(11).footer()).html(t);
                        $("#statustotal").val(t);
                    }
                });

            }
        });
    }
    ;
});

}
;

This discussion has been closed.