在Grails中通过AJAX调用刷新g:每个标记

在Grails中通过AJAX调用刷新g:每个标记,ajax,grails,each,gsp,grails-controller,Ajax,Grails,Each,Gsp,Grails Controller,我有一个统计面板,显示最近注册的用户。我想实现一个AJAX调用来上传面板,而不必重新加载整个页面 我的视图中有以下代码: <g:each in="${lastUsers}" var="user"> <div class="mt-comments"> <div class="mt-comment"> <div class="mt-comment-img"> &

我有一个统计面板,显示最近注册的用户。我想实现一个AJAX调用来上传面板,而不必重新加载整个页面

我的视图中有以下代码

<g:each in="${lastUsers}" var="user">
     <div class="mt-comments">
         <div class="mt-comment">
              <div class="mt-comment-img">
                  <g:if test="${user?.avatar}">
                       <img src="${createLink(controller:'controllerUser',
                       action:'image', id: user?.id)}" />
                  </g:if>
                  <g:else>
                       <img class="img-circle"
                       src="${resource(dir: 'img/profile', file: 'user_profile.png')}"/>
                 </g:else>
              </div>
              <div class="mt-comment-body">
                 <div class="mt-comment-info">
                     <span class="mt-comment-author">${user?.username}</span>
                     <span class="mt-comment-date">
                        <g:formatDate date="${user?.dateCreated}"/>
                     </span>
                 </div>
                 <div class="mt-comment-text">${user?.email}</div>
                    <div class="mt-comment-details">
                       <g:if test="${user?.enabled}">
                       <span class="mt-comment-status label label-sm label-success circle">
                       </g:if>
                       <g:else>
                       <span class="mt-comment-status label label-sm label-info circle">
                       </g:else>
                       <g:formatBoolean boolean="${user?.enabled}"/>
                       </span>
                       <ul class="mt-comment-actions">
                         <li>
                             <g:link controller="user" action="edit" id="${user?.id}">Edit</g:link>
                         </li>
                       </ul>
                  </div>
              </div>
         </div>
    </div>
</g:each>

谢谢您的帮助。

您可以使用grails模板和jQuery加载方法来代替JSON数据。这是一个快速的POC

模板(\u userComments.gsp)

这将作为一个可重用的视图,可以通过AJAX调用,也可以直接包含在其他视图中

<g:each in="${lastUsers}" var="user">
     <div class="mt-comments">
         .....
    </div>
</g:each>
控制器

我们定义了两种方法,一种用于主视图,另一种用于ajax调用

def index() {
    def lastUsers = User.list();
    render(view:'main', model: [lastUsers: lastUsers])
}

// Ajax Call
def userComments() {
    def lastUsers = User.list(); // what ever your fetch logic is
    render(template:'userComments', model: [lastUsers: lastUsers])
}

您可以使用grails模板和jQuery加载方法,而不是使用JSON数据。这是一个快速的POC

模板(\u userComments.gsp)

这将作为一个可重用的视图,可以通过AJAX调用,也可以直接包含在其他视图中

<g:each in="${lastUsers}" var="user">
     <div class="mt-comments">
         .....
    </div>
</g:each>
控制器

我们定义了两种方法,一种用于主视图,另一种用于ajax调用

def index() {
    def lastUsers = User.list();
    render(view:'main', model: [lastUsers: lastUsers])
}

// Ajax Call
def userComments() {
    def lastUsers = User.list(); // what ever your fetch logic is
    render(template:'userComments', model: [lastUsers: lastUsers])
}

@JChap有下面的正确答案。。。您必须认识到,所有g:标记都在服务器端呈现为HTML/javascript等格式,并且只能在服务器上重新呈现。因此,下面AJAX调用的结果是用HTML替换userComments div的内容。@JChap在下面给出了正确答案。。。您必须认识到,所有g:标记都在服务器端呈现为HTML/javascript等格式,并且只能在服务器上重新呈现。因此,下面AJAX调用的结果是用HTML替换userComments div的内容。
def index() {
    def lastUsers = User.list();
    render(view:'main', model: [lastUsers: lastUsers])
}

// Ajax Call
def userComments() {
    def lastUsers = User.list(); // what ever your fetch logic is
    render(template:'userComments', model: [lastUsers: lastUsers])
}