5

I have written a code like:

<p:column headerText="Edit" width="40" rendered="#{(leaveDetails.strLeaveStatus == 'Canceled') or (leaveDetails.strLeaveStatus == 'Availed')}">
    <p:commandLink actionListener="#{userLeaveBean.editAppliedLeave}" title="Edit" disabled="true" process="@this" update="leaveDataTable" immediate="false">
        <h:graphicImage url="resources/images/edit.JPG"/>
            <f:attribute name="userId" value="#{employee.name}"/>
            <f:attribute name="editFirstHalf" value="#{leaveDetails.strStartTiming}"/>
            <f:attribute name="editSecondHalf" value="#{leaveDetails.strEndTiming}"/>
            <f:attribute name="editFrom" value="#{leaveDetails.dtLeaveFromDate}"/>
            <f:attribute name="editTo" value="#{leaveDetails.dtLeaveToDate}"/>
            <f:attribute name="leaveId" value="#{leaveDetails.strLeaveId}"/>
    </p:commandLink>
</p:column>

But the rendered attribute is not working for the condition. How can I use the logical operator to make the condition work?Using PrimeFaces 3.4.2

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
NDeveloper
  • 3,115
  • 7
  • 23
  • 34

3 Answers3

13

You can't conditionally render a whole column on a per-row basis. This makes logically no utter sense. You can only conditionally render it on a per-table basis. The <p:column rendered> cannot take a condition based on properties of the iterated row. It can only take a condition based on properties of the parent bean.

If you intend to conditionally hide only the cell of the currently iterated row, then just move the rendered attribute from <p:column> to <p:commandLink> or at least a component which wraps the whole <p:column> content, such as <h:panelGroup>.

Or if you really intend to conditionally hide a whole column, then move the conditions used in rendered attribute of <p:column> to the #{userLeaveBean} parent bean.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I want to do something like this: If the value for `leaveDetails.strLeaveStatus` is 'Canceled' or 'Availed', the `` should be disabled otherwise `` should be enabled.Dont want to hide the column on any condition. – NDeveloper Jun 07 '13 at 11:34
  • 2
    Then just put the condition in the `disabled` attribute of ``? Note that your current code attempt with `` is clearly trying to hide the column on a condition. – BalusC Jun 07 '13 at 11:38
-1

first import

<html xmlns:ui="http://java.sun.com/jsf/facelets">

and add a ui fragment inside the column

<p:column headerText="Edit" width="40">
<ui:fragment rendered="#{(leaveDetails.strLeaveStatus == 'Canceled') or (leaveDetails.strLeaveStatus == 'Availed')}">
    <p:commandLink actionListener="#{userLeaveBean.editAppliedLeave}" title="Edit" disabled="true" process="@this" update="leaveDataTable" immediate="false">
        <h:graphicImage url="resources/images/edit.JPG"/>
            <f:attribute name="userId" value="#{employee.name}"/>
            <f:attribute name="editFirstHalf" value="#{leaveDetails.strStartTiming}"/>
            <f:attribute name="editSecondHalf" value="#{leaveDetails.strEndTiming}"/>
            <f:attribute name="editFrom" value="#{leaveDetails.dtLeaveFromDate}"/>
            <f:attribute name="editTo" value="#{leaveDetails.dtLeaveToDate}"/>
            <f:attribute name="leaveId" value="#{leaveDetails.strLeaveId}"/>
    </p:commandLink>
</ui:fragment> 
</p:column>
  • 1
    How is this an answer to the question? There is no explanation at all. – Kukeltje Nov 19 '18 at 18:44
  • @EduardoSalgado Hi there, welcome to StackOverflow. Could you add a description of the significance of the import statement and ui fragment? That would really help readers understand _why_ this may work. Afterwards, please take your time to familiarise yourself with the site by taking _[the tour](https://stackoverflow.com/tour)_. Hope to see you around :-) – TrebledJ Nov 19 '18 at 18:49
-2

The best way I used to resolve my problem with the help of GOD BalusC is:

<p:column headerText="Edit" width="40">
    <p:commandLink actionListener="#{userLeaveBean.editAppliedLeave}" title="Edit" process="@this" update="leaveDataTable" 
        immediate="false" disabled="#{(leaveDetails.strLeaveStatus == 'Canceled') or (leaveDetails.strLeaveStatus == 'Availed')}">
        <h:graphicImage url="resources/images/edit.JPG"/>
        <f:attribute name="userId" value="#{employee.name}"/>
        <f:attribute name="editFirstHalf" value="#{leaveDetails.strStartTiming}"/>
        <f:attribute name="editSecondHalf" value="#{leaveDetails.strEndTiming}"/>
        <f:attribute name="editFrom" value="#{leaveDetails.dtLeaveFromDate}"/>
        <f:attribute name="editTo" value="#{leaveDetails.dtLeaveToDate}"/>
        <f:attribute name="leaveId" value="#{leaveDetails.strLeaveId}"/>
    </p:commandLink>
</p:column>

and it works as smooth as butter!

NDeveloper
  • 3,115
  • 7
  • 23
  • 34
  • 5
    Note that your initial question isn't formulated as such. You did nowhere state the concrete functional requirement as in *"I need to disable the command link on condition X"*. In the future questions, you'd better do so instead of basically dumping a bunch of lines of wrong code (wrong in such way that the concrete functional requirement isn't immediately obvious from it) and merely saying *"It does not work"*. – BalusC Jun 07 '13 at 12:18