Мне нужно инициализировать datatable внутри свойства success ajax call. Код выглядит следующим образом, и я сокращаю ненужный код: вызов, как мы видим, идет к методу действия, который возвращает JSON данных.
$('#id_fetch,.filterOkButton').on('click', function () {
debugger;
$.ajax({
url: '/Reports/_GetDiscountGrid',
data: {
State: stat,
Zone: zon,
Amrac: amr,
Store: store,
Hierarchy: hirarcy,
DiscountType: discountType,
Itemcategorycode: itemcatCode,
Itemcode: code,
Period: period,
OfferNo: offernumber
},
datatype: "html",
success: function (data) {
$('#tbldiscountDIV').empty();
$('#tbldiscountDIV').show();
LoadBreadCam();
//initializing DataTable
$('#tblDiscount').DataTable().clear();
$('#tblDiscount').DataTable().destroy();
var countRecords = data.recordCount;
var totalOfDiscount = data.sumDiscount;
var totalOfNetValue = data.total_Net_Value;
var totalOfQuantity = data.total_quantity;
var dataset = "";
var out = [];
out = data.res;
$('#tblDiscount').DataTable({
serverSide: true,
ordering: false,
searching: false,
lengthChange: false,
footer: true,
scrollY: 500,
scrollX: true,
scroller: {
loadingIndicator: true
},
columnDefs: [{ targets: [14, 16, 17], className: 'dt-body-right' },
{ targets: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15], classname: 'dt-body-left' }
],
dom: 'Bfrtip',
async: false,
ajax: function (data, callback, settings) {
var transferDate = '';
var parsedDate = '';
var expiryDate = '';
var parsedExpiryDate = '';
dataset = data.res;
countRecords = data.recordCount;
totalOfDiscount = data.sumDiscount;
totalOfNetValue = data.total_Net_Value;
totalOfQuantity = data.total_quantity;
$("#tblDiscount_paginate").hide();
//pushing into array
for (var i = 0; i < data.res.length; i++) {
transferDate = data.res[i].TransDate;
parsedDate = moment(transferDate);
data.res[i].TransDate = parsedDate.format('DD-MM-YYYY');
expiryDate = data.res[i].ExpirationDate;
parsedExpiryDate = moment(expiryDate);
data.res[i].ExpirationDate = parsedExpiryDate.format('DD-MM-YYYY');
if (hirarcy == "I") {
out.push([data.res[i].state, data.res[i].zone,
data.res[i].Depot, data.res[i].StoreCode, data.res[i].OfferType, data.res[i].DiscountType,
data.res[i].OfferNo, data.res[i].Description, data.res[i].ReceiptNo, data.res[i].TransDate,
data.res[i].Category, data.res[i].ItemCode, data.res[i].LotNo, data.res[i].ExpirationDate,
data.res[i].Quantity, data.res[i].FreeItem, data.res[i].NetValue, data.res[i].Discount]);
}
else if (hirarcy == "S") {
out.push([data.res[i].state, data.res[i].zone, data.res[i].amro,
data.res[i].StoreCode, data.res[i].OfferType, data.res[i].DiscountType,
data.res[i].OfferNo, data.res[i].Description, data.res[i].ReceiptNo, data.res[i].TransDate,
data.res[i].Category, data.res[i].ItemCode, data.res[i].LotNo, data.res[i].ExpirationDate,
data.res[i].Quantity, data.res[i].FreeItem, data.res[i].NetValue, data.res[i].Discount]);
}
else if (hirarcy == "F") {
out.push([data.res[i].state, data.res[i].zone, data.res[i].rac,
data.res[i].StoreCode, data.res[i].OfferType, data.res[i].DiscountType,
data.res[i].OfferNo, data.res[i].Description, data.res[i].ReceiptNo, data.res[i].TransDate,
data.res[i].Category, data.res[i].ItemCode, data.res[i].LotNo, data.res[i].ExpirationDate,
data.res[i].Quantity, data.res[i].FreeItem, data.res[i].NetValue, data.res[i].Discount]);
}
};//end of for loop
setTimeout(function () {
callback({
draw: data.draw,
data: out,
recordsTotal: parseInt(countRecords),
recordsFiltered: parseInt(countRecords)
});
}, 150); //scroll request size and limit size is same
}, //end of Datatable ajax
footerCallback: function (row, data, start, end, display) {
var api = this.api()
var json = api.ajax.json();
$(api.column(17).footer()).html(totalOfDiscount);
var api = this.api(), data;
$(api.column(17).footer()).html('Total:' + totalOfDiscount);
$(api.column(16).footer()).html('Total:' + totalOfNetValue);
$(api.column(14).footer()).html('Total:' + totalOfQuantity);
}
});//end of dataTable
},//end of success for _GetDiscountGrid
error: function(jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 500) {
alert('Internal error: ' + jqXHR.responseText);
} else {
alert('Please revise your FILTER selection as the \n' +
'information You are fetching has more than 1 lac Records');
location.reload(true);
}
}
});
}
);
В консоли или javascript ошибки нет код. Но мой dataTable не рисуется. Я понятия не имею, с чем это происходит. Кто-нибудь может направить меня в этом? NO_ERROR