ila7goki
ila7goki

Reputation: 11

overriding selectCheckboxMenuRenderer of primefaces

I am trying to add a title attribute to primefaces' selectCheckboxMenu component by extending SelectCheckboxMenuRenderer and overriding the encodeOption method.

But it looks like a different renderer is used. When checking the html source of the page , I can see unordered list items rendered which I don't see anywhere in the SelectCheckboxMenuRenderer class.

I am not sure if I am missing anything.

    public class CustomSelectCheckboxMenuRenderer extends SelectCheckboxMenuRenderer
    {
@Override
protected void encodeOption(FacesContext context, SelectCheckboxMenu menu, Object values, Object submittedValues,
        Converter converter, SelectItem option, int idx) throws IOException
{
    ResponseWriter writer = context.getResponseWriter();
    String itemValueAsString = getOptionAsString(context, menu, converter, option.getValue());
    String name = menu.getClientId(context);
    String id = name + UINamingContainer.getSeparatorChar(context) + idx;
    boolean disabled = option.isDisabled() || menu.isDisabled();

    Object valuesArray;
    Object itemValue;
    if (submittedValues != null)
    {
        valuesArray = submittedValues;
        itemValue = itemValueAsString;
    }
    else
    {
        valuesArray = values;
        itemValue = option.getValue();
    }

    boolean checked = isSelected(context, menu, itemValue, valuesArray, converter);
    if (option.isNoSelectionOption() && values != null && !checked)
    {
        return;
    }

    // input
    writer.startElement("input", null);
    writer.writeAttribute("id", id, null);
    writer.writeAttribute("name", name, null);
    writer.writeAttribute("type", "checkbox", null);
    writer.writeAttribute("value", itemValueAsString, null);

    if (checked)
        writer.writeAttribute("checked", "checked", null);
    if (disabled)
        writer.writeAttribute("disabled", "disabled", null);
    if (menu.getOnchange() != null)
        writer.writeAttribute("onchange", menu.getOnchange(), null);

    writer.endElement("input");

    // label
    writer.startElement("label", null);
    writer.writeAttribute("for", id, null);
    if (disabled)
        writer.writeAttribute("class", "ui-state-disabled", null);

    writer.writeAttribute("title", option.getDescription(), null);

    if (option.isEscape())
        writer.writeText(option.getLabel(), null);
    else
        writer.write(option.getLabel());

    writer.endElement("label");
}

}

This is my customSelectCheckboxMenuRenderer class. I have marked using the line that I added in the code using *. Let me know if I am missing something.

Upvotes: 1

Views: 1214

Answers (1)

BalusC
BalusC

Reputation: 1108732

Works for me. Apparently you forgot to register the renderer in faces-config.xml.

Here's how I did it, with the whole class copypasted unmodified into com.example package:

<render-kit>
    <renderer>
        <component-family>org.primefaces.component</component-family>
        <renderer-type>org.primefaces.component.SelectCheckboxMenuRenderer</renderer-type>
        <renderer-class>com.example.CustomSelectCheckboxMenuRenderer</renderer-class>
    </renderer>
</render-kit>

You need to edit only <renderer-class> to match the FQN of your renderer class.

Upvotes: 1

Related Questions