0

Hello I'm using Primefaces Data table to display some data and I'm following mechanism shown in below code to display Numbers for Row Indexes.(Its working fine too). My question is :

Is there any way, I can display English Alphabets as row indices like below? I don't want to put alphabets in an array at bean and fetch them on every row render.!

<p:dataTable id="resultTable" var="car" value="#{myBean.carList}" rowIndexVar="rowNum">

<p:column headerText="No.">
#{rowNum+1}
</p:column>

<p:column headerText="Name" >
#{car.name}
</p:column>

</p:dataTable>

EDIT Thanks for all of your answers. I found BalusC(unfortunately he removed the answer) and JavaKid's answers easy then putting Javascript. But there were some problems in those answers so i changed and Here is the answer. If you put &# #{65+(rowNum+1)}; in HTML it throws error saying A decimal representation must immediately follow the "&#" So i changed it a Bit and now its working.

ANSWER

<h:outputText value="&amp;&#35;#{64+(rowNum+1)};" escape="false"/>

thanks for every ones answers.

Kishor Prakash
  • 8,011
  • 12
  • 61
  • 92

3 Answers3

2

You can use array in javascript to map index and anphabets:

                <script type="text/javascript">                
                    var alphabets = new Array();
                    alphabets[0] = "A";
                    alphabets[1] = "B";
                    alphabets[2] = "C";
                    // you can declare as: var alphabets =new Array("A","B","C","D");
                    //...
                </script>
               <p:dataTable id="resultTable" var="car" value="#{myBean.carList}" rowIndexVar="rowNum">        
                    <p:column headerText="No.">
                        <script type="text/javascript">
                            document.write(alphabets[#{rowNum}]);
                        </script>
                    </p:column>
                   <p:column headerText="Name" >
                     #{car.name}
                   </p:column>        
               </p:dataTable>
Rong Nguyen
  • 4,143
  • 5
  • 27
  • 53
  • 1
    That's an interesting solution. But still, too much hardcoded and it's kind of clumsy. Still, you've got my vote. I rather proposed a server side solution (one of them). – skuntsel May 02 '13 at 10:49
2

Alternatively, in case your environment supports EL 2.2 you could call method with parameters. As we know that Unicode representation of 'A' is '41' (or 'a' is '61'), we may use the Character.toChars(int codePoint) utility method to perform the transformation:

Character.toChars(rowNumber + 41)[0]; //for capital letters

So, in number column just use

<h:outputText value="#{bean.calculateRowIndexChar(rowNum)}"/>

with bean method

public char calculateRowIndexChar(int rowNumber) {
    return Character.toChars((rowNumber % 26) + 41)[0];
}

In case your environment doesn't support EL 2.2 you could instead create your custom EL function as described by BalusC in How to create a custom EL function?.

Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67
2

You can use Ascii characters. For example A - A and B - B .....
Similarly you can use &# #{64+(rowNum+1)} ;

  • 1
    Thanks JavaKid. BalusC was also suggested similier answer. I tried but there is a problem in your approach. It throws the error saying '' must be followed by a decimal character. so I did some change and finally found the answer, see the edited question for answer. – Kishor Prakash May 09 '13 at 11:59