0

Looking for a solution to an old JSF page.

I am trying to use the contains method to render a combobox based on if another column contains the words red box.

If col4 contains the words 'red box' then print 'True', if col4 does not contain the words, then print 'False'.

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets" xmlns="http://www.w3.org/1999/xhtml" xmlns:a4j="http://richfaces.org/a4j" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:c="http://java.sun.com/jstl/core" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:rich="http://richfaces.org/rich">
 <ui:define name="content">
  <rich:panel id="myForm" style="height: 420px;width: 730px;">

    <rich:dataTable id="myTable" value="#{myModel.myTable}" var="table">
    
     <rich:column>
      <f:facet name="header">
       <h:outputText value="Select" />
      </f:facet>
        <c:if test="#{fn:contains(table.col4, 'red box')}">
            <h:outputText value="True" />
        </c:if>
        <c:if test="#{not fn:contains(table.col4, 'red box')}">
            <h:outputText value="False" />
        </c:if>
     </rich:column>

     <rich:column>
      <f:facet name="header">
       <h:outputText value="Comments"/>
      </f:facet>
      <h:inputTextarea id="col4_1" value="#{table.col4}"/>
     </rich:column>

    </rich:dataTable>

  </rich:panel>
 </ui:define>
</ui:composition>
Quentinb
  • 476
  • 1
  • 9
  • 30

2 Answers2

0

I think table.col4.contains('') is Java String's method, not JSF EL so you can't add "!" before expression

This case may be JSTL will help

tablib:

xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
<c:if test="#{not fn:contains(table.col4, 'red box')}">
  <h:selectOneMenu disabled="false" value="#{table.mark}">
    <f:selectItems value="#{comboboxModel.markList}"/>
  </h:selectOneMenu>
</c:if>
Minh
  • 83
  • 5
  • Thanks, but what about the other block of code? I need to show both, one will be enabled and the other will be disabled. When it contains "red box" it must show a disabled combobox. When it does not contain "red box" it must show an enabled combobox. I tried this with 2 c:if statements and the second one using test="#{fn:contains(table.col4, 'red box')}", but that did not work. It always goes into the one with the "not fn". – Quentinb May 09 '22 at 15:53
  • first, if you want to set a control enable/disable, it must always render. I don't understand your UI much, can you share your full XHTML file? I assume you only want to enable/disable when it contains 'red box' or not – Minh May 09 '22 at 16:11
  • Thanks, I have added the complete JSF page now and change the combo boxes to just print true or false. – Quentinb May 09 '22 at 16:23
0

The solution was to do this:

<rich:column>
 <f:facet name="header">
  <h:outputText value="Select" />
 </f:facet>
  <h:outputText rendered="${fn:contains(table.col4, 'red box')}" value="True" />
  <h:outputText rendered="${not fn:contains(table.col4, 'red box')}" value="False" />
</rich:column>
Quentinb
  • 476
  • 1
  • 9
  • 30