Question

my image.. http://www.4shared.com/photo/Rj_0Ymdt/1111.html this is my coding in controller

def simpan(){
def banklimit = new BankLimit()
    for(int i = 0; i<params.limitPerDay.size();i++) {
        def baaa = CurrencyList.list(params)
        banklimit.dayLimit = params.limitPerDay[i].toBigDecimal()
        banklimit.alertLimit = (banklimit.dayLimit*0.8)     
        banklimit.currency = CurrencyList.findBySym(baaa.sym[i])
        banklimit.save()
    }
    return banklimit
}

<table>
            <thead>
                <tr>                        
                        <g:sortableColumn property="sym" title="${message(code: 'banklimit.currency.sym.label', default: 'Simbol')}" />                     
                        <g:sortableColumn property="limitday" title="${message(code: 'banklimit.dayLimit.label', default: 'Limit/Day')}" />                     
                </tr>           
            </thead>
            <tbody>
            <g:each in="${aaa}" status="i" var="bbb">
                <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">

                    <td id="${bbb.id}"> ${fieldValue(bean: bbb, field: "sym")}</td>
                    <td  id="${bbb.id}" colspan="2"> <g:textField name="limitPerDay" value="${value}" /></td>               
                </tr>

            </g:each>   
                <tr>
                    <td></td>
                    <td><g:submitButton name="save" value="SAVE" /> <g:actionSubmit action="back" value="BACK" /></td>
                </tr>           
            </tbody>
        </table>

i want to save the field on coloum limit /day if i click "SAVE" buttons. based my coding, when i click save, only the last row was save in database... example : theres 4 row.. row 1,2,3,4 filled, then i click save..why only 4th rows save in database? row 1 ,2,3 arenot saving?

Was it helpful?

Solution

You have just one banklimit = new BankLimit() but saving it 4 times. it's one object saved 4 times with different values in loop.

You need:

for(int i = 0; i<params.limitPerDay.size();i++) {
    def banklimit = new BankLimit()
    //.... fill with values
    banklimit.save()
}

For client side it's better to make:

<g:each in="${aaa}" status="i" var="bbb">
    <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
        <td>${fieldValue(bean: bbb, field: "sym")}</td>
        <td colspan="2"><g:textField name="limitPerDay[${i}]" value="${value}" /></td>               
    </tr>
</g:each>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top