javascriptjsfselectbooleancheckbox

What is pre-requisite for value attribute of h:selectBooleanCheckbox to be rendered to HTML


I have a managed bean which contains a list of ids. When I iterate through that list using a data table, I use a h:selectBooleanCheckbox and assign the id value to it like this:

<h:selectBooleanCheckbox value = "#{managedBean.objectMap[<objectKey>]}">

However, after the page is rendered, I don't see the value attribute being rendered. So I wanted to know why it is not being rendered?


Solution

  • However, after the page is rendered, I don't see the value attribute being rendered. So I wanted to know why it is not being rendered?

    Because it's not relevant in order to trigger a Boolean. Just the request parameter being null or non-null depending on HTML checked state is sufficient to represent a Boolean. The <h:selectBooleanCheckbox> doesn't support non-Boolean values anyway.

    If you're absolutely positive you need a checkbox value, use either <h:selectManyCheckbox> with only a single item.

    <h:selectManyCheckbox value="#{bean.selectedValues}">
        <f:selectItem itemValue="#{bean.someValue}" itemLabel="#{null}" />
    </h:selectManyCheckbox>
    

    Or if it's actually for JavaScript purposes (I'm just guessing because you confusingly tagged on a JSF question without explaining its relevance anywhere), then you could also use a HTML5 data attribute which you can set as passthrough attribute (requires JSF 2.2).

    <html ... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
    ...
    <h:selectBooleanCheckbox value="#{bean.someBoolean}" a:data-value="#{bean.someValue}" />
    

    It'll be available in JS as below:

    var value = element.dataset.value;
    

    Or jQuery:

    var value = $(element).data("value");
    

    See also: