GWT, passing an ArrayList to JSNI

2019-09-04 01:40发布

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?

标签: gwt jsni
1条回答
▲ chillily
2楼-- · 2019-09-04 02:22

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[][])

查看更多
登录 后发表回答