Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am a newcomer to GWT and Javascript.

I am trying to send in a java int[] to my javascript function. I am using a gwt-exporter to handle the processing for me. This is how I have set things up.

static class gwtExplorerTest implements Exportable {

    @Export("$wnd.handleAnchorClick")
    public static void handleAnchorClick(int param1, int param2 , int[] a1 , int[] a2)
    {
        //process  things here          
    }
}

Can someone help me with javascript code to pass in the arrays I need here? What I currently have is:

href="javascript:window.handleAnchorClick(" + currentRow + "," + currentColumn + "," + rowVals + "," + colVals + ",") "

as my JS function call, where rowVals and colVals are the two arrays I need to pass in. It does not seem to work. Can someone please help me out?

Thanks

share|improve this question
1  
As I can deduce from your code, what you are trying is to pass arrays from your javascript to your java function. aren't you? if so take a look to my answer –  Manolo Carrasco Moñino Apr 19 '13 at 8:22

2 Answers 2

up vote 0 down vote accepted

Your function in java is right and gwt-exporter supports this syntax. The call from JS should be like:

 window.handleAnchorClick(1, 2, [3,4], [5,6])

Your problem is that you are trying to call the exported method from a href attribute in your html and you are using a bad syntax.

First, it is better to use the onClick attribute instead of href, because you dont need the javascript: tag, and it is better to prevent default. And, I'd rather define a function to do the call to avoid syntax issues.

<script>
  var currentRow = 1;
  var currentColumn = 2;
  var rowVals = [3,4];
  var colVals = [5,6];

  function mycall() {
    window.handleAnchorClick(currentRow, currentColumn, rowVals, colVals);
  }
</script>

<!-- I prefer this -->
<a href="#" onClick="javascript:mycall()">click</a>

<!-- But this should work as well -->
<a href="#" onClick="window.handleAnchorClick(currentRow,currentColumn,rowVals,colVals)">click</a>
share|improve this answer
    
Thank you for your guidance. And sorry for the late reply!. –  frodo Apr 22 '13 at 4:23

If you are using json string, then i hope you need to change parameter type to string in handleAnchorClick method. Then you need to typecast to json.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.