问题
I want to use the values from multiple rows returned in a resultSet to invoke a procedure again and again and display the result of the invoked procedure on main html page
Adapter JS
var selectStatement = WL.Server.createSQLStatement("select * from medicine WHERE Name= ? ");
var selectStatement2 = WL.Server.createSQLStatement("select LocId from location WHERE LocName= ? ");
var selectStatement3 = WL.Server.createSQLStatement("select RegNo from storeloc WHERE LocId= ? ");
var selectStatement4 = WL.Server.createSQLStatement("select RegNo from stormedavl where RegNo=? AND (MedId=? AND Availability=true)");
var selectStatement5 = WL.Server.createSQLStatement("select * from store WHERE RegNo= ? ");
function getMedicineDetails1(Name,Location) {
var a=getMedicineDetails(Name);
var MedId=a.resultSet;
var b=getLocId(Location);
var LocId=b.resultSet;
var c=getRegNo(LocId[0].LocId);
var cc=c.resultSet;
//here c.resultSet contains two rows
var d={},e;
if(cc && cc.length>0)
{
for(var i=0;i<c.resultSet.length;i++)
{
d[i]=getFinal(cc[i].RegNo,MedId[0].MedId);
}
return d;
}
}
function getMedicineDetails(Name) {
return WL.Server.invokeSQLStatement({
preparedStatement : selectStatement,
parameters : [Name]
});
}
function getLocId(Location) {
return WL.Server.invokeSQLStatement({
preparedStatement : selectStatement2,
parameters : [Location]
});
}
function getRegNo(LocId) {
return WL.Server.invokeSQLStatement({
preparedStatement : selectStatement3,
parameters : [LocId]
});
}
function getFinal(RegNo,MedId) {
return WL.Server.invokeSQLStatement({
preparedStatement : selectStatement4,
parameters : [RegNo,MedId]
});
}
function getStoreDetails(RegNo) {
return WL.Server.invokeSQLStatement({
preparedStatement : selectStatement5,
parameters : [RegNo]
});
}
JS file
window.$ = window.jQuery = WLJQ;
function wlCommonInit() {
}
$(document).ready(function(){
$("#search").click(function(){
GetEmployeeData();
});
});
var med;
function GetEmployeeData() {
var medicine= $("#medicine").val();
var location=$("#location").val();
alert(medicine);
var invocationData = {
adapter : 'ATM',
procedure : 'getMedicineDetails1',
parameters: [medicine,location]
};
WL.Client.invokeProcedure(invocationData,{
onSuccess : loadFeedsSuccess,
onFailure : loadFeedsFailure
});
}
function loadFeedsSuccess(result){
alert("Hii");
WL.Logger.debug("Feed retrieve success");
if (result.invocationResult.resultSet.length>0)
{
displayFeeds2(result.invocationResult.resultSet);
}
else
loadFeedsFailure();
}
function loadFeedsFailure(result){
alert("Values Not Found in DB");
}
function displayFeeds2(items){
alert("ii");
var table = document.getElementById("myTable1");
for(var i=0;i<items.length;i++)
{
var row = table.insertRow(i+1);
// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
var cell1 = row.insertCell(0);
cell1.innerHTML = items[i].RegNo;
}
}
function LoadResultPage()
{
$("AppDiv").hide();
$("#itemsList").show();
};
HTML FILE
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>ATM</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<link rel="shortcut icon" href="images/favicon.png">
<link rel="apple-touch-icon" href="images/apple-touch-icon.png">
<link rel="stylesheet" href="css/ATM.css">
<script>window.$ = window.jQuery = WLJQ;</script>
</head>
<body id="content" style="display: none;">
<!--application UI goes here-->
<div id="AppDiv">
<h1> <img border="0" src="images/atm.png" width="50" height="50" align="middle">
<font color='white'> ANY TIME MEDICINE </font>
</h1>
<br>
<div data-role="page" id="mainPage">
<div data-role="header">search medicine
</div><br>
<hr>
<div data-role="content">
<input type="text" value="MEDICINE" id="medicine"><hr>
<input type="text" value="LOCATION" id="location"><hr>
<input type="submit" id="search" value="SEARCH">
</div>
</div>
<div id="itemsList" data-role="page">
<table id="myTable1" border="1">
<tr>
<th>Registration No</th>
</tr>
</table>
<div data-role="header">gg
</div>
<div data-role="content">
</div>
</div>
<script src="js/initOptions.js"></script>
<script src="js/ATM.js"></script>
<script src="js/messages.js"></script>
</div>
</body>
</html>
The problem is that at the output we get only one RegNo(registration no )instead of two as a result of calling getFinal adapter procedure from the loop.I want to ask the way we can store the result of invocations of getFinal procedure and then displat them at the html page.Basically i want to ask if we can return an array of resultSets from server to he client.
回答1:
What you need to do is to mashup both result sets from the getRegNo with the getFinal. Here is how to get an array of resultSets from the server
Change your code from:
//here c.resultSet contains two rows
var d={},e;
if(cc && cc.length>0) {
for(var i=0;i<c.resultSet.length;i++) {
d[i]=getFinal(cc[i].RegNo,MedId[0].MedId);
}
return d;
}
in your code you will get something like {0 : 'regNo_1', 1 : 'regNo_2'}
where the integer is d[i]
to:
//here c.resultSet contains two rows
if (cc && cc.length > 0) {
for (var i = 0; i < cc.length; i++) {
cc[i].regNo = getFinal(cc[i].RegNo,MedId[0].MedId);
}
return {'regNos' : cc};
}
}
In order to get an array of resultSets that will look like this:
{
"regNos": [
{
"regNo": 12345,
"regNo": {
"isSuccessful": true,
"regNo": [
{
"regNo": "1"
}
]
}
},
{
"regNo": 54321,
"regNo": {
"isSuccessful": true,
"resultSet": [
{
"regNo": "2"
}
]
}
}
],
"isSuccessful": true
}
来源:https://stackoverflow.com/questions/22783175/return-an-array-of-resultsets-from-the-server-in-worklight