views:

607

answers:

3

I have the following h:selectOneRadio:

<h:selectOneRadio id="#{prefix}_rating" value="#{examinationPanel.tempResult}" >
<f:selectItems value="#{examinationPanel.options}"></f:selectItems>
</h:selectOneRadio>

And then the backing bean is loading the options based on some userpreferences:

public List<SelectItem> loadOptions (Set<ResultEnum> possibleResults){
    List<SelectItem> options = new ArrayList<SelectItem>();
    for(ResultEnum result:possibleResults){
        SelectItem option = new SelectItem(result,messages.getString(result.name()));
        options.add(option);
    }
    return options;
}

How can I define shortcuts for the options in the radio group? por example typing "1" selects the first option, typing "2" the second, etc...

Thanks in advance! ps. I'm using Jsf 1.2, Richfaces, Primefaces and Facelets.

+2  A: 

That's not by default builtin. Your best resort is using JavaScript. Attach a function to the keypress event of the <body>, check the keyCode of the key pressed and change the selected item of the dropdown accordingly. This is doable in only a few lines using jQuery. Here's an SSCCE, just copy'n'paste'n'run it.

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2461588</title>
        <script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
        <script>
            $(document).ready(function() {
                $('body').keypress(function(e) {
                    var index = parseInt(String.fromCharCode(e.keyCode));
                    if (0 < index && index < 10) {
                        $('#dropdown option').eq(index - 1).attr('selected', true);
                    }
                });
            });
        </script>
    </head>
    <body>
        <select id="dropdown">
            <option>option1</option>
            <option>option2</option>
            <option>option3</option>
            <option>option4</option>
            <option>option5</option>
            <option>option6</option>
            <option>option7</option>
            <option>option8</option>
            <option>option9</option>
        </select>
    </body>
</html>

If you want to support 10 or more items, you'll need to maintain a stack of pressed keys, apply it immediately and introduce a timeout to reset the stack (1 second?).

BalusC
I thought so...I was hoping that somebody knew a weird function in a not common component that could do that for me :D. Thanks for the answer anyway.
pakore
Remember that you can exclude the generated id from JSF in your form objects. This way it is much easier to use jQuery selectors in JSF. You do that like this <h:form prependId="false">If you want to escape the id's properly in JSF with jQuery, you can use this function:function escapeIdForJQuery(id) { return id.replace(/:/g,"\\:").replace(/\./g,"\\.");}
Shervin
@Shervin: just escape the colon by backslash in selector. E.g. `$('#formId\:menuId')`.
BalusC
I think you need double backslash
Shervin
@Shervin: when you want to represent a backslash itself, yes. But that is here not the case.
BalusC
+2  A: 

You can investigate whether <rich:hotKey> would help. In short, it lets you define keyboard shortcuts, but it may require some jQuery knowledge.

Bozho
Interesting component.
BalusC
If I could you vote as valid answer as well I would do it!
pakore
+1  A: 

PrimeFaces hotKey can help as well.

Cagatay Civici