Grails g:选择设置selectedIndex

时间:2010-02-04 01:04:08

标签: list forms grails selectedindex

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

我尝试在flash中传递新对象,但我无法弄清楚如何在用于生成g:select数据的列表中获取它的索引。

1 个答案:

答案 0 :(得分:2)

假设您在控制器级别的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>
相关问题