如何从 JavaScript 和 Grails 访问变量?

发布于 2024-09-27 11:02:37 字数 975 浏览 5 评论 0原文

我有一个 Grails 变量,其类型为 JASONList,在模板中呈现。

有没有办法从 JavaScript 函数内部访问这个列表?

假设我想要 onresize 来适应屏幕上的所有对象。无需进行数据库调用并从 Ajax 重新获取整个列表...

假设模板执行类似以下操作:

<g:each var="report" in="${reportList?.myArrayList}">
  <li style="display:inline; list-style:none;">
    <img src="  ${report?.img}">
  </li>
</g:each>
<script type="text/javascript">
    function resize(list) {
        if (list.size <givenSize) // Pseudocode
            list.subList() // Pseudocode
    }
    window.onresize = resize("${reportList}")
</script>

这样做的问题是,出于某种原因,Grails gsp 不会将“${reportList}”呈现为列表。相反,它将其呈现为字符串“${reportList}”。

我可能认为这个问题完全错误,但是有没有办法调整这些对象的大小或通过 document.getElementById 或类似性质的东西获取它们?

$reportList 由 POJO 作为 JSON 转换填充...

I have a Grails variable which is of type JASONList that is rendered in a template.

Is there a way to access this list from inside a JavaScript function?

Let's say I want onresize to fit all the objects on the screen. Without making a database call and refetching the entire list from Ajax...

Let's say the template does something like this:

<g:each var="report" in="${reportList?.myArrayList}">
  <li style="display:inline; list-style:none;">
    <img src="  ${report?.img}">
  </li>
</g:each>
<script type="text/javascript">
    function resize(list) {
        if (list.size <givenSize) // Pseudocode
            list.subList() // Pseudocode
    }
    window.onresize = resize("${reportList}")
</script>

The problem with this is that for some reason Grails gsp does not render "${reportList}" as a list. Instead it renders it as the string "${reportList}".

I am probably thinking of this problem completely wrong, but is there a way to resize these objects or get them through document.getElementById or something of that nature?

The $reportList is populated by POJO as JSON conversion...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坠似风落 2024-10-04 11:02:37

Grails 变量仅存在于服务器端。 JavaScript 在浏览器(客户端)中运行。发送到浏览器的所有内容都是字符串,因此虽然您可以使用 Grails 生成一段 JavaScript,例如 window.onresize = resize("${reportList}"),但浏览器只会看到${reportList} 计算结果的字符串

这意味着,如果您使用 Grails 将变量传递给 resize() JavaScript 函数,则参数 (list) 将只是一个字符串 - 您不会能够访问服务器端列表方法,例如 list.sizelist.subList(),因为 list 变量是 no更长的清单;它只是一个字符串

Grails variables only exist on the server side. JavaScript runs in the browser (client side). Everything that's sent to the browser is a string, so while you can use Grails to generate a piece of JavaScript like window.onresize = resize("${reportList}"), the browser will only see the string that ${reportList} evaluates to.

That means that, if you use Grails to pass a variable to the resize() JavaScript function, the parameter (list) will only ever be a string - you won't be able to access server-side list methods like list.size or list.subList(), because the list variable is no longer a list; it's just a string.

慕烟庭风 2024-10-04 11:02:37

我不知道,但也许 Grails 不想计算 script 标记内的表达式。动态生成的脚本并不是一个很好的做法。

但在找到确切原因之前,您可以尝试如下操作:

<g:each var="report" in="${reportList?.myArrayList}">
  <li style="display:inline; list-style:none;">
    <img src="  ${report?.img}">
  </li>
</g:each>
<%= """<script type=\"text/javascript\">
function resize(list){
  if(list.size <givenSize) //posudo code
     list.subList() // psudocode
}

window.onresize = resize(\"$reportList\")
</script>""" %>

I don't know, but maybe Grails doesn't want to evaluate expressions inside script tags. Dynamically generated scripts is not a very good practice.

But until you find the exact cause, you could try something like this:

<g:each var="report" in="${reportList?.myArrayList}">
  <li style="display:inline; list-style:none;">
    <img src="  ${report?.img}">
  </li>
</g:each>
<%= """<script type=\"text/javascript\">
function resize(list){
  if(list.size <givenSize) //posudo code
     list.subList() // psudocode
}

window.onresize = resize(\"$reportList\")
</script>""" %>
世俗缘 2024-10-04 11:02:37

我不确定为什么你的 ${reportList} 被渲染为 ${reportList},因为当我执行以下操作时:

var t = "${taskList}";

我在 HTML 中得到以下内容:

var t = "[com.wbr.highbar.Task : 1, com.wbr.highbar.Task : 4, com.wbr.highbar.Task : 2, com.wbr.highbar.Task : 5]";

也就是说,你仍然会遇到问题,因为 JavaScript 会不知道如何处理您的报告列表。如果它是纯 JSON,则需要对其进行 eval() 以便将其转换为 JavaScript 对象。

I'm not sure why your ${reportList} is being rendered as ${reportList}, because when I do the following:

var t = "${taskList}";

I get the following in my HTML:

var t = "[com.wbr.highbar.Task : 1, com.wbr.highbar.Task : 4, com.wbr.highbar.Task : 2, com.wbr.highbar.Task : 5]";

That said, you're still going to have issues, because JavaScript will have no idea what to do with your reportList. If it is pure JSON, you would need to eval() it so that it gets turned into a JavaScript Object.

于我来说 2024-10-04 11:02:37

我找到了我的问题。基本上,如果您在 Grails 中使用 POJO,Grails 的 JSON 转换就不是很智能。它所做的只是对象上的 toString,而不是潜在地查看所有公共访问器等。

这有点令人失望,但基本上我需要在 POJO 的 toString 方法中创建 JSON 转换。

I figured out my problem. Basically if you are using POJO in Grails the Grails as JSON conversion is not very smart. All it does is a toString on the object instead of potentially looking at all the public accessors, etc.

It is kind of disappointing, but basically I need to create the JSON conversion in the toString method of my POJO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文