2011-11-19 98 views
1

比方說,我有這個之情況:Grails的複選框處理

enter image description here

但是,讓我們說我有上百個的複選框,這是我需要submiting形式後處理在同一時間的一切。然後,我將需要保存到基於BD一件對箱子進行檢查,每塊

這樣的ID,我需要這樣的:

一)辦法知道哪些複選框被選中,數百名內其中 b)每個複選框都應該與一個即將通過的id鏈接,以便執行特定的操作。

我有一個<g:each>標籤寫我整個表,讀取數據庫中的值。我將不勝感激任何幫助, 在先進的感謝,RR

回答

1

在你的GSP你需要顯示的所有複選框:

<g:each in="${model}" status="i" var="invoiceItem"> 
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}"> 
    <td> 
     <g:checkBox name="invoiceItem_${i}"/> 
    </td> 
    </tr> 
</g:each> 

在控制器動作,您需要選擇複選框映射到您的域對象

List invoiceList = session.invoiceList 
params.each { 
    if (it.key.contains("invoiceItem_")){ 
     if (it.value.contains("on")){ 
      InvoiceItem invoiceItem = invoiceList.get((it.key - "invoiceItem_") as Integer) 
     } 
    } 

}

2

您可以將PARAMS綁定到域對象或命令對象的列表屬性。

查看:

<g:each in="${elements}"> 
    <g:checkBox name="elementSelected[${it.id}]" value="${it.id}" /> 
</g:each> 

Command對象:

class ElementCommand { 
    List elementSelected 
} 

控制器:

def execute = { ElementCommand cmd ->  
    cmd.elementSelected.each { 
     if (it) { 
      processId(it.toInteger()) 
     } 
    } 
}