1

I'm trying to render a readonly checkbox with a negated boolean value from my model.

Salesforce won't let you do this:

<apex:inputCheckbox disabled='true' value='{! !model.myBoolean}' />

i guess this is just meant for model binding

Or this:

<apex:inputCheckbox disabled="true" selected="{! !model.myBoolean}" />

So my next thought was fine, I'll just use a standard HTML input:

<input type="checkbox" disabled="true" {!if(model.myBoolean,'','checked')}" />

However, this apparently doesn't compile.

So short of dirtying up my model by creating a new getter to just negate this value (the view should NOT dictate whats in you're model!!!), is there anything else I can do?

2
  • So if model.myBoolean is false you want to display checkbox as true or vice versa? Commented Dec 23, 2016 at 3:07
  • @TusharSharma yea
    – NSjonas
    Commented Dec 23, 2016 at 3:53

1 Answer 1

1

Save yourself some trouble and enlist the help of jQuery:

<apex:page>
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"/>
    <input type="checkbox" disabled="true" />
    <input type="checkbox" disabled="true" class="{!if(model.myBoolean,'','chk')}"/>

    <script>
        $('.chk').attr('checked','');
    </script>
</apex:page>

The problem you are having is that HTML does not care what the value of checked is, as long as it is present on the element it will be displayed as checked.

Visualforce will not allow you to compile unless you have values associated with each attribute, this is why your IF merge would not compile

Also, the selected attribute of the inputCheckbox does not allow you to use dynamic value. Another you thought it was going to be easy but SF quirk got in the way

Error: Unknown property 'if(true,'true','false')'

So you are in a catch 22. jQuery/JS is easiest or render based on value using panels:

<apex:outputPanel rendered="{!model.myBoolean}">
   <input type="checkbox" disabled="true" />
</apex:outputPanel>
<apex:outputPanel rendered="{! !model.myBoolean}">
  <input type="checkbox" disabled="true" checked='true' />
</apex:outputPanel>

Simplified version

this is implementation simplified with add ONE line

<apex:page>

    <input type="checkbox" disabled="true" class="{!if(model.myBoolean,'','chk')}"/>

    <script>
        document.getElementsByClassName("chk")[0].setAttribute('checked','');
    </script>

</apex:page>
6
  • seems a little complicated... I try to avoid using javascript for one off things like this because it makes it harder for future maintainers (this is a massive page with already too much going on). Ended up just using 2 output panels. Thanks for the solution though
    – NSjonas
    Commented Dec 23, 2016 at 4:10
  • To each their own. The panels part was the last 6 words of my post. Some do not like extra elements some do not like extra JS. The JS approach is simply an include and One line of JS. Purce JS would be One line without the include. So it is less complicated really in the final solution....
    – Eric
    Commented Dec 23, 2016 at 4:12
  • oh, sorry i missed that. I'm going to edit your solution to add the panels method into it. I guess as long as you keep the script inline, right next to the box then its not too hard to find. I typically try to keep all my javascript in static resources though. Also, it seems as though you can use dynamically values with the selected attr, as long as its the unmodified field binding: selected="{! model.myBoolean}" seems to work fine...
    – NSjonas
    Commented Dec 23, 2016 at 4:15
  • 1
    @NSjonas - I added the simplified version which is ONE line added to the page instead of the panels. Depending on your needs you can use IDs, classes, for if multiple elements do a for each. it really comes down to preference though - and yea, I was editing the answer as I went.....
    – Eric
    Commented Dec 23, 2016 at 4:17
  • 1
    it won't let you negate. I tried: selected="{! !model.myBoolean}", selected="{! Not(model.myBoolean)}" & {! model.myBoolean == false}"... All of these threw compilation errors. Really seems like it should work though...
    – NSjonas
    Commented Dec 23, 2016 at 4:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .