Manipulate a List given by the session in javascript - javascript

I am setting an attribute called "bikes" in the session, which is a List.
For instance, this code prints the firt element of the list :
function affiche() {
var myString = "${bikes.get(0).getName()}";
alert(myString);
}
I would like to use for instance a for loop and use something like
"${bikes.get(i).getName()}";
but I don't find a way to do it.
Any ideas ?
Thank you by advance
EDIT : with a java page, i am doing
session.setAttribute("bikes", bikes);
the only way I found to access the List in my jsp page is to do something like
var st = "$bikes";
but this gives something like [(filePathToObjectPackage#543622), (filePathToObjectPackage#54328)]. I think those numbers are related to memory. I can be wrong, but i thought the only way to access an object inside that list was to do
var myString = "${bikes.get(0).getName()}";
If so, i would like to be able to call it for a number contained in a variable.
I hope this is clearer now ...

Do you have try in this way:
function affiche() {
var myString = "";
for (var i = 0; i < bikes.length; i ++ ){
myString = "${bikes.get(i).getName()}";
alert(myString);
}
}

Related

How to get DockPanel name in javascript?

I have a problem and I searched many hours and I did not find useful information on the internet.I have 7 widgets in my website.These widgets are defined as DockPanel. I want user to drop any panel in any zones on my page will save the location.But I have to get dockpanel names to send my database through my webmethod. I try all combination to get my widget names, and I failed.Please help me to solve this issue.
https://docs.devexpress.com/AspNet/js-ASPxClientDockManager.GetPanels
https://docs.devexpress.com/AspNet/js-ASPxClientControlCollection.GetByName(name)
https://documentation.devexpress.com/AspNet/9448/Common-Concepts/Client-Side-Functionality/Client-Side-Events
https://documentation.devexpress.com/AspNet/9150/ASP-NET-MVC-Extensions/Common-Concepts/Client-Side-API
https://www.devexpress.com/Support/Center/Question/Details/T202401/how-to-find-dockpanel-on-the-client-using-javascript
I tried this solution but I did not fix the issue.
getName = function () {
var results = widgetName.exec((this).Object.toString());
var myJSON = JSON.stringify(results);
// return (myJSON && myJSON.length > 1) ? myJSON[1] : "";
return myJSON;
};
it is my minimal getname funtion.I try to convert panelname to object after that turn to string.How can I fix this?
When I run console.log in inspect window,I see everytime undefined or null or one letter.How can I fix this?
All 'widgets' are nothing but Objects and all objects have 'Attributes', the name, id, or whatever, are attributes.
So, try this:
var x = document.getElementsByTagName("Panel")[0].getAttribute("name");
Console.log(x);
Or if you use JQuery:
var x = $("#Panel").data("name");

Dynamically create a TW object in IBM BPM

I am using IBM BPM 8.6
I have an input string as follows:
"\"RECORD_CONTACT\":\"Maram\" , \"DRUG\":\"Panadol\"
In a script on server side, I want to dynamically create a business object like this:
tw.local.recordContact = Maram;
tw.local.drug = Panadol;
How can I dynamically create the business object?
There are a few problems with your request. The first is that you are not creating a business object, you are creating variables. In IBM BPM the variables have to be declared at design time or you will get an error, so invoking attempting to call something like -
tw.local.myVariable = 'Bob';
Will throw an exception if tw.local.myVariable has not been declared. Base on your other question you asked here (link), I'm going to assume you actually have an ANY variable declared called "return" so that
tw.local.return.myVariable = 'Bob'
will work. Given that I based on Sven's answer I think something like the following will work (you will need to validate)
var str = "\"RECORD_CONTACT\":\"Maram\" , \"DRUG\":\"Panadol\"";
var jsonStr = "{" + str.replace(/\\\"/g,'\"') + "}";
var tempValue = JSON.parse(jsonStr);
var keyArray = Object.keys(tempValue);
var valueArray = Object.values(tempValue);
for(var keyCount=0; keyCount<keyArray.length; keyCount++{
var evalString = "tw.local.return."+keyArray[keyCount]+"="+valueArray[keyCount];
eval(evalString);
}
I'll note that doing this is a very bad idea as it would be very brittle code and that using eval() in this manner opens you up to all sorts of possible exploits. It will also fail badly if the value for one of the keys is not a simple type.
-Andrew Paier
One should know what you are going to do with dynamically created Business Objects (BO) to answer you better. Like a very generic way would be - creating JSON object instead of BO.
But if you want to stick with BO then this is only possible when you know all the BO structure (schema) beforehand during design time.
var str = "\"RECORD_CONTACT\":\"Maram\" , \"DRUG\":\"Panadol\"";
vat objArray = str.split("reg ex to split each object string")
foreach (obj in objArray ){
if(obj.indexOf( "RECORD_CONTACT")!=-1)
tw.local.recordContact = new tw.object.RECORD_CONTACT();
//below goes code get value of each attribute of BPM from string
}
else if(obj.indexOf( "DRUG")!=-1){
//similar code to create BO DRUG
}
Don't forget to create BO before using those :)

Passing Data received in one html page to another

Say I have variables that I acquire in one html page, such as a UserName or a url or something. And in another html page I have input boxes for these variables and I want to autocomplete them by sending the data from the first html page to the input boxes in the second one. Can anyone indicate to me how I can achieve this?
Use JavaScript to create the equivalent collection for use by other JS code:
Code:
<script type="text/javascript">
var querystring = [ ];
var qs = location.search;
if ( qs.length > 1 )
{
qs = qs.substring(1); // skip past the ?
var pairs = qs.split( /\&/g ); // get all the name=value pairst
for ( var p = 0; p < pairs.length )
{
var pair = pairs[p];
querystring[ pair[0] ] = unescape( pair[1].replace(/\+/g," ");
}
}
</script>
Then, anyplace in your page where in ASP code you might use
Code:
var foo = Request.QueryString("foo");
you instead simply do
Code:
var foo = querystring["foo"];
CAUTION: "foo" will be case sensitive, unlike in ASP. If you wish, you could replace
Code:
querystring[ pair[0] ] = unescape( pair[1].replace(/\+/g," ");
with
querystring[ pair[0].toLowerCase() ] = unescape( pair[1].replace(/\+/g," ");
and then always use lower case names ("foo" in place of "Foo" or "FOO") when finding values.
Untested, though I have used this same code before. If there's a goof, it's just a typo.
You can use JQuery for that. Assuming you are not using any server side code.
Pass the value as a param to the next page.
Like myurl?param=xyz
Then you can get the value in the next page like this,
See this answer and sourcecode
var xyz = jQuery.url.param("param_in_url");
For this you can use php session .store that variable in session and get them in any page.or if you are calling that page from the page where u have values say username call like
next page
You should use $_SESSION variable in php. OR you can use sessionStorage of javascript.

Getting json sibling data

I currently have a json object, that I loop through and output a list of links.
See fiddle:
http://jsfiddle.net/jasonday/hzZ8j/
each link is given an id based upon the storeID in the json.
What I want to do, is when a link is clicked it finds the id in the json, and then writes the sibling element "otherData" to #otherDataDiv
I've worked with traversing xml, but I'm not sure how to accomplish this with json.
Any help would be appreciated. thanks.
You just have to loop over, like this:
var target = "store17",
foundStore = {};
for(var k1 in object.state){ var state = object.state[k1];
for(var k2 in state.store){ var store = state[k2];
if (store.storeid == target){
foundStore = store;
break;
}
}
}
However, if you were using jQuery templates then you could just look for 'tmplItem' in the data array on the element.
Additionally, if you weren't building the HTML manually for this, I would suggest using jQuery data here for this project. It would solve your problem immensely.
to store: $(selector).data('unique name here',data);
to retrieve: var usefulname = $(selector).data('unique name here');
and then in your onclick for each link you could:
var otherData = $(this).data('unique name here').otherData;

Is there a way to extract objects (class instances) in JavaScript to Android using WebView?

I am trying to extract objects and lists created in JavaScript to use them inside Android application. (I have succeeded in extracting single values.) I am using addJavaScriptInterface method to implement this.
Inside test.html, I have the following script code:
(I've tried without ".slice()" but did not work either)
function getList(){
var categoryTotals = {};
categoryTotals[0] = 1;
categoryTotals[1] = 2;
categoryTotals[2] = 3;
return categoryTotals.slice();
}
And the WebViewClient's onPageFinished method contains the following code:
mWebView.loadUrl("javascript:window.HTMLOUT.callAndroidList(getList());");
My JavaScriptInterface has the following function:
public void callAndroidList(final List list){
myList = list;
Log.d("ListTest" , "LIST 1 >>>>>>>>>>>>> " + ListTest.myList.get(0));
Log.d("ListTest" , "LIST 2 >>>>>>>>>>>>> " + ListTest.myList.get(1));
Log.d("ListTest" , "LIST 3 >>>>>>>>>>>>> " + ListTest.myList.get(2));
}
When I run this code, I get NullPointerException saying that callAndroidList's parameter, list, is null. I haven't dealt much with JavaScript so I think it is possible that this is related to creation and removal of JavaScript object instances.
Could you help me out? Thanks in advance.
> function getList(){
> var categoryTotals = {};
That will create an object, I think you mean to create an array, so perhaps it should be:
var categoryTotals = [];
.
> categoryTotals[0] = 1;
> categoryTotals[1] = 2;
> categoryTotals[2] = 3;
> return categoryTotals.slice();
Since you are creating an object with numeric properties, not an array, it does not have a slice method. Initializing categoryTotals as an array should fix that.
Incidentally, there does not seem to be any point in using slice to return a copy of the array. Since categoryTotals is not used for anything else, why not just return it?

Categories