Onclick 2 values into textbox

Desired Output


Click image to zoom
















index.jsp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

</head>
<body>

<script>
function parse(details){
 
 if (window.DOMParser)  // Mozilla,Chrome etc.
 {
 parser=new DOMParser();
 xmlDoc=parser.parseFromString(details,"text/xml");
 }
 else // Internet Explorer
 {
 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
 xmlDoc.async=false;
 xmlDoc.loadXML(details);
 }
 
 var x =xmlDoc.getElementsByTagName("start");
 
 var key="";
 var values="";
 
 for (var i = 0; i < x.length; i++) {
  var ret = [];
    var tot=[];
    ret[0] = x[i].getElementsByTagName("Sm_Scheme_Id")[0].childNodes[0].nodeValue;
    key +=ret[0]+ " ";
   
    ret[1] =x[i].getElementsByTagName("Sm_trd_Type")[0].childNodes[0].nodeValue;
    ret[2] =x[i].getElementsByTagName("Sm_Buy_Brok")[0].childNodes[0].nodeValue;
    ret[3] =x[i].getElementsByTagName("Sm_Sell_Brok")[0].childNodes[0].nodeValue;
    
    
    
   
    tot.push(ret);
    //console.log("tot=" + tot);
    values +=tot+ "|";  // here values are seperated by comma "|"
 }
  return {key:key , values:values};
}


function parsedValues()
{
var x=parse(data);

 var key=x.key;
 alert("key: "+x.key);

 var values=x.values;
 var values_words=values.split("|");
 var values_length=values_words.length;
 alert("values: "+x.values+ "\n Total words: "+(values_length-1));

           alert("values_words [1] :   "+values_words[1]);
           return values;
   
}


var data="<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> <soap:Body> <GetValueOnSchemeIdResponse xmlns=\"http://tempuri.org/\"> <GetValueOnSchemeIdResult> <xs:schema id=\"NewDataSet\" xmlns=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\"> <xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:MainDataTable=\"Table\" msdata:UseCurrentLocale=\"true\"> <xs:complexType> <xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\"> <xs:element name=\"Table\"> <xs:complexType> <xs:sequence> <xs:element name=\"Sm_Scheme_Id\" type=\"xs:int\" minOccurs=\"0\"/> <xs:element name=\"Sm_trd_Type\" type=\"xs:string\" minOccurs=\"0\"/> <xs:element name=\"Sm_Buy_Brok\" type=\"xs:decimal\" minOccurs=\"0\"/> <xs:element name=\"Sm_Sell_Brok\" type=\"xs:decimal\" minOccurs=\"0\"/> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> <diffgr:diffgram xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\"> <NewDataSet xmlns=\"\"> <Table diffgr:id=\"Table1\" msdata:rowOrder=\"0\"> <Sm_Scheme_Id>10</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.5000</Sm_Buy_Brok> <Sm_Sell_Brok>0.5000</Sm_Sell_Brok> </Table> <Table diffgr:id=\"Table2\" msdata:rowOrder=\"1\"> <Sm_Scheme_Id>1</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.0050</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </Table> <Table diffgr:id=\"Table3\" msdata:rowOrder=\"2\"> <Sm_Scheme_Id>11</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.3500</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </Table> <Table diffgr:id=\"Table4\" msdata:rowOrder=\"3\"> <Sm_Scheme_Id>12</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>1.0000</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </Table> <Table diffgr:id=\"Table5\" msdata:rowOrder=\"4\"> <Sm_Scheme_Id>13</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>2.0000</Sm_Buy_Brok> <Sm_Sell_Brok>2.0000</Sm_Sell_Brok> </Table> <Table diffgr:id=\"Table6\" msdata:rowOrder=\"5\"> <Sm_Scheme_Id>14</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.3000</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </Table> <Table diffgr:id=\"Table7\" msdata:rowOrder=\"6\"> <Sm_Scheme_Id>15</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.2500</Sm_Buy_Brok> <Sm_Sell_Brok>0.2500</Sm_Sell_Brok> </Table> <Table diffgr:id=\"Table8\" msdata:rowOrder=\"7\"> <Sm_Scheme_Id>16</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.0250</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </Table> <Table diffgr:id=\"Table9\" msdata:rowOrder=\"8\"> <Sm_Scheme_Id>17</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>2.5000</Sm_Buy_Brok> <Sm_Sell_Brok>2.5000</Sm_Sell_Brok> </Table> <Table diffgr:id=\"Table10\" msdata:rowOrder=\"9\"> <Sm_Scheme_Id>18</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.1500</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </Table> <Table diffgr:id=\"Table11\" msdata:rowOrder=\"10\"> <Sm_Scheme_Id>19</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.0100</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </Table> <Table diffgr:id=\"Table12\" msdata:rowOrder=\"11\"> <Sm_Scheme_Id>21</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.2500</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </Table> <Table diffgr:id=\"Table13\" msdata:rowOrder=\"12\"> <Sm_Scheme_Id>31</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.3000</Sm_Buy_Brok> <Sm_Sell_Brok>0.3000</Sm_Sell_Brok> </Table> <Table diffgr:id=\"Table14\" msdata:rowOrder=\"13\"> <Sm_Scheme_Id>41</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>2.0000</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </Table> <Table diffgr:id=\"Table15\" msdata:rowOrder=\"14\"> <Sm_Scheme_Id>51</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.1250</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </Table> </NewDataSet> </diffgr:diffgram> </GetValueOnSchemeIdResult> </GetValueOnSchemeIdResponse> </soap:Body> </soap:Envelope>";

var input = data.match("<diffgr:diffgram xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\">(.*)</diffgr:diffgram>");;
var inputxml = input[1];  
alert("inputxml:   "+inputxml);  

