代码之家  ›  专栏  ›  技术社区  ›  user3714598

grails:如何在remoteFunction中传递参数

  •  0
  • user3714598  · 技术社区  · 9 年前

    我想使用grails的remoteFunction获取参数。

    HTML格式

     <table class="table table-hover table-bordered" id="profittable">
                            <thead>
                                <tr>
                                  <th>Date</th>
                                  <th>Profit</th>
                                  <th>Delete?</th>
                                </tr>
                            </thead>
                            <tbody> 
    
                    <g:each in="${dailyProfit}" var="dp">
                        <tr onclick="<g:remoteFunction action='edit' params="[date:${dp.date}]"></g:remoteFunction>" >
                            <td><g:formatDate format="yyyy-MM-dd" date="${dp.date}"/></td>
                            <td>
                                                        <g:formatNumber number="${dp.profit}" type="currency" currencyCode="PHP" format="###.##" />
                                                    </td>
                                                    <td>
                                                        <g:form controller="dailyProfit" action="delete" >
                                                            <g:hiddenField name="date" value="${dp.date.format("yyyy-MM-dd")}" />
                                                            <g:actionSubmit class="delete" value="Delete" >
                                                                <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
                                                            </g:actionSubmit>    
                                                        </g:form>
                                                    </td>
                        </tr>
                    </g:each>
                            </tbody>
                        </table>
    

    错误消息

    URI/SampleGrailsApp/dailyProfit/index类 org.codehaus.groovy.grails.web.taglib.exceptions.grails标记异常 消息属性值引用未关闭(action='edit' params=“[date:${dp.date}]”)。

    要编辑的操作

    remoteFunction标记位于表的每个tr内。计划是,如果单击该行,将显示编辑页面

    def edit() {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        Date date = format.parse(params.date);
        def dailyProfit = DailyProfit.findByDate(date)
        render view:"edit" , model:[dailyProfit : dailyProfit]
    }
    
     def update() {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        Date date = format.parse(params.date);
        def dailyProfit = DailyProfit.findByDate(date)
        if(dailyProfit){
            dailyProfit.properties = params
            dailyProfit.save(flush:true)
        }
        list() 
    }
    

    使用grails的remoteFunction标记传递参数的正确方法是什么?

    2 回复  |  直到 9 年前
        1
  •  1
  •   praveen_programmer    9 年前

    你可以这样做。

    <tr onclick = "${remoteFunction(
        controller: 'xyz',
        action: 'edit',update:'divId',
        params: [date: dp.date])}" >
    
        2
  •  0
  •   Danilo    9 年前

    这也是一个有效的语法:

    <tr onClick="<g:remoteFunction action='edit' params="${[param1: 'value', param2: 0]}"></g:remoteFunction>">.....</tr>
    

    请注意,代码将转换为带有您指定的参数的Ajax调用。作为Ajax调用,您不会看到页面更改。

    如果要在单击该行时将用户发送到编辑页面,请选择以下选项:

    <tr onclick='document.location = "<g:createLink action='edit' params="${[date: dp.date]}"/>" '> ... </tr>