javajavascriptgwtjsnigwt-exporter

GWT - passing Java arrays to Javascript


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


Solution

  • 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>