【问题标题】:Grails refresh divGrails 刷新 div
【发布时间】:2011-12-10 23:30:07
【问题描述】:

我的网站上有这样的东西:

<body>
<div id="1">
content1
</div>
<div id="2">
content2
</div>

<g:render template="/layouts/aa1"/>

<g:form name="myForm" action="myaction">
<g:submitButton name="update" value="Update" />
</g:form>
</body>

我的 aa1 布局是这样的:

<div id="3">
       <g:each in="print">
       ${it.carModel}
       </g:each>

</div>

控制器是:

def updateDiv = {
def max = 20
def offset = 0 
   if(params=["myForm"]){
       def print = Cars.list(max: max, offset: offset + 10)
   }
render(template:'aa1', [print: print])
}

我想要做的是,当我单击“更新”时,g:render 会再次被调用并使用 ajax 进行更新(仅更新该部分),并且值会发生变化。每次单击,都会显示接下来的 20 辆汽车。我怎样才能做到这一点?我的方向正确吗?

【问题讨论】:

    标签: ajax grails html


    【解决方案1】:

    Grails 有一个标签formRemote 可以为您执行此操作。 基本上,不是使用常规的form 标记,而是使用formRemote 标记,指定要调用的控制器和操作以及要使用结果更新的dom 元素。 你也可以看看submitToRemote标签,它做了类似的事情。

    我已经删除了下面的一些代码,以帮助您朝着正确的方向前进。它没有经过测试,因此可能存在错误

    控制器:

    def updateDiv = {
        def max = 20
        def offset = 0 
    
        if(params=["myForm"]){
           def print = Cars.list(max: max, offset: offset + 10)
        }
    
        render(template:'aa1', collection:print, var:'car')
    }
    

    普惠制:

    <body>
        <div id="1">
            content1
        </div>
        <div id="2">
            content2
        </div>
    
        <g:formRemote name="myForm" on404="alert('not found!')" update="3"
                  url="[ controller: 'YourControllerName', action:'updateDiv' ]">
            Book Id: <input name="id" type="text"></input>
        </g:formRemote>
    
        <div id="3">
             <g:render id="" template="/layouts/aa1" collection="${print}"/>
        </div>
    </body>
    

    模板:

    ${car.carModel}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      • 1970-01-01
      • 2011-01-05
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多