Datatable with input strings

I have a datatable that generates strings of text inputs and select lists, and when I submit the values, they are zero. I understand this is because jsf generates a separate random ID for each login. I really need the data line by line and then parse what I need. Any suggestions?:

<h:dataTable id="returnableItems" value="#{returnableItemsBean.orderCustomerFamilyItems}" var="item" styleClass="data stripeTable center">
  <h:column>
    <f:facet name="header">Item Number</f:facet>
    <h:outputText id="itemNum" value="#{item.itemNum}"/>
  </h:column>
  <h:column>
    <f:facet name="header">Description</f:facet>
    <h:outputText value="#{item.itemDescription}"/>
  </h:column>
  <h:column>
    <f:facet name="header">Original Quantity</f:facet>
    <h:outputText value="#{item.originalQuantity}"/>
  </h:column>
  <h:column>
    <f:facet name="header">Remaining Quantity</f:facet>
    <h:outputText value="#{item.eligibleQuantity}"/>
  </h:column>
  <h:column>
    <f:facet name="header">Quantity For Return</f:facet>
    <h:panelGrid styleClass="stepper">
      <span class="ns ui-stepper">
        <input id="returnQuantity" type="text" name="returnQuantity" size="2" autocomplete="off" class="ui-stepper-textbox" value="0" />
        <button type="button" name="ns_button_1_0" value="" class="ui-stepper-plus" onclick="setMaxVal(this,#{item.eligibleQuantity});">+</button>
        <button type="button" name="ns_button_2_0" value="" class="ui-stepper-minus">-</button>
      </span>
    </h:panelGrid>
  </h:column>
  <h:column>
    <f:facet name="header">Reason for Return</f:facet>
    <h:selectOneMenu id="returnReason" value="#{returnableItemsBean.orderReason.ordReasonCode}">
      <f:selectItems value="#{returnableItemsBean.orderReasons}"/>
      <f:converter  converterId="orderReasonConverter"/>
    </h:selectOneMenu>
  </h:column>
  <h:column>
    <f:facet name="header">Return/Replacement</f:facet>
    <h:selectOneMenu id="returnOption">
      <f:selectItem itemLabel="Option" itemValue=""/>
      <f:selectItem itemLabel="Return" itemValue="return"/>
      <f:selectItem itemLabel="Replacement" itemValue="replacement"/>
    </h:selectOneMenu>
  </h:column>
</h:dataTable>

      

0


a source to share


2 answers


I don't understand why you are using HTML input and buttons and not JSF in your form. Use <h:inputText>

and instead <h:commandButton>

and bind their values ​​to a property in your backing bean. Of course, the entire table must be inside the tag <h:form> ... </h:form>

.

Also, your input should probably be bound to the "item" variable property, as you would for output. If not, each line will have an input associated with the same property of your backing bean.



Are you using JSP or Facelets?

+2


a source


jsf generates a separate random ID for each login

IDs are not random; h: dataTable is a NamingContainer and manages its clientId children to be unique (required by the HTML spec). JSF: works with component ids (id vs clientId) .

The straight HTML you added will be difficult to integrate with the JSF-view-presenter map. It would be better to stick with JSF control. This code shows how you can manipulate field values ​​on the client side using JavaScript and a custom TLD function :

  <h:dataTable value="#{rowDataBean.data}" var="row">
    <h:column>
      <f:facet name="header">
        <h:outputText value="product" />
      </f:facet>
      <h:outputText value="#{row.name}" />
    </h:column>
    <h:column id="column2">
      <f:facet name="header">
        <h:outputText value="quantity" />
      </f:facet>
      <h:inputText id="quantityCount" value="#{row.quantity}" />
      <h:commandButton value="+"
        onclick="increment('#{id:clientId('quantityCount')}'); return false;" />
    </h:column>
  </h:dataTable>

      



JavaScript is called:

function increment(id) {
  //TODO: error handling
  var textField = document.getElementById(id);
  var value = textField.value;
  value++;
  textField.value = value;
}

      

If you are using Facelets instead of JSP, you will need to define the function in the Facelets tag library .

0


a source







All Articles