2012-08-02 2 views
5

Sto provando a configurare una tabella dati con JSF che contiene caselle per il totale di righe che somma il totale di righe su Cambia di qualsiasi casella nella riga. la riga si presenta come:Azione JSF da onChange

<tr id="input_row_1"> 
<td id="mondayInput1"> 
    <h:inputText id="monday1" size="4" value="#{timesheet.monday1}" required="false" /> 
    <!-- <input name="monday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> --> 
</td> 
<td id="tuesdayInput1"> 
    <h:inputText id="tuesday1" size="4" value="#{timesheet.tuesday1}" required="false" /> 
    <!--<input name="tuesday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> --> 
</td> 
<td id="wednesdayInput1"> 
    <h:inputText id="wednesday1" size="4" value="#{timesheet.wednesday1}" required="false" /> 
    <!--<input name="wednesday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> --> 
</td> 
<td id="thursdayInput1"> 
    <h:inputText id="thursday1" size="4" value="#{timesheet.thursday1}" required="false" /> 
    <!--<input name="thursday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> --> 
</td> 
<td id="fridayInput1"> 
    <h:inputText id="friday1" size="4" value="#{timesheet.friday1}" required="false" /> 
    <!--<input name="friday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> --> 
</td> 
<td id="saturdayInput1"> 
    <h:inputText id="saturday1" size="4" value="#{timesheet.saturday1}" required="false" /> 
    <!--<input name="saturday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> --> 
</td> 
<td id="sundayInput1"> 
    <h:inputText id="sunday1" size="4" value="#{timesheet.sunday1}" required="false" /> 
    <!--<input name="sunday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> --> 
</td> 
<td> 
    <h:inputText id="total1" size="4" value="#{timesheet.total1}" required="false" /> 
    <!-- <input name="total1" id="total1" type="text" size="5" readonly="readonly" /> --> 
</td> 

così ho cercato prima di aggiungere onChange="#{timesheet.setTotal1}" alle voci di giorno ma l'evento onChange chiama il codice JavaScript, non java/JSF. Qualcuno può aiutarti a ottenere il totale da aggiornare?

EDIT: Ho accettato risposta di BalusC ma aveva bisogno di includere anche l'attributo event al tag <f:ajax> per farlo funzionare, in questo modo

<h:inputText id="friday1" size="4" value="#{timesheet.friday1}" maxlength="4" required="false" > 
    <f:ajax event="change" render="total1" listener="#{timesheet.updateTotal1}" /> 
</h:inputText> 

risposta

6

In effetti, l'attributo onchange è solo trattato come una pianura attributo HTML di testo che dovrebbe rappresentare una funzione di gestione JavaScript onchange. Questo non è diverso dal codice HTML "plain vanilla". Qualsiasi espressione EL in quell'attributo verrà considerata come espressione di valore. Vedi anche lo tag documentation.

Sulla base della cronologia delle domande, si sta utilizzando JSF 2.x (si prega di menzionare esplicitamente l'esatta impl/versione di JSF nelle domande future poiché molte cose sono fatte in modo diverso in JSF 2.x rispetto a JSF 1.x) . Quindi, è sufficiente utilizzare il nuovo tag JSF 2.0 <f:ajax>.

<h:inputText ...> 
    <f:ajax listener="#{timesheet.changeTotal1}" render="idOfTotalComponent" /> 
</h:inputText> 

... 

<h:outputText id="idOfTotalComponent" value="#{timesheet.total1}" /> 

con

public void changeTotal1(AjaxBehaviorEvent event) { 
    total1 = ...; 
} 
+0

ho fatto questo, e ora sto ottenendo: "javax.servlet.ServletException: proprietà 'updateTotal1' non trovato sul tipo com.timesheet.Timesheet", come se updateTotal1 è una proprietà piuttosto che un metodo/azione –

+0

In "", vuoi dire? Apparentemente il namespace XML di 'f:' taglib non è stato affatto dichiarato. L'intero tag '' sarebbe quindi trattato come "testo normale" (leggi: qualsiasi EL viene quindi considerata come espressione di valore). – BalusC

+0

Ma è stato dichiarato, non ho appena incluso tutto il codice html –