Solved

JSON Data Output in a tabular grid (invoked a jsp modal upon ajax success from a  js file)

Posted on 2011-02-27
2
569 Views
Last Modified: 2012-05-11
I have been trying to seek help on this kind of question.

Here's mine. Need somebody who can help me paint the data upon ajax success on a modal window.


$.ajax({
                  type: 'post',
                  url: something.do,
                  data: $('#someForm').serialize(),
                  success: function(jsonResponse) {
                        if(jsonResponse.errorMap != null) {
                              alert(jsonResponse.errorMap);
                        } else if (jsonResponse.transactionPage != null) {
                              showData(jsonResponse.transactionPage); // instead of this I want it to loop and iterate and paint data as given below...
                        }
                  }
            });  





This showData method gets me unformatted data. I want to format it in a tabular grid in a modal window (JSP).

This is the data that is getting returned

{
  "transactions":
  [
    {
      "error": "",
      "ctrNum1": "123456789",
      "successful": true
    },
   
    {
      "error": "",
      "ctrNum2": "123456791",
      "successful": true
    },
    {
      "error": "",
      "ctrNum3": "123456792",
      "successful": true
    },
    {
      "error": "",
      "ctrNum4": "123456793",
      "successful": true
    }
  ]
}


Also I need iterate or loop in dynamically.



I want to paint the data in a tabular format upon success.

success: function(jsonResponse) {
               if(jsonResponse.errorMap != null){
                alert(jsonResponse.errorMap);
               }else if(jsonResponse.transactionSummary != null){
                  var trans = jsonResponse.transactionSummary;
                  var date =  json.transactionDate;
                      var html = "";
                        alert(jsonResponse.transactionSummary);
                        alert(jsonResponse.transactionSummary.ctrNum);
                        for (var i=0; i < trans.length; i++) {
                              html += "<div><div>" +  (i + 1) + "</div><div>" + trans.ctrNum + "</div>";
                             
                              if (trans.successful == true) {
                                    html += "<div>Successful</div></div>"
                              } else {
                                    html += "<div>" + trans.error + "</div></div>"
                              }
                        }
                 
                  //showData(jsonResponse.transactionSummary);
                  }
              }


0
Comment
Question by:mahaq
2 Comments
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 34995018

Try something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26851670.html</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">

jQuery(document).ready(function() {

var jsonResponse = {};
jsonResponse.transactionPage = {
  "transactions": 
  [
    {
      "error": "",
      "ctrNum1": "123456789",
      "successful": true
    },
    
    {
      "error": "",
      "ctrNum2": "123456791",
      "successful": true
    },
    {
      "error": "",
      "ctrNum3": "123456792",
      "successful": true
    },
    {
      "error": "",
      "ctrNum4": "123456793",
      "successful": true
    }
  ]
};

showData(jsonResponse.transactionPage);

function showData( transactionPage ) {
  var row = $("<tr/>");
  for ( var column in transactionPage.transactions[0] ) {
    row.append( $("<th/>").html( column ) );
  }
  $("#status").append( row );

  for ( var index in transactionPage.transactions ) {
    var row = $("<tr/>");
    for ( var column in transactionPage.transactions[index] ) {  /////alert(transactionPage.transactions[index][column])
      var cell = transactionPage.transactions[index][column];
      cell = ( cell.toString() != "" )? cell : " ";
      row.append( $("<td/>").text( cell ) );
    }
    $("#status").append( row );
  }
}


  $("#dialog")
  .dialog({
    autoOpen: false,
    width: "auto",
    height: "auto",
    modal: true,
    draggable: false,
    resizable: false
  });

  $("#paypal").click( function() {
    $("#dialog").dialog( "open" );
  });

});

</script>
</head>
<body>

<input id="paypal" type="button" value="Click me!!!" />

<div id="dialog" title="JSON Data">
  <table id="status" border="1"></table>
</div>

</body>
</html>

Open in new window

0
 

Author Closing Comment

by:mahaq
ID: 35029551
It was a great solution, simplified too, thanks a lot.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Removing HTML elements using javascript 4 45
Javascript tutorials with real examples 5 25
where is team.js? 3 25
JS does not refresh 6 19
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question