本文介绍了GWT,将ArrayList传递给JSNI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的GWT应用程序中,我有一个javascript函数,该函数需要一个array数组作为参数.我使用RPC获得数据,因此获得了List<我的数据库中的列表>.我需要这个,因为我必须填充一种树状视图.例如,我从RPC调用中获得了此信息:{"A","A1","A2"},{"B","B1"},我必须将此传递给我的javascript函数:[["A ," A1," A2],[" B," B1]].在我的界面中,我想显示:

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

如何使用JSNI将其传递给我的javascript函数?

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

推荐答案

如果您可以不使用DevMode(例如使用SuperDevMode),则Java数组与生产模式下的JsArray*相同,因此String[][]为与JsArray<JsArrayString>相同.
在DevMode中,有一个JsArrayUtils可以提供帮助(在DevMode中创建一个副本,在生产模式下按原样返回,没有任何开销),但不适用于嵌套数组(实际上甚至不用于字符串数组),因此在您的情况.

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.

如果您需要/想要列表而不是数组,或者需要DevMode支持,则必须将数据复制到JsArray<JsArrayString>.

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

如果可以使用数组但需要DevMode支持,则可以使用GWT.isScript()进行特定的代码分支:在DevMode中将副本复制到JsArray<JsArrayString>中,在生产模式下按原样传递数组(这也意味着2个JSNI方法,用于JsArray<JsArrayString>String[][])

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

这篇关于GWT,将ArrayList传递给JSNI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 06:52