Grails g:选择设置 selectedIndex

发布于 2024-08-20 17:09:35 字数 184 浏览 3 评论 0原文

如何在上设置 selectedIndex带有列表中值的标签?我有一个页面允许您添加记录。然后该页面转到包含 ag:select 的视图,我希望 g:select 默认为我刚刚插入数据库的该项目。

我尝试在闪存中传递新对象,但我不知道如何获取它在用于生成 g:select 数据的列表中的索引。

How do you set the selectedIndex on a <g:select> tag with a value from the list? I have a page that allows you to add a record. the page then goes to a view containing a g:select and i want the g:select to default to that item i just inserted into the database.

I tried passing the new object in the flash but i can't figure out how to get it's index in the list being used to generate the g:select data.

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

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

发布评论

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

评论(1

油饼 2024-08-27 17:09:35

假设您在控制器级别的 flash.book 中存储 Book 对象,您的第二页可能如下所示:

<html>
    <head>
        <g:javascript library="prototype" />
        <g:javascript>
              function showLast(selectedId) {
                  if (selectedId) {
                    $('#books option[value=' + selectedId + "]")[0].selected = true;
                  } else {
                    $('books').selectedIndex = 0;
                  }
              };

              Event.observe(window, 'load', init, false);

              function init() {
                  showLast(${flash?.book?.id});
              }
            </g:javascript>
    </head>
    <body>
        <g:select id="books" name="id"
                  from="${Book.list()}"
                  value="title"
                  optionValue="title"
                  optionKey="id"
         />
    </body>
</html>

Supposing you store a Book object in flash.book at the controller level, your second page could look like this :

<html>
    <head>
        <g:javascript library="prototype" />
        <g:javascript>
              function showLast(selectedId) {
                  if (selectedId) {
                    $('#books option[value=' + selectedId + "]")[0].selected = true;
                  } else {
                    $('books').selectedIndex = 0;
                  }
              };

              Event.observe(window, 'load', init, false);

              function init() {
                  showLast(${flash?.book?.id});
              }
            </g:javascript>
    </head>
    <body>
        <g:select id="books" name="id"
                  from="${Book.list()}"
                  value="title"
                  optionValue="title"
                  optionKey="id"
         />
    </body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文