Surya Mandal
Surya Mandal

Reputation: 159

retrive value from collection and show in datatable

I want to show language in view_lang.xhtml using datatable,Below are my classes

CountryBean.java

private ArrayList<Country> existingCountryList;
public ArrayList<Country> getExistingCountryList() {


    System.out.println("CountryBean.getExistingCountryList::Enter");


        existingCountryList = new ArrayList<Country>();
        existingCountryList.addAll(getCountryService().getExistingCountry());
        System.out.println("existingCountryList in countryBean"+existingCountryList);
        System.out.println("CountryBean.getExistingCountryList:::Exit");

return existingCountryList;


}

country.java

private Set<CountryLanguage> countryLanguage = new HashSet<CountryLanguage>(0);

CountryLanguage.java

private CountryLanguageID countryLangPK = new CountryLanguageID();

CountryLanguageID.java

private Country country;
private Language language;

view_lang.xhtml

<h:dataTable id="existingCountry" var="countryLang" value="#{countryBean.existingCountryList}"
        style="width: 100%"  cellpadding="0"  cellspacing="1" border="0" class="role_detail_section" rowClasses="activity_white, activity_blue">

    <h:column>
            <f:facet name="header">
                <h:outputText value="Language(Code)" styleClass="heading_pm_det_white"/>
            </f:facet>

              <h:outputText value="#{countryLang.languageName}(#{countryLang.languageCode})" styleClass="heading_pm_det_white" />
        </h:column>

    </h:dataTable>

I am able to get country object with language but not able to print in datatabel. what will be syntex do I have to use forEach, if yes then how. thnx

Upvotes: 0

Views: 87

Answers (1)

BalusC
BalusC

Reputation: 1108632

You can use <ui:repeat> for this, but this doesn't support Set (because it's not ordered by an index). You need to convert it to a List or an array. If you're using EL 2.2, then you could use the Set#toArray() call directly in EL:

<ui:repeat value="#{countryLang.countryLanguage.toArray()}" var="countryLanguage">
    ...
</ui:repeat>

Update, as per the comments, you'd like ot print it comma separated, here's how you could do it:

<ui:repeat value="#{countryLanguage.language.languageName}" var="languageName" varStatus="loop">
    #{languageName}#{loop.last ? '' : ', '}
</ui:repeat>

Note: if languageName is actually a Set instead of List, obviously use toArray() there.

Upvotes: 1

Related Questions