0

How can i dynamically change the value of a h:dataTable? I have the following code (just a snippet):

        <h:dataTable value="#{DataProvider.mapValues}" var="o" border="1">
                <h:column>
                    <h:outputText value="#{o.key}"></h:outputText>
                </h:column>
                <h:column>
                    <h:outputText value="#{o.value}"></h:outputText>
                </h:column>
        </h:dataTable>

            <h:selectOneMenu>
                <f:selectItem itemLabel="Choose Language.."></f:selectItem>
                <f:selectItem itemLabel="DE"></f:selectItem>
                <f:selectItem itemLabel="CZ"></f:selectItem>
                <f:selectItem itemLabel="EN"></f:selectItem>
                <f:selectItem itemLabel="FR"></f:selectItem>
                <f:selectItem itemLabel="ES"></f:selectItem>
                <f:selectItem itemLabel="PT"></f:selectItem>
            </h:selectOneMenu>

So, basically, when I choose a language from the SelectOneMenu, the dataTable should use different values (like "#{DataProvider.mapValues(en)"). To be clear: everytime I select a value from the Combobox, the dataTable should reload with an other value.

I guess this would work with JavaScript, but I don't know how exactly it has to be done. Any help would be appreciated. Thanks!

EDIT: I have to use JSF1.2, so I can't use any built-in AJAX features.

hurley
  • 363
  • 1
  • 6
  • 13

1 Answers1

2

Make DataProvider.getMapValues something like this:

private String selectedLanguage; // + getter and setter    

private Map getMapForLanguage(String language)
{ ... }

public Map getMapValues(){
    return getMapForLanguage(selectedLanguage);
}

bind the selectedLanguage to your h:selectOneMenu selected value and do an ajax update of h:dataTable upon selection like this:

<h:dataTable id="myTable" ... >
...
<h:selectOneMenu value="#{DataProvider.selectedLanguage}">
    ...
    <f:ajax render="myTable"/>
</h:selectOneMenu>
dratewka
  • 2,104
  • 14
  • 15
  • 2
    I would not recommend adding any business logic in the getter, refer to [Why JSF calls getters multiple times](http://stackoverflow.com/q/2090033/1065197). Instead, add a `listener` in your `` that will update the values of the map (or the structure OP's using to fill the ``). – Luiggi Mendoza Jul 17 '13 at 08:18
  • @LuiggiMendoza - you're right, if `getMapForLanguage` takes some time to complete I'd also recommend this approach. – dratewka Jul 17 '13 at 08:59
  • Thanks for your approach! Unfortunately I've forgot to mention that I have to use JSF1.2, which has no built-in support for AJAX. Any other idea? – hurley Jul 17 '13 at 09:12
  • @hurley if you're using a third party library like RichFaces there is a `` that behaves similar to ``. Next time add **the exact JSF version** you're working with. – Luiggi Mendoza Jul 17 '13 at 14:38