|
Hello all, I'm struggling with something that's probably really easy, so hopefully someone here can point me in the right direction :-) I'm attempting to create a table for a list of objects using a foreach loop. No problems here, but within this table, the user should be able to change the 'amount' value per 'ShoppingCartItem'. After submitting, I would like to get the original list with the updated amount fields per object. In the snippets below I've copy pasted the relevant code that I've built: Flow right now: 1. User enters a page containing a table with OrderItems 2. User changes the amount values of a few object(s) 3. User presses 'next' 4. Command object 'catches' the new List, validates and maps it back to the ShoppingCart ============================================== The ShoppingCart object: class ShoppingCart implements Serializable { List<ShoppingCartItem> shoppingCartItems = [] } ============================================== The ShoppingCartItem object: class ShoppingCartItem implements Serializable { int amount int productId } ============================================== The part of the .GSP containing the table: <g:each in="${instance?.shoppingCartItems}" status="i" var="shoppingCartItem"> <tr class="${(i % 2) == 0 ? 'odd' : 'even'}"> <td> <g:textField name="shoppingCartItems[${i}].amount" value="${shoppingCartItem?.amount}" /> </td> <td> ${fieldValue(bean: shoppingCartItem, field: "productTitle")} </td> </tr> </g:each> ============================================== The part of the Controller where the Command Object should be initialized: on('next') { AddOrderItemAmountsToReservationCommand command -> if (command.hasErrors()) { flow.command = command return error() } bindData(flow.instance, command) [instance: flow.instance] }.to 'orderSummary' ============================================== The Command Object: class AddOrderItemAmountsToReservationCommand implements Serializable { List shoppingCartItems } The above code doesn't work, but I managed to get it working to a level where I got a list of amounts containing the correct values in the params map. Also, the list inside the Command object isn't initialized but I don't think that should be a problem because I'm basically trying to set the value to an already existing List... Somehow I believe there's something wrong with the name of the textfield. I'm still struggling on how I should tell Grails to reconstruct the original list with updated amounts for each object inside the list :-)
Kind regards, Eric Ettes |
|
<g:textField name="shoppingCartItems[${i}].amount" value="${shoppingCartItem?.amount}" /> Where is 'shoppingCartItems' coming from ? Don't you think you should be using 'shoppingCartItem' object here ? I don't see any object named as 'shoppingCartItems' in your code ? Regards Gaurav Chauhan On Wed, Nov 9, 2011 at 9:53 PM, Eric Ettes <[hidden email]> wrote:
|
|
Hi Gaurav,
Thanks for your reply :-) 'ShoppingCartItems' is the List<ShoppingCartItem> inside the ShoppingCart class (and the AddOrderItemAmountsToReservationCommand). In the code snippets I was trying to see wether Grails would understand that I wanted to map property 'amount' of ShoppingCartItems[i] I also tried to use name="shoppingCartItem.amount" and name="shoppingCartItem[${i}].amount", but even though the properties are correctly inside the params, the propertyresolver won't map them back to the ShoppingCartItem objects in the list... Kind regards, Eric On Wednesday, November 9, 2011 at 5:53 PM, Gaurav Chauhan wrote:
|
|
Did you try this ?
<g:textField name="${shoppingCartItem?.amount}" value="${shoppingCartItem?.amount}" /> Regards Gaurav Chauhan On Wed, Nov 9, 2011 at 10:31 PM, Eric Ettes <[hidden email]> wrote:
|
|
Hi Gaurav,
Thanks for the reply :-) Just gave it a go, but because the object isn't initialized yet (It's an empty basket at that time), it doesn't have a value and thus I'm getting an exception 'Tag [textField] is missing required attribute [name] or [field]' Whenever I use shoppingCartItems.shoppingCartItem[${i}].amount as a name, I can inspect the parameter map and retrieve the values like this: params.'shoppingCartItems.shoppingCartItem[0].amount' params.'shoppingCartItems.shoppingCartItem[1].amount' etc... Same for shoppingCartItem[${i}].amount, only then I can use params.'shoppingCartItem[1].amount' Kind regards, Eric On Wednesday, November 9, 2011 at 6:03 PM, Gaurav Chauhan wrote:
|
| Powered by Nabble | Edit this page |
