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
565 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now