Alert elements form returned JSON

Alert elements form returned JSON

classic12classic12 Posts: 228Questions: 60Answers: 4

I am using the following to set the json returned to a global variable

 $('#dtInvoiceDetails')
    .on('xhr.dt', function ( e, settings, json, xhr ) {
           selectedPrintData = JSON.stringify(json);

    } );   

the console shows

selectedPrintData = {"data":[{"DT_RowId":"row_10108","inv_list":{"lineID":"10108","InvID":"3113","quantity":"23","barCode":"5555","stockCode":"8686856","size":"77","colour":"","description":"more jjjjjjj","costPrice":"0.00","sellPrice":"5.00","lineTotalNett":"115.00","lineTotal":"0.00","vatRate":"0.00"}}],"options":[],"files":[]}

I have tried

console.log(selectedPrintData[0]['data']['inv_list']['quantity']);
console.log(selectedPrintData[0][data][inv_list][quantity]);

What is the correct way to get thhe returned data into an array then be able to alert the 'quantity' value.

Cheers

Steve Warby

This question has an accepted answers - jump to answer

Answers

  • classic12classic12 Posts: 228Questions: 60Answers: 4

    I got it working but there must be a simpler way to achieve this.

        console.log( ' selectedPrintData = '+ JSON.stringify(selectedPrintData));
        newSelectedPrintData = [selectedPrintData];
        console.log( ' newSelectedPrintData = '+ JSON.stringify(newSelectedPrintData));
        var inv_list = selectedPrintData.data;
        console.log( ' inv_list = '+ JSON.stringify(inv_list));
    
        for (var i = 0; i < inv_list.length; i++) {
            doc.text(5,rowPos,inv_list[i].inv_list.quantity);
         }
    
    [Log]  selectedPrintData = {"data":[{"DT_RowId":"row_10120","inv_list":{"lineID":"10120","InvID":"3113","quantity":"100","barCode":"5555","stockCode":"8686856rrr","size":"77","colour":"ref","description":"Big drill massifffffh","costPrice":"0.00","sellPrice":"10.00","lineTotalNett":"1000.00","lineTotal":"24.00","vatRate":"20.00"}}],"options":[],"files":[]} (code.js, line 1352)
    
     newSelectedPrintData = [{"data":[{"DT_RowId":"row_10120","inv_list":{"lineID":"10120","InvID":"3113","quantity":"100","barCode":"5555","stockCode":"8686856rrr","size":"77","colour":"ref","description":"Big drill massifffffh","costPrice":"0.00","sellPrice":"10.00","lineTotalNett":"1000.00","lineTotal":"24.00","vatRate":"20.00"}}],"options":[],"files":[]}]
    
     inv_list = [{"DT_RowId":"row_10120","inv_list":{"lineID":"10120","InvID":"3113","quantity":"100","barCode":"5555","stockCode":"8686856rrr","size":"77","colour":"ref","description":"Big drill massifffffh","costPrice":"0.00","sellPrice":"10.00","lineTotalNett":"1000.00","lineTotal":"24.00","vatRate":"20.00"}}]
    printDocument — code.js:1358
    

    Cheers

    Steve Warby

  • rf1234rf1234 Posts: 2,990Questions: 87Answers: 421
    Answer ✓

    "What is the correct way to get thhe returned data into an array then be able to alert the 'quantity' value."

    the data is alredy in an array (of objects): json.data

    $('#dtInvoiceDetails')
       .on('xhr.dt', function ( e, settings, json, xhr ) {
              alert(json.data[0].quantity)
       } );  
    

    or something like this

    $('#dtInvoiceDetails')
       .on('xhr.dt', function ( e, settings, json, xhr ) {
             for (i=0; i < json.data.length; i++) {
                 alert(json.data[i].quantity)
             }
       } );  
    
  • classic12classic12 Posts: 228Questions: 60Answers: 4

    Thanks....

This discussion has been closed.