Pass value from javascript to servlet

9,885 views
Skip to first unread message

mrishadali

unread,
May 8, 2012, 11:22:43 AM5/8/12
to Google Visualization API
Hi,
I want to pass a selected value from the table to the servlet.

var row = table.getSelection()[0].row;
var selection = data.getValue(row, 1);

I want to pass the var "selection" to the servlet.
How do I do that?
Thanks in advance.

asgallant

unread,
May 8, 2012, 11:48:26 AM5/8/12
to google-visua...@googlegroups.com
You could pass that with an AJAX call to the servlet, something like this (with help from jQuery):


var row table.getSelection()[0].row
var selection data.getValue(row1);

$.ajax({
    url'path/to/servlet',
    data{
        postVariableNameselection
    },
    type'POST'
}); 

You can have the servlet retrieve the data from the 'postVariableName' POST variable.

mrishadali

unread,
May 9, 2012, 5:27:55 AM5/9/12
to google-visua...@googlegroups.com
Thanks a lot for your help.... it worked.
I can print the value from the servlet
but there is another problem
I can't forward to another jsp.....

<code>
id = request.getParameter("userID");
System.out.println("Selected ID : "+id);

request.setAttribute("newID", id);

RequestDispatcher rd = request.getRequestDispatcher("/newpage.jsp");
rd.forward(request, response);
<code>

if I call this servlet from a button, it works.
but its not forwarding to another JSP from the ajax code.
is there any different way to do that????

Thanks again

asgallant

unread,
May 9, 2012, 9:33:11 AM5/9/12
to google-visua...@googlegroups.com
I'm sorry, but I can't help you there.  Maybe someone else here knows?

You might also have better luck asking in a Java forum.

mrishadali

unread,
May 9, 2012, 9:56:47 AM5/9/12
to google-visua...@googlegroups.com
No problem, 
Thanks a lot anyway.
I've managed to do that by calling "doPost()" method from the ajax code (you provided) and using a global variable for selected value, then calling the "doGet()" method from the form submit button.
But I don't think its a good approach.
Thanks again

On Tuesday, May 8, 2012 4:22:43 PM UTC+1, mrishadali wrote:

mrishadali

unread,
May 9, 2012, 11:31:10 AM5/9/12
to google-visua...@googlegroups.com
Can I ask you something...
Do you work for google??? Or you get paid for this???
You are doing a wonderful job...
Thanks a lot...:)


On Tuesday, May 8, 2012 4:22:43 PM UTC+1, mrishadali wrote:

asgallant

unread,
May 9, 2012, 2:15:12 PM5/9/12
to google-visua...@googlegroups.com
No, I don't work for Google, and I don't get paid for this.  I just like to help >;o)

mrishadali

unread,
May 18, 2012, 9:50:46 AM5/18/12
to google-visua...@googlegroups.com
Hi asgallant,
You have been very helpfull so far. Can you please give me a sample code to pass multiple selected rows. 
I can pass the column values of the selected rows (as you instructed) but how can I get the multiple selected rows. 
Thank you.


On Tuesday, May 8, 2012 4:22:43 PM UTC+1, mrishadali wrote:

asgallant

unread,
May 18, 2012, 10:25:08 AM5/18/12
to google-visua...@googlegroups.com
The #getSelection method returns an array of selected elements, so if multiple rows are selected, then you just need to parse the array for each row:

var sel table.getSelection();
var postData {};
for (var 0sel.lengthi++{
    // this is going to vary depending on how java handles POST variables
    // PHP allows you to enter arrays of data using object names formatted
    // like: 'myDataArray[0]', 'myDataArray[1]', 'myDataArray[2]', etc
    postData['postArray[' ']'data.getValue(sel[i].row1);
}

$.ajax({
    url'path/to/servlet',
    datapostData,
    type'POST'
}) 

Java might not allow you to get arrays of data that way, but it must have some means of doing it.

mrishadali

unread,
May 21, 2012, 8:17:23 AM5/21/12
to google-visua...@googlegroups.com
Hi, 
yeah.. cant receive the arrays in java but saving all rows in one string separated by a separator and then getting it in servlet.
Thanks  anyway...
 
On Tuesday, May 8, 2012 4:22:43 PM UTC+1, mrishadali wrote:

mrishadali

unread,
May 23, 2012, 4:25:57 AM5/23/12
to google-visua...@googlegroups.com
Hi, 
Is there a way restrict a user to select only one row in a table?
Thanks 
 

On Tuesday, May 8, 2012 4:22:43 PM UTC+1, mrishadali wrote:

mrishadali

unread,
May 23, 2012, 9:36:46 AM5/23/12
to google-visua...@googlegroups.com
I want to disable other rows when one row is selected.....
Can anyone please help


On Tuesday, May 8, 2012 4:22:43 PM UTC+1, mrishadali wrote:
On Tuesday, May 8, 2012 4:22:43 PM UTC+1, mrishadali wrote:

asgallant

unread,
May 23, 2012, 9:54:42 AM5/23/12
to google-visua...@googlegroups.com
You'll have to track the currently selected row and remove it from further selections:

​var selectedRow;
google.visualization.events.addListener(table'select'function ({
    var selection table.getSelection();
    // if any rows are selected, remove the old selected row from
    // the array and set the new selected row
    // otherwise, set the selected row to null
    if (selection.length 0{
        // if the currently selected row is not null and is still
        // in the selection, remove it from the selection
        if (selectedRow != null && selection.indexOf(selectedRow!= -1{
            selection.splice(selection.indexOf(selectedRow)1);
        }
        // set the selected row to the first element in the selection
        // there should never be more than one element in the array
        // at this point
        selectedRow selection[0];
        table.setSelection(selection);
    }
    else {
        selectedRow null;
    }
}); 

mrishadali

unread,
May 24, 2012, 5:42:48 AM5/24/12
to google-visua...@googlegroups.com
The easy way is to put an alert if (selection.length > 1) and set the selection to previous selected row but I wanted to disable the other rows when one is selected.
Because I am doing some in-line edit (users can edit one row) what i want is when one row is selected for editing others should be disable and when its done change the display as normal.
Thanks anyway.
You've helped me a lot.

On Tuesday, May 8, 2012 4:22:43 PM UTC+1, mrishadali wrote:

Devendra Patel

unread,
Feb 20, 2014, 2:04:12 AM2/20/14
to google-visua...@googlegroups.com
can i pass muliple variable in this ajax method. 

asgallant

unread,
Feb 20, 2014, 11:01:54 AM2/20/14
to google-visua...@googlegroups.com
Do you mean passing multiple variables in this AJAX call?

$.ajax({
    url: 'path/to/servlet',
    data: {
        postVariableName: selection
    },
    type: 'POST'
});​

Yes, you can.  Just add them to the "data" parameter:

$.ajax({
    url: 'path/to/servlet',
    data: {
        foo: false,
        bar: 'banana',
        cad: 100
    },
    type: 'POST'
});​
Reply all
Reply to author
Forward
0 new messages