Grails传递params并生成形式

时间:2017-01-17 08:21:30

标签: grails

我有一个具有下一个属性的域类

package rentme

class Car {

    String brand
    String model
    String fuelType
    BigDecimal pricePerDay
    String busy

    static constraints = {
        brand(inList:["AUDI", "BMW", "MERCEDES", "NISSAN", "HONDA", "FORD"])
        model()
        fuelType(inList:["FUEL", "DIESEL", "AUTOGAS"])
        pricePerDay(min:0.0, max:1000.0)
        busy(inList:["YES", "NO"])

    }
}

我有car / index.gsp,其中列出了我的条目

<!DOCTYPE html>
<html>
    <head>
        <meta name="layout" content="main" />
        <g:set var="entityName" value="${message(code: 'car.label', default: 'Car')}" />
        <title><g:message code="default.list.label" args="[entityName]" /></title>
    </head>
    <body>
        <a href="#list-car" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
        <div class="nav" role="navigation">
            <ul>
                <li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
                <li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li>
            </ul>
        </div>
        <div id="list-car" class="content scaffold-list" role="main">
            <h1><g:message code="default.list.label" args="[entityName]" /></h1>
            <g:if test="${flash.message}">
                <div class="message" role="status">${flash.message}</div>
            </g:if>
            <f:table collection="${carList}" />


            <div class="pagination">
                <g:paginate total="${carCount ?: 0}" />
            </div>
        </div>
    </body>
</html>

这是我想要添加到每个车辆条目&#34;订单按钮&#34;当点击订购按钮时,我想要这个按钮将创建一个带有所选条目参数的订单表格,因此,生成的表格将是所选条目的数据和客户的额外字段。

1 个答案:

答案 0 :(得分:0)

您需要在结果的each list项中手动迭代。然后,您将在单击时添加所需按钮,它可以是直接链接或jquery

<g:link controller="mycontroller" action="myAction" 
params="${[id:instance.id, whatever:instance.whatever]}">click me</a>

或如该页面上显示的与main page绑定的链接可以是

<span id="myId" data-id="${instance.id}">Click Me</a>
<div id="results">
 <!-- results will end up in here from ajax on same page -->
 <!-- you could look into modal popup or jquery dialog box to load in form -->
  <!-- as a pop up page instead -->
</div>
<script>
    $(function() {  
    $('#myId).on('click',function() {
       var myId=$(this).attr('data-id');
       var url="${createLink(controller: 'myController',action:'doSomething')}";
        $.ajax({
            cache:false,            
            timeout:1000,
            type: 'POST',
            url: url,
            data:{id:myId},
            success: function(data){
                $('#results').html(data);
            }
        });
    })
})
</script>

听起来有很多值得学习的地方