Search code examples
ajaxgrails

Grails render template is not responding correctly


I am using Grails 2.4.3. I have an ajax call to get a list of object. The list is ok. It has 10 elements. The problem is that when I want to send the list with model it is not behaving correctly. Nothing is generated in g:each block in my template although I have 10 records.

Here are my attempts below.

My ajax call:

$.ajax({
        type:'POST',
        data:{id: 1},
        url:'/mdNote/getCareTopicTemplate/',
        success:function(data,textStatus){
            $modal.find('.modal-body').empty().append(data);
            $modal.find('.modal-title').empty().append(cfg.title);
            $modal.modal('show');
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){},
        complete:function(XMLHttpRequest,textStatus){}
    });

My controller action:

def getCareTopicTemplate() {
    def careTopicList = mdNoteService.careTopicList()
    render(template: '/md/patient/existingTemplateRadioDiv', model: [careTopicList: careTopicList])
}

My template:

<div class="row" id="existingTemplateRadioDiv">
    <div class="form-group" style="margin-left: 10px;">
        Hello , this is outer text.
        <g:each in="${careTopicList}" var="careTopicList" status="i">
            this is inner text.
        </g:each>
    </div>
</div>

My list in IDE console:

enter image description here

In my view page after render template:

enter image description here


Solution

  • You need to change the name you're giving var in your <g:each> tag. You're telling it to use the same variable, careTopicList, that you're already iterating over. Something like the below should do the trick.

    <g:each in="${careTopicList}" var="careTopic" status="i">