GWT, passing an ArrayList to JSNI

非 Y 不嫁゛ 提交于 2020-01-16 18:20:09

问题


In my GWT application I have a javascript function wich require an array of array as argument. I get the data using RPC, so I get a List< List > from my database. I need this because I have to fill a kind of tree view. For example, I get this from my RPC call: {"A", "A1", "A2"}, {"B", "B1"}, and I have to pass this to my javascript function: [["A", "A1", "A2"], ["B", "B1"]]. In my interface I want to show:

A+
  A1
  A2
B+
  B1

How can I pass it to my javascript function using JSNI?


回答1:


If you can live without DevMode (because you use SuperDevMode for instance), Java arrays are the same as JsArray* in production mode, so String[][] is the same as JsArray<JsArrayString>.
In DevMode, there's JsArrayUtils which can help (makes a copy in DevMode, returns as-is in production mode, with no overhead), but not for nested arrays (and actually not even for arrays of strings), so not in your case.

If you need/want either lists rather than arrays, or DevMode support, then you'll have to copy the data into a JsArray<JsArrayString>.

If you can use arrays but need DevMode support, you can use GWT.isScript() to make a specific code branch: make a copy into a JsArray<JsArrayString> in DevMode, pass the array as-is in prod mode (that also means 2 JSNI methods, for JsArray<JsArrayString> and String[][])



来源:https://stackoverflow.com/questions/12557271/gwt-passing-an-arraylist-to-jsni

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!