Traverse array list in Javascript file - javascript

In my Struts 2 based application, I have an ArrayList. How can I send this ArrayList to a Javascript file so that I can compare list item with element of jsp and show some message based on validation? How can I use the array list data in Javascript file.
I do not want use JSON.

JSON comes to mind - not sure of this example so here is one more

you can use <logic:iterate> and iterate through your List, here is example

Your question makes it seem like you want to send the ArrayList to the client directly, this means it will not be processed by a JSP. The easiest way to do this is with JSON, then XML, then perhaps some custom built format which you'd need to parse with JS.
But since you then say that you want to compare a value from the action class to a value in the JSP this means you'll want the comparison done when composing the view on the server and not on the client. For this see the struts2 tag documentation the <s:iterate> and <s:if> tags will let you do this.
As your question stands the two ideas are irreconcilable without some guess work.

Related

Passing additional information per input box using HTML

I'm asking this question since I'm simply not clear on where to research further - I have experience primarily with Python, while my HTML and Javascript knowledge are on an as-needed basis. I simply haven't been able to figure out the most elegant and standard way to do this, and which component (the HTML itself, a Javascript component, Django) should be responsible for doing what I want.
I am trying to create a form for input of linguistic data with a Django app. Important for what I'm doing here is that each datum has associated tags. Also, most linguistic data is paradigmatic, so I want to be able to have users input an entire paradigm at a time (rather than one single datum at a time, which can be done already using the Django admin interface). I also, ultimately, want to have the paradigmatic input be dynamic in the future, so that I do not have to hardcode things.
For example, I would like have a form that looks like the following:
On submit, I would like the page to send something like this to the server:
'I', tags:'singular, 1st'
'we', tags:'plural, 1st'
ˈYou',tags:'singular, 2nd,masculine'
...
I simply don't know what the best way is to embed this extra data into the website so that it gets submitted when the page gets submitted. Ideally, the input boxes could somehow 'inherit' the tags based on their position in the columns and rows rather than each input box needing to be have that information added individually.
The only idea I've had so far is to embed the tag information into the names of the input boxes so that the string passed on a get command would be something like '?singular_1st=I&plural_1st=we&singular_2nd_masculine=You' and I would just have to process the strings on the Django backend. I could also use javascript to assign the names of each of the textboxes on pageload somehow, though I'm not sure how to figure out which textbox is located where relative to the headers of the table.
What is the best way to do this? Can I somehow pass more structured data on submitting the page, rather than having to reparse everything on the backend? I've read something about passing JSON to the server, but I'm not sure how exactly to do this. Similarly, there's a data attribute in HTML 5, but I'm not sure how (or if) that gets passed to the server on submitting a page. I'm not asking for anyone to do my coding for me, but simply a suggestion about how best to do this and further resources or tutorials showing similar projects.

json_encode(): Sending both a Table and Variables from PHP to JavaScript

From my PHP script I have to send to JavaScript two pieces of data in one AJAX return call:
relatively large array with app. 200 rows and 10 columns
set of variables providing additional information about that array
Both pieces of information should be returned from PHP to JavaScript in one JavaScript return call.
A standard way of sending arrays from PHP is by using json_encode(). This function however accepts only one parameter, either a string or an array.
How to, please, send both the table and variables from PHP to JavaScript then? If json_encode() wont handle this task, is there any other reliable way?
I have tried combining the variable set and the table into one array, and passing that array to JavaScript. This worked, however, when I wanted to get lenght of such array, where table and variables were combined, it simply did not work. Which is the reason, why I came to consult this problem here.
Thank you very much.
You can package the table array and the variable set array in to one single array:
$package = Array($dbarray, $vars);
$json_data = json_encode($package);

Passing a PHP array via Javascript to another PHP script

I may be going about this the hard way, as I modeled my code after an older article: populating triple dropdown list but I have found I don't need to query the database, because all the information is already neatly stored for me in a PHP array used earlier in the script.
I have a PHP array already created ($events):
<?php foreach($events as $event) {
Now I have to pass the events array to another file for processing:
<select name="search_month" onChange="getMonth('filter-leaderboard.php?month='+this.value)">
I'm just not sure how to pass it via Javascript.
How do I go about doing that?
Thanks!
Looking at your question, it appears you're trying to get the month using AJAX?
If so, it'd probably be better to post the array as the body rather than doing a get request.
You can use the json_encode function (http://au2.php.net/json_encode) to convert the array into a JSON object so you can work with it in JavaScript.

Get array from separate PHP file and store into jQuery array

I've searched around and wasn't able to find a clear path for accomplishing this.
I'm familiar with using json_encode() and json_decode() for converting php arrays to and from json, however I'm stumped about the below scenario:
I have a user interactive page where they need to access and use data from one of dozens of php array files. I have a php file (get-array.php) that determines which array to send back based on what the user has been selecting.
Normally I would just use .load() to load php results into a div, but I need the array back and stored in a jQuery array. What would be the best method for doing this?
Many thanks!
Use http://api.jquery.com/jQuery.ajax/ or the shorthand method, http://api.jquery.com/jQuery.get/ to retrieve JSON.
Or even better, this shorthand method is made just for this: http://api.jquery.com/jQuery.getJSON/
When you have your PHP array a d are ready to send it, you can use the JSON encode method of PHP to prepare it as JSON: http://php.net/manual/en/function.json-encode.php

How to do Javascript access a local database in txt format

I am newbie working on a 100% js prototype. It consist of 3 docs: an html page full of xml tags, a small dictionary in a text file format, and a js file with jquery.
The js needs to parse the xml tags (no problem here) and look into the mini-dictionary list for available translations.
Which is the best way to implement the mini-dictionary list. (No more than 50.000 records). Is there a way to load the list into a memory database and access it from js? Which is the usual path to take in this case? What is the simplest and machine-independent way to do this?
Any directions as to where should I research are greatly appreciated.
I would suggest encoding mini-dictionary with JSON data format, and then using AJAX to get that file and parse it. But then you are risking someone will just copy whole dictionary and steal your work.
That is, if you are not using server side language, like PHP. If you are using it, then just store everything into database and request just specific words with AJAX.

Categories

Resources