简体   繁体   English

Grails 2.3.x在GSP中更新列表(查看)并将更改保存在数据库中

[英]Grails 2.3.x Updating a List in GSP (View) and save changes in the Database

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 want to save that in the database. 因此,一旦完成“检查”已售出的书籍,就希望将其保存在数据库中。 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]
}

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. 这个想法是通过复选框允许用户更改该书的“已售”值。 How can I save my new bookInstanceList? 如何保存新的bookInstanceList?

Thank you very much 非常感谢你

From what I see you want to dynamically update the backend as the user clicks the check box? 从我看来,您想在用户单击复选框时动态更新后端吗?

If so then you need an ajax call something like this would do it: 如果是这样,那么您需要进行ajax调用,例如:

 <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}" onChange="TriggerFilter(this)" /></td>
                <td>
            </tr>
        </g:each>

<g:javascript>
function TriggerFilter(e) {
   if (e.checked==true) {
     $.get('<g:createLink action='Your_Action' controller="YourController" 
      params="[ filterbind: ''+attrs.filterbind+'',term:''+attrs.term+'' ]"/>'
      ,function(data){
         $('#FilterField').hide().html(data).fadeIn('slow');
       });
   }else{
     $('#FilterField').hide().html('').fadeIn('slow');
   }
}
</g:javascript>



<div id="FilterField" class="filterField">
{Result returned in here}
</div>

You need to fit it e.value into the params take a look at grails java script get createLink there are lots of examples in my plugin here: 您需要将其e.value放入参数中,看看grails Java脚本get createLink,这里的插件中有很多示例:

https://github.com/vahidhedayati/ajaxdependancyselection/tree/master/grails-app/views https://github.com/vahidhedayati/ajaxdependancyselection/tree/master/grails-app/views

and I would suggest looking online/reading on how to use it 我建议在网上查找/阅读使用方法

You may wish to change the get to : 您可能希望将get更改为:

how to use grails ${createLink} in javascript 如何在JavaScript中使用grails $ {createLink}

var url = '${createLink(controller:'Books', action: 'update')}' + e.value ;
$.get(url ,function(data){
             $('#FilterField').hide().html(data).fadeIn('slow');
           });
       }else{
         $('#FilterField').hide().html('').fadeIn('slow');
       }
    }

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

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