简体   繁体   English

Grails在GSP中更新列表(查看),然后使用按钮提交

[英]Grails Update a List in GSP (View) and then submit with a button

Imagine you have a table displaying a list of books (like the index does) and you want to make changes in a column called "Sold" (a checkbox for example). 想象一下,您有一个表显示一个书籍列表(如索引一样),并且您想在名为“已售出”的列中进行更改(例如,一个复选框)。 So once you finish "Checking" the books you have sold, you click a Button to save! 因此,一旦完成“检查”已售出的书籍,您就可以单击一个按钮进行保存! How do you send back that list to the controller and update it? 您如何将该列表发回控制器并进行更新?

So, the code is something like this, in the controller: 因此,在控制器中,代码是这样的:

def aMethod(){ ... [bookInstanceList: myBookList] } def aMethod(){... [bookInstanceList:myBookList]}

In the GSP: 在GSP中:

    <g:each in="${bookInstanceList}" status="i" var="bookInstance">
        <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">

            <td><g:link action="show" id="${bookInstance.id}">${fieldValue(bean: bookInstance, field: "author")}</g:link></td>
            <td><g:checkBox name="sold" value="${bookInstance?.sold}" /></td>
            <td>
        </tr>
    </g:each>

The idea is with the checkbox let the user change the "Sold" value from that book, and then make a submit with a Button. 这个想法是通过复选框允许用户更改该书的“已售”值,然后使用“按钮”提交。 How can I save my new bookInstanceList? 如何保存新的bookInstanceList?

Thank you very much 非常感谢你

There is a simple sample app at https://github.com/jeffbrown/books which shows one way you could do this. https://github.com/jeffbrown/books有一个简单的示例应用程序,它显示了您可以执行此操作的一种方法。 Run the app, open the default index page, click on the link and that will take you to a page where you can click checkboxes and update the library of books. 运行该应用程序,打开默认的索引页面,单击链接,您将进入一个页面,您可以在其中单击复选框并更新图书库。

Files of interest are https://github.com/jeffbrown/books/blob/master/grails-app/controllers/com/demo/BookController.groovy and https://github.com/jeffbrown/books/blob/master/grails-app/views/book/index.gsp . 感兴趣的文件是https://github.com/jeffbrown/books/blob/master/grails-app/controllers/com/demo/BookController.groovyhttps://github.com/jeffbrown/books/blob/master/ grails-app / views / book / index.gsp

I hope that helps. 希望对您有所帮助。

I've removed some of the markup for brevity and to bring focus to the important bits. 为了简洁起见,我删除了一些标记,并将重点放在重要的部分上。 This is one way of doing it without relying on Javascript and what do you know, it is pure grails. 这是不依赖Java脚本的一种实现方式,您知道的是,这完全是grails。 Never say never, @rmlan. 永远不要说永远,@ rmlan。

<g:form action="updateSold" controller="book">
   <table>
      <tbody>
         <g:each in="${bookInstanceList}" status="i" var="bookInstance">
            <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
               <td><g:link action="show" id="${bookInstance.id}">${fieldValue(bean: bookInstance, field: "title")}</g:link></td>
               <td>
                   <g:checkBox name="sold" value="${bookInstance.sold}" />
                   <g:hiddenField name="id" value="${bookInstance.id}" />
               </td>
           </tr>
        </g:each>
     </tbody>
   </table>
   <g:submitButton name="updateSold" value="Update" />
</g:form>

This is the controller action: 这是控制器操作:

def updateSold() {
   def solds = params.list('sold')
   def ids = params.list('id')

   ids.eachWithIndex { id, idx ->
      if (solds[idx]) {
          // the book's sold has been checked, so update it to TRUE           
      } else {
         // the books sold has not been checked, so update it to FALSE
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM