Menu

Creating display tables from 2D string array

Help
2008-02-20
2012-10-09
  • wecansolveit.org

    I have a list or rather a two dimensional array of strings rather than an object. I want to dynamically create the display tables based on the number of columns I may have for the string array and then display in each display:column each of the columns of the two dimensional array.

    I cannot create an object at all because I do not know how many fields or parameters I might be storing and displaying in the display table.

    Any suggestions or help for this newbie will be really appreciated..

     
    • Ed Webb

      Ed Webb - 2008-02-20

      Have you tried using the 2D array as a datasource for displaytag? You don't need to define any columns inside the <display:table> element if you want all the data displayed. It might be smart enough to handle it.

      Otherwise you'll have to force your array into a more suitable form such as an array of Map objects, each Map representing one row and created from the 2nd dimension of your 2D array.

      Ed!

       
      • wecansolveit.org

        Thanks Ed!

        For your quick follow-up. I am really happy using your Display Tag Libraries with ease and I am already loving it. To quickly solve my problem, I came up with a bad solution. I created an array list of strings and then created a bean with about 20 variables, all strings, and had a generic bean with about 20 string arguments (the maximum number of columns I may have to populate into a bean).

        Once I ran my dynamically created query (as the tables, number of column and their names, conditions were dynamically obtained) and the resultset was passed as string arguments to individual objects of this generic Bean and then a list of these generic Bean objects was created and saved to session.

        I had another array of strings containing the column names which was also used to control how many columns I will be displaying along with their names in the display table. This is a rather awkward solution to my dynamic problem.

        I will give your idea a try and get back to you asap...

         
        • wecansolveit.org

          Interestingly my question was already posted sometime in 2006, and is attached below. And even now, I have the same problem that the Strings in the array are not displayed, rather its .toString() equivalents are displayed() and all I see is garbage instead of strings. Any suggestions to this newbie please :

          Hi
          I am trying to display a 2D String array using the display tag. Here is how I am trying to use it.

          String[][] data = String[5][5];
          for (int i=0; i<5; i++) {
          for (int j=0; j<5; j++) {
          data[i][j] = "element " + i + " " + j;
          }
          }
          request.setAttribute("data", data);
          <display:table name="data"/>

          I was expecting to see a 5 by 5 table with the cells populated with the text in the 2D array. But instead I see a table with 5 rows and each row contains the toString() representation of a String[].

          What is the right way of doing this. Please help.

          Hemant

           
          • wecansolveit.org

            Unable to do it... Please provide some input please

             
            • Ed Webb

              Ed Webb - 2008-02-21

              I think we have decided on the mailing list that you cannot use an array as a row source (or the contents of a Map contained inside a row source object) as DisplayTag will simple call String[].toString().

              I won't repost my suggested work around for the Map problem here but you can read it in the archives:

              <https://sourceforge.net/mailarchive/message.php?msg_id=47BC4554.2040101%40mws-systems.co.uk>

              You may be able to do something similar for your array. If anyone has a better solution please let us know.

              Ed!

               

Log in to post a comment.