4 Replies Latest reply on Oct 5, 2015 4:26 PM by Wedla Melo

    how to handle dynamic id on rich:column to use it for sort items

    Wedla Melo Newbie

      hello!

      this is my first post and i want to say that english is not my native language, so i apologize since now if i commit any grammar or orthographic mistakes.

      i'm also a newbie using jsf and its components, but now i studying and fouding some problems on my system that i might resolve.

      my job is automatize tests for the system, because of this i need to fix id in columns and other components that will turn into html elements. the problem that i'm facing right now is the sort of rich columns.

      our system is big enough to include fixed ids in all datatables. so, because of this, we develope a class that generates ids to rich columns when they are not specified in first place.

      our xhtml code it follows:

       

      <rich:column id="#{empty idColumn ? idGenerator.id : idColumn}" ... (there more code after that) />

       

      this idGenerator is a component CDI class, which follows here too:

       

      @Named

      public class IdGenerator {

          final static String letras = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";

          final static int size = letras.length();

       

          public String getId() {

              return letras.charAt(new Random().nextInt(size)) + String.valueOf(UUID.randomUUID()).replace("-", "");

          }

      }

       

      by using this method getId(), it generates ramdom ids to the columns, but the sort order does not work when we click to do that. to make sure my doubt will not cause any more doubts, it's not a problem with jsp-hibernate, because we pass a parameter columnId which it's the name of the column in the table in our database. the idColumn is simple an idea to use id in these columns to turn possible our automatize tests.

       

      i aprreciatte since now all the attention and hope that you could help me to fix that.

        • 1. Re: how to handle dynamic id on rich:column to use it for sort items
          Michal Petrov Master

          If you debug your application you'll see the getId() method is called after every request. JSF is recreating the component with each request so the columns get assigned a new id every time. This would cause even more problems if it wasn't an id, a getter method simply needs to return a value and not perform any other actions.

           

          Instead of trying to fix a problem in your solution explain the original problem. Why don't you update your data so that every column has an id? DataTables can be made sortable, why do you require the columns to have an id? They're getting a random id assigned anyway.

          • 2. Re: how to handle dynamic id on rich:column to use it for sort items
            Wedla Melo Newbie

            thank you very much for all the attention that you gave to my question.

             

             

            actually, we are using our own component which is based in richfaces components, which means that we are using a component that contains others, that was developed by the software factory that we are working for.

            this component when is displayed in html page hasn't an id, do you get it? when we analyzed the code we didn't find the id for the element, so we created this property (id) as you saw in the code I did put in the question.

             

            i need to have html ids. i don't need to have id for columns in the table from the database, because we already specified these ones, and we use them to make possible to sort the elements from the column. this property is called columnId, that has the same value as we find in the id columns from the tables.

             

            but i need to automatize the tests in the system, so, for the order of priority, we must have ids to the rich columns, which is concatenated with a constant value and has always two values which are asc or desc from ascending and descending sorting.

             

            so, in our java code, we get part of an id from the field, not its complete, by using xpath. but we cannot just put a fixed id like asc or desc, because the columns will be repetead and this breaks the system, 'cause repetead ids are not allowed.

             

            i hope that i clarified to you our problem. and i really understood the problem by using the function getId() as explained in your answer. a friend of mine, who is in developer team, told me about this, that jsf would probably generating different ids, but he was not able to tell me why, but you did.

             

             

            so, again i thank you for the answer and the attention.

            • 3. Re: how to handle dynamic id on rich:column to use it for sort items
              Michal Petrov Master

              The generated HTML for rich:dataTable looks like this:

              <table>
                <thead>
                 <tr>
                   <th>
                   …
                <tbody>
                  <tr>
                    <td>
                    …
              

              Each one of those elements will have an id even if you don't set one, is that not the case?

              You also said that some columns have ids so I'm assuming they are referencing some property in the datamodel that is missing for the other columns.

               

              I don't understand how you're sorting the table, rich:dataTable comes with built-in sorting but even if you use a custom one you do not need an id for that.

              • 4. Re: how to handle dynamic id on rich:column to use it for sort items
                Wedla Melo Newbie

                yes, all html elements have an id by specifying it or not. i don't know why i said otherwise, maybe to get a quick answer to your answer.

                datasort.png

                but we have in our datatables html elements to sort elements in forms ascendent or descendant, as shown in the image above.

                 

                our problem is that we have multiples tables, not only one like this in the entire system. so, we thought of generating ids with 'asc' and 'desc' in its ends would resolve our problem, when we are not automating the page, when we are doing it, we set ids to them, do you get it? because of this, we generated random ids for all columns that we didn't set previously.

                 

                i appreciate your attention but we resolved this issue by using the property html styleclass, because it does not interfere in the rich component behaviour, so does not have any impact in the system.  it's a palliative way to get the columns names and make possible our tests.