inputxml=inputxml.replace(/<Sm_Scheme_Id>/g, '<start><Sm_Scheme_Id>');
inputxml=inputxml.replace(/<\/Sm_Sell_Brok>/g, '</Sm_Sell_Brok></start>');

var newString="<start>";
inputxml=inputxml.replace(/<\/Table>.+?<start>/g,newString);
prompt("b  ",inputxml);   // replacing all occurence of <Table diffgr:id=\"Table1\" msdata:rowOrder=\"0\"> with <start>

inputxml=inputxml.replace(/<NewDataSet xmlns=\"\">.+?<start>/g,'<NewDataSet>'+newString);  
prompt("c  ",inputxml);   // replacing 1st occurence of <Table diffgr:id=\"Table1\" msdata:rowOrder=\"0\"> with <start> of variable b

inputxml=inputxml.replace(/<\/Table>.+?<\/NewDataSet>/g,'<\/NewDataSet>');  // replacing </Table></NewDataSet> of variable c(last 2 tags) to blank value   
prompt("d  ",inputxml);

prompt("inputxml",inputxml);
data=inputxml;

var output=parsedValues();

var length=output.length;
var divide=output.length/2;
var first = output.substr(0, divide);  
var second = output.substr(divide);  

//alert ("output: "+output);

$.ajax({
    type: "POST", 
    url: "DisplayInTable.jsp",
    data: {first:first, second:second, type:"abc", position:"FIRST"},
     success: function(response){
       var win = window.open("","","width=800, height=400,resizable=yes");
       win.document.write(response);      
    }
 }); 
</script>

<form name="dataform">
<input type="text" id="first">
<input type="text" id="second">
</form>

<br><br>

<b>Note:</b> <br>
1) XML is case sensitive <br>
2) XML must have root tag ie. Only one open tag and close tag in entire xml. <br>
3) Opening and closing tag must be same. <br>
eg. <br>
  1) <b>&lt;starT&gt; </b>   <b>  &lt;/start&gt;  </b> <br>
   This tag may look same but alphabet T is capital in opening tag.<br>
   So XML parser will not parse this XML <br>

  2) <b> &lt;table1&gt; </b>  <b> &lt;/table&gt; </b> --> this is also not allowed <br>

</body>
</html>



DisplayInTable.jsp
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Display values in table</title>

 <script> 
 function getTable_No(Table_No,type,pos,row)
 {
  var type = type;
  var position=pos;
  var row=row; 
  var words=row.split(",");
  var buy=words[2];  // getting  2nd element starting from 0
  var sell=words[3];
  
  if(type == "abc")
  {
   if(position == "FIRST")
   {
    opener.document.dataform.first.value=buy;
    opener.document.dataform.second.value=sell;
    window.close();
   }
   if(position == "SECOND")
   {
    opener.document.dataform.first.value=buy;
    opener.document.dataform.second.value=sell;
    window.close();
   }
   
   
  }
  else if(type == "xyz")
   {
   if(position == "FIRST")
   {
    opener.document.dataform.first.value=buy;
    opener.document.dataform.second.value=sell;
    window.close();
   }
   }
 }
  </script> 
  
  
</head>
<body>

<%
String first = request.getParameter("first");
String second = request.getParameter("second");
String data1=first+second;
String data2 = request.getParameter("type");
String pos = request.getParameter("position");

System.out.println("data1: "+data1);
System.out.println("data2: "+data2);
System.out.println("pos: "+pos);

String[] splited_to_row = data1.split("\\|");
int splited_to_row_Length=splited_to_row.length;
//System.out.println("splited_to_row 0:  "+splited_to_row[0]);
//System.out.println("splited_to_row_Length :  "+splited_to_row_Length);

String[] splited_to_column=splited_to_row[0].split(",");  // hardcodes 0 for getting length
int splited_to_column_Length=splited_to_column.length;
  
//System.out.println("splited_to_column_Length: "+splited_to_column_Length);
//System.out.println("splited_to_column 0: "+splited_to_column[0]);

//for(int i=0; i < splited_to_row_Length; i++)
//{
// splited_to_column=splited_to_row[i].split(",");
// for(int j=0; j < splited_to_column_Length; j++)
// {
// System.out.println("splited_to_column for loop ------: "+splited_to_column[j]);
// }
//}
%>
<table border="1" style="background-color:#FFFFFF;border-collapse:collapse;border:1px solid #000000;color:#000000;width:100%" cellpadding="3" cellspacing="3">
<tr>
<th>Sm_Scheme_Id</th>
<th>Sm_trd_Type</th>
<th>Sm_Buy_Brok</th>
<th>Sm_Sell_Brok</th>
</tr>

 <%
 for(int i=0; i < splited_to_row_Length; i++)
{
 %>
 <tr>
 <%
 splited_to_column=splited_to_row[i].split(",");
 %>
 <td style="color: blue;" id='<%=splited_to_column[0]%>' onclick="getTable_No(this.id,'<%=data2 %>','<%=pos%>','<%=splited_to_row[i]%>')">  <%-- passing id, data2, pos, splited_to_row(which is an array)  as parameter --%>
   <%=splited_to_column[0]%>
   
 </td>
 <%
  
 for(int j=0; j < splited_to_column_Length-1; j++)
 {
  %>
  <td>
  <%System.out.println("splited_to_column for loop ------: "+splited_to_column[j]);%>
  <%=splited_to_column[j+1]%>
  </td>
  
 <%
 }

 %>
 </tr>
 <%
}
 %>


</table>
</body>
</html>



Desired Output




No comments:

Post a Comment