简体   繁体   English

Grails如何使AJAX发送从模板中以表格形式编辑的对象

[英]Grails how make AJAX send the object that is edited in the form from a template

I want to update a field in the edit-form when another field is changed. 我想在更改另一个字段时更新编辑表单中的一个字段。 When the field1 is changed an AJAX-script will call the controller that calculates a new value for field2 and then render the template that updates this field. 更改field1时,AJAX脚本将调用控制器,该控制器为field2计算一个新值,然后呈现更新该字段的模板。 But the controller needs two values, one is the value of field1 and one is the object that is edited. 但是控制器需要两个值,一个是field1的值,一个是被编辑的对象。 I need it to make it possible to render the template. 我需要它来渲染模板。

    <script>
        $(document).ready(function(){
            $( document ).on('change', '.availableCert', function ( event ){ 
                $.ajax({
                    url: '${g.createLink( controller:'offerDetail', action:'updatePrice' )}',
                    data: {availableCert:this.value, id:this.id},
                    type: 'get'
                }).success( function ( data ) { $( '#updatePrice' ).html( data );     });
            });
        });
    </script>

    <script>
        $(document).ready(function(){
            $( document ).on('change', '.adjustPrice', function ( event ){ 
                $.ajax({
                    url: '${g.createLink( controller:'offerDetail', action:'updatePrice' )}',
                    data: {adjustPrice:this.value, id:this.id},
                    type: 'get'
                }).success( function ( data ) { $( '#updatePrice' ).html( data );     });
            });
        });
    </script>

    <script>
        $(document).ready(function(){
            $( document ).on('change', '.volumeOffered', function ( event ){ 
                $.ajax({
                    url: '${g.createLink( controller:'offerDetail', action:'updatePrice' )}',
                    data: {volumeOffered:this.value, id:this.id},
                    type: 'get'
                }).success( function ( data ) { $( '#updatePrice' ).html( data );     });
            });
        });
    </script>

part of the template: 模板的一部分:

        <g:select class="availableCert" name="availableCert" from="${offerDetail.availableCert}" value="${offerDetail.choosedCert}" />    
</td>        
<td> FSC: ${offerDetail.priceFSC}</td>
<td> UC: ${offerDetail.priceUC}</td>
<td> CW: ${offerDetail.priceCW}</td>
<td> PEFC: ${offerDetail.pricePEFC}</td>
<td>  
    EndPrice:    ${offerDetail.endPrice}
</td>

So when the controller wants to render this field he needs to have the object ${offerDetail} to make it possible for the template to render it. 因此,当控制器想要渲染该字段时,他需要具有对象$ {offerDetail},以使模板可以渲染它。

I can't see how the javascript can retrieve this object. 我看不到javascript如何检索此对象。 But it maybe much simpler than I can imagine or? 但这也许比我想象的简单得多?

Controller code: 控制器代码:

def updatePrice() {
   def OfferDetail od
    if (params.id != null){
       od = OfferDetail.get(params.id)
   }
    if (params.availableCert != null) {
       od.choosedCert = params.availableCert
   } else if (params.adjustPrice != null) {
       od.priceAdjust = params.adjustPrice.toBigDecimal()
   } else if (params.volumeOffered != null) {
       od.volumeOffered = params.volumeOffered
   } else {}
   render template: "offerDData", model: [offerDetail:od]
}

I think you can streamline your approach with dropdowns and have a single javascript function to handle all dropdown changes eg 我认为您可以通过下拉菜单简化方法,并具有一个javascript函数来处理所有下拉菜单更改,例如

<script>
    $(document).ready(function(){
        $( document ).on('change', '.odChange', function ( event ){ 
            $.ajax({
                url: '${g.createLink( controller:'offerDetail', action:'updatePrice' )}',
                data: {  updateVal :this.value, 
                         dropDownId: this.id,
                         offerDetailId: od.id
                      },
                type: 'get'
            }).success( function ( data ) { $( '#updatePrice' ).html( data );     });
        });
    });
</script>

<g:select class="odChange" name="availableCert" from="${offerDetail.availableCert}" value="${offerDetail.choosedCert}" />
<g:select class="odChange" name="adjustPrice" from="${offerDetail.adjustPrice}" value="${offerDetail.adjustPrice}" />
<g:select class="odChange" name="volumeOffered" from="${offerDetail.volumeOffered}" value="${offerDetail.volumeOffered}" />

It's worth looking at other options for data binding 值得一看其他数据绑定选项

Eg for now you could try: 例如,现在您可以尝试:

def updatePrice() {
   def od = OfferDetail.get( params.offerDetailId )
   od."${params.dropDownId}" = params.updateVal
   od.save( flush: true, failOnError: true )
   render template: "offerDData", model: [offerDetail:od]
}

Using the above it's important that your gsp field names match up with your domain's field names 使用以上内容,请务必将您的gsp字段名称与您的域的字段名称匹配

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

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