Barisdag
Barisdag

Reputation: 21

Get HashMap value by dynamic key in EL

I have a data table which contains "Package" objects and 2 columns (packageBarcode and address). I need to add a column (total number of letters in a package), but this value is not in the object. That's why, I used HashMap. I mapped the packageId and totalNumberOfLetters in this HashMap. I want to display this values in dataTable. How can I do this?

<p:column headerText="package" 
          sortBy="#{package.barcode}" 
          filterBy="#{package.barcode}"  
          > 
     <h:outputText value="#{package.barcode}" /> 

</p:column> 

Upvotes: 2

Views: 3062

Answers (1)

BalusC
BalusC

Reputation: 1108732

Provided a

private List<Package> packages;
private Map<Long, Integer> totalNumberOfLettersByPackageId;

you can access it as below

<h:dataTable value="#{bean.packages}" var="_package">
    <h:column>#{bean.totalNumberOfLettersByPackageId[_package.id]}</h:column>
</h:dataTable>

Do note that I prefixed package with _, because package is a Java literal and a sane EL implementation would throw a runtime exception on that.

Upvotes: 4

Related Questions