Want to print ajax resopnse outside of ajax function

Want to print ajax resopnse outside of ajax function

krishanu_debnathkrishanu_debnath Posts: 18Questions: 1Answers: 0

data = [{"id":"1","Code":"123"},{"id":"2","Code":"123"}, {"id":"3","Code":"123"}]
Function used is:=
function validateform(){
var Code = '0'; //
var from = document.rptForm.fromTime.value;
var fromss = from.split("-");
var month = monthname(fromss[1]);
var year = fromss[0];
var current_date = new Date();
from = from + ' 00:00:00';
var to = document.rptForm.toTime.value;
to = to + ' 23:59:59';
var e = document.rptForm.vehicalsubcode;
var jsonData = '{"from":"'+from+'","to":"'+to+'"}';
jsonData = { "ReportJsonString":'[' + jsonData + ']'};
$("#rpt_table_wrapper").empty();
$('#datatableDiv div').html('');
var datatable = '';
datatable += '

'; datatable += ''; datatable += ''; datatable += ''; datatable += '
DateTransaction TypeVehicle CategoryCount

';
$(datatable).appendTo($("#datatableDiv"));
if (!$.fn.DataTable.isDataTable('#rpt_table')) {
$('#rpt_table').DataTable( {
dom: 'Bfrtip',
fixedHeader: true,
colReorder: true,
responsive: true,
sPaginationType: "full_numbers",
bLengthChange: true,
order: [[ 0, 'asc' ]],
aLengthMenu: [[5, 10, 15, 20, -1], [5, 10, 15, 20, , "All"]],
//lengthMenu: [ [5, 10, 50, 100, 1000, -1], [5, 10, 50, 100, 1000, "All"]],//both are same and working properly
iDisplayLength: 5,
aaSorting: [1, 'asc'],
bProcessing: true,
initComplete: function( settings, json ) {
$('div.loading').remove();
},
ajax: function (data, callback, settings) {
$.ajax({
url: url",
type: "POST",
cache: false,
data: jsonData,
contentType: "application/json;",
dataType:"json"
}).done(function(response) {
Code = response[0].Code;
callback({
draw: response.draw,
data: response,
recordsTotal: response
});
}).fail(function(err){
console.error('error...', err);
});
//return plazaCode;
},
buttons: [
{
extend: 'pdfHtml5',
title: 'Transection Detail Report Of '+month+'-'+year,
messageTop:'Code: '+ Code,
messageBottom: '\n This is system generated report on '+current_date,
orientation : 'landscape',
pageSize : 'LEGAL',
text: '<i class="fa fa-file-pdf-o"></i> Export Pdf',
customize: function(doc) {
doc.styles.tableBodyEven.alignment = 'center';
doc.styles.tableBodyOdd.alignment = 'center';
/** this line changes the alignment of 'messageBottom' and 'messageTop' /
doc.styles.message.alignment = "right";
/
this line changes the alignment of table body /
doc.content[1].table.widths = [ '40%', '25%', '17.5%', '17.5%'];
/
this line changes table body -->start /
/var rowCount = doc.content[1].table.body.length;
for (i = 1; i < rowCount; i++) {
doc.content[1].table.body[i][1].alignment = 'center';
doc.content[1].table.body[i][2].alignment = 'center';
doc.content[1].table.body[i][3].alignment = 'right';
doc.content[1].table.body[i][3].text += ' ₹';
};
/
/
this line changes table body -->end **/
},
titleAttr: 'PDF'
}
],
columns: [
{data: 'dt'},
{data: 'txntype'},
{data: 'scat'},
{data: 'cnt'}
]
});
}

        }
    </script>

:---------------------------
Every is working fine.... But I variable 'Code' which will store response data into button messageTop.

Replies

  • kthorngrenkthorngren Posts: 21,348Questions: 26Answers: 4,955

    You didn't describe what happens but I'm guessing what you are seeing in the export is Code: 0'. This happens because the value of messageTop is set when Datatables initializes. You will want to use a function like in this example.

    Kevin

  • krishanu_debnathkrishanu_debnath Posts: 18Questions: 1Answers: 0

    @kthorngren your assumption is nearly right. but what I am looking for is the value which i get from url response which is in 'done(function(response)', I want that response (second bold line from the top) into button messageTop.

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    Put it into a variable in your done callback and then return it from the messageTop function as Kevin suggests.

    Allan

  • krishanu_debnathkrishanu_debnath Posts: 18Questions: 1Answers: 0

    @allan if possible can you please share some example.

  • kthorngrenkthorngren Posts: 21,348Questions: 26Answers: 4,955

    Try changing this:

    messageTop:'Code: '+ Code,
    

    to a function like this:

                    messageTop: function () {
                        return 'Code: '+ Code;
                    },
    

    Kevin

  • krishanu_debnathkrishanu_debnath Posts: 18Questions: 1Answers: 0

    @kthorngren thank you... resolved.

  • krishanu_debnathkrishanu_debnath Posts: 18Questions: 1Answers: 0

    @kthorngren @allan one more query.. instead of showing 'No data available in table' its loading continuously when ajax return null.

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    Yes, don't return null - that isn't valid for the JSON DataTables expects. Use {"data":[]} if there are no rows to show.

    Allan

  • krishanu_debnathkrishanu_debnath Posts: 18Questions: 1Answers: 0

    @allan
    Thank you for perfect solution for previous question. that is resolved.
    please check this example.. https://datatables.net/examples/ajax/objects.html

    If I want to print salary along with '.00' what to do. example.. in first row of example's result is 162,700 but i want to print 162,700.00

  • krishanu_debnathkrishanu_debnath Posts: 18Questions: 1Answers: 0

    @allan how to close discussion after successfully resolved and tick the right answer

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    This thread was opened as a "Discussion" rather than a "Question", to there isn't a way to mark it as answered. The forum will close it automatically after a set amount of time.

    If I want to print salary along with '.00' what to do.

    Use the number renderer.

    Allan

Sign In or Register to comment.