We help IT Professionals succeed at work.

why drop down box are not coming correctly while comparing two JSON objects

chaitu chaitu
chaitu chaitu used Ask the Experts™
on
why drop down box are not coming correctly while comparing two JSON objects;
by default 5 values should be drop down in each select box for every row and one value should be selected by comparing another JSON object.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
<title> New Document </title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
</head>
 <body>
 <table id='emp_accnts' border='1' style="display:none">
</table>

  <script>
var data={"loadAllBankAccounts":
{"3":"HDFC-CURRENT","2":"HDFC-SALARY","1":"HDFC-FIXED","5":"ICICI-CURRENT","4":"ICICI-SALARY"},
"loadAccountsByEmployeeId":
 [{"accountNo":"101-234-2099","amount":100,"bankAccountType":"HDFC-FIXED","bankAccountTypeId":1,"employeeId":217},
 {"accountNo":"231-221-2221","amount":200,"bankAccountType":"HDFC-SALARY","bankAccountTypeId":2,"employeeId":217},
 {"accountNo":"131-209-0981","amount":300,"bankAccountType":"HDFC-CURRENT","bankAccountTypeId":3,"employeeId":217}]};

$("#emp_accnts").show();
	var json1 = data.loadAccountsByEmployeeId;
			var header = false;
			for (key in json1) {
					if (!header) var headertr = $('<tr/>');
					var tr = $('<tr/>');
					if (!header) var texttr = $('<tr/>');

					for (key1 in json1[key]) {

					if(key1 == "bankAccountTypeId" || key1 == "employeeId")
						continue;

							if (!header)
							headertr.append($('<th/>').html(key1));
							
						//if (!header)
						//	texttr.append($('<td/>').html("<input type='text' id='' name='' value=''/>"));

 						var options = $("#bankaccount"); 
							
					if(key1 == "bankAccountType")
						{
						 	tr.append($('<td/>').html("<select id='bankaccount'>"));
						
					//	alert("key1.."+key1)
								var bankAccountTypes = data.loadAllBankAccounts;
									for (accountType in bankAccountTypes) {

//alert(json1[key][key1]+'..json1[key][key1]..accountType..'+bankAccountTypes[accountType])
									

											if(json1[key][key1] == bankAccountTypes[accountType])
											{

											alert("if.."+bankAccountTypes[accountType])
												options.append($("<option />").val(accountType).text(bankAccountTypes[accountType]));
												//break;
												
											}
											else
											{
												//alert("else.."+bankAccountTypes[accountType])
												options.append($("<option />").val(accountType).text(bankAccountTypes[accountType]));
											}


									}
									

						 }
						 else
						 tr.append($('<td/>').html("<input type='text' id='' name='' value='"+json1[key][key1]+"'/>"));

					}

					
					if(!header){
						//$("#emp_accnts").append(texttr);
						$("#emp_accnts").append(headertr);
						header=true;
					}
					$("#emp_accnts").append(tr);
				
			}



  </script>

 </body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Slightly refactored in this jsfiddle - code here.

Author

Commented:
thank you boss.

Author

Commented:
still not sure what is the mistake i made???