Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8281441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:01:58+00:00 2026-06-08T10:01:58+00:00

So, the _form.gsp template associated with my create.gsp creates an initial table from a

  • 0

So, the _form.gsp template associated with my create.gsp creates an initial table from a template for the row as follows:

<table id="myTable">
<!-- define table headers here -->
<g:each var="i" in="${1..5}">
    <g:render template="tableRow" model="['i': i]" />
</g:each>
</table>

What I’d like to do is add a button or a link underneath that table that let’s you add five more rows, while keeping all the data you’ve entered in the form so far.

I can see how that’s possible in “pure” javascript, but I’d basically have to repeat the _myTable.gsp HTML in my javascript file. I’d like to avoid that (DRY, etc.).

How can I do that?

Edit
So, I tried Gregg’s solution (below). Here’s what I came up with.

The Controller has an action:

def addMoreRows() {
    println params
    def i = params.rowNumber + 1
    def j = i+5
    println "i is set to " + i
    render(template: "foapRow", bean:i, var:i, model: ['rowStart': i, 'rowEnd': j])
    }

The create.gsp page calls the _form.gsp as normal, adding a rowStart and a rowEnd to the model.

create.gsp

<g:render template="form" model="['userId':userId, 'rowStart':1, 'rowEnd':5]"/>

*_form.gsp*, in turn, passes those parameters on to the row template, and creates a link to call the above controller action. It also has the javascript Gregg recommended:

<script type="text/javascript">
    $("#addRowsLink").on("click", function(e) {
        e.preventDefault();
            $.get("/Controller/addMoreRows", function(html) {
                $("#theTableInQuestion>tbody").append(html);
            });
        });
    </script>
    <table>
    ...
        <g:render template="tableRow" model="['rowStart':1, 'rowEnd':5]"/>
    </table>
    <g:remoteLink id="addRowsLink" action="addMoreRows" update="theTableInQuestion" onSuccess="addRows(#theTableInQuestion, data, textStatus)" params="['rowNumber':data]">Add More Rows</g:remoteLink>

The *_tableRow.gsp* begins and ends with:

<g:each var="i" in="${rowStart..rowEnd}">
    <tr>
    ...
    </tr>
</g:each>

From a previous attempt, I have this function in my included javascript file:

function addRows(tableId, rowCode, status) {
    $(tableId + ' tr:last').after(rowCode);
}

Right now, when I click the “Add More Rows” link, I still get taken to a new page, and it only has one row on it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:02:01+00:00Added an answer on June 8, 2026 at 10:02 am

    One possible solution. You’re going to need to change your template so it does the looping:

    GSP:

    <table id="myTable">
    <tbody>
        <g:render template="tableRows" model="[loopCount:loopCount, moreData:moreData]" />
    </tbody>
    </table>
    

    Template:

    <g:each in="${loopCount}" var="idx">
      <tr>
         <td>.....</td>
         ......
      </tr>
    </g:each>
    

    JavaScript:

    $("#someButtonId").on("click", function(e) {
      e.preventDefault();
      $.get("/controller/someAction", function(html) {
        $("#myTable>tbody").append(html);
      });
    });
    

    Controller:

    def someAction = {
      // logic here
      render template: "tableRows", model="[loopCount: 5, moreData:moreData]"
    }
    

    You could also submit all the data in your table to the server every time and refresh the entire page, adding logic to loop over some variable number of rows. But you would need to collect all that data on the server and make sure it gets put back in the request.

    There’s probably a dozen ways to do this so don’t be surprised if you get that many answers. :o)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get the value from outside the form. My gsp looks like
I have a form submission that is rendering a gsp template and inserting it
why is a declaration of 'g:javascript library=xyz'-tag in a gsp or template switch off
I have a modal dialog where I load a form from a gsp and
the gsp <g:form controller=sort action=filteren> <g:select from=${functionremote.Sort.list() } onchange=this.form.submit() value=${flash.filter} name=filter optionKey=id noSelection=['':'selecteer']/> </g:form>
Form is made from a function that prints out all rows that are awaiting
Form has the DoubleBuffered property (bool, inherited from Control). If this is set to
Im creating a GSP form that i wish to submit using the $.ajax() call.
In my GSP page, I have this form: <g:form action=ajaxAdd> <g:textArea id='postContent' name=content rows=3
On index.gsp I have this to redirect it to list.gsp so I'm imagining is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.