k-deux
k-deux

Reputation: 493

Update multiple instances of a domain object in grails

is it possible to have a table in a single form which shows one instance of a domain object as follows:

(table of domain class "Person" )

Id     Name       LastName
1      John       Doe
2      Jane       Doe
3      Jerry      Doe

Each cell should be an inputfield

the idea is, the table should look like a spreadsheet, I can edit everything and have a single submit button.

If the submit button is pressed all instances of person will be updated.

Do you have an idea how to implement that?

Upvotes: 2

Views: 1011

Answers (1)

Jarred Olson
Jarred Olson

Reputation: 3243

Yes it is possible. You'll have to build a form with X number of rows for creating (or add a button so the user can make more rows). For updating you'd have a fixed number (all of the entries in your database). The HTML would look like this:

<input type="text" readonly="readonly" value="1" name="id_0"/><input type="text" name="name_0"><input type="text" name="lastName_0"/>
<input type="text" readonly="readonly" value="2" name="id_1"/><input type="text" name="name_1"><input type="text" name="lastName_1"/>

The gsp code would vary depending on what exactly you're trying to do. But you would essentially loop over all of your objects and create the above structure for each.

In the controller the params object will look like this:

[id_1:"2", name_0:"John", name_1:"Jane", lastName_1:"Doe", lastName_0:"Doe", id_0:"1"]

I messed with the order on purpose because you can't rely on order so you'll have to match up first names with last names based on their number. So you can retrieve the object based on the id, update the properties you need to and save it. If you need help with the gsp code provide some more code and details and I'd be glad to help.

Upvotes: 1

Related Questions