Solved

<table>

Posted on 2006-11-29
24
4,320 Views
Last Modified: 2008-02-01
hi experts

I want to have two or more tables generated dynamically. My table structure is like this

<table>
<tr>
HEADING
</tr>
<tr>
<td>
my fields  in table
</td>
</tr>
</table>
 
If the number of rows in above table reach 10, i want to dynamically create a new table with same structure.

I was trying to play around with JSTL to get this done.

I have set a counter at the top of the page like this and  incrementing the counter after each row

 
<table>
<c:set var="counter" scope="session" value="1"/>

<tr>
HEADING 1
</tr>
<tr>
<td>
my fields  in table .

<c:if test="condition">
<c:set var="counter" value="${counter+1}" scope="session"/>
</c:if>

</td>
</tr>
</table>
 <c:out value='${counter}'/>


The value of <c:out value='${counter}'/>  represents the number of rows which is 15 right now.
Is it possible using <c:forEach> loop that I check a condition that if “counter” reaches 10, create a new table and put the additional 5 rows in that table. Something like

<table>
<c:set var="counter" scope="session" value="1"/>
<c:forEach …>
<c:if test=”{counter == ‘10’}”>
Break off and create another table and put the 5 rows in that table
<tr>
HEADING 1
</tr>
<tr>
<td>
<c:if test="condition">
<c:set var="counter" value="${counter+1}" scope="session"/>
</c:if>
</td>
</tr>
</table>
</c:if>
</c:forEach>

 
any ideas will be greatly appreciated.
thanks,
J
0
Comment
Question by:jaggernat
  • 11
  • 7
  • 6
24 Comments
 
LVL 5

Accepted Solution

by:
dasmaer earned 325 total points
ID: 18041618
Hi J

You're on the right lines.  Where you have written "Break off and create another table and put the 5 rows in that table" you need to put the html to close off the current table and start a new one.

<table>
<c:set var="counter" scope="session" value="1"/>
<c:forEach …>
<c:if test=”{counter == ‘10’}”>
<!-- the counter is now at 10 so close off the current table -->
</table><table>
</c:if>
<tr>
<td>HEADING 1</td>
</tr>
<tr>
<td>
<c:if test="condition">
<c:set var="counter" value="${counter+1}" scope="session"/>
</td>
</tr>
</table>
</c:if>
</c:forEach>

Or something like that... is this what you;re thinking or am I heading off in the wrong direction?
Regards dasmaer
0
 
LVL 5

Expert Comment

by:dasmaer
ID: 18041626
Check if opening and closing of the if's as I'm not sure if I got it right for what you're trying to achieve.
0
 
LVL 10

Author Comment

by:jaggernat
ID: 18041661
all my rows are generated dynamically (if condition is true). can i just reuse the same table over and over without adding another table? any idea

0
 
LVL 5

Expert Comment

by:dasmaer
ID: 18041773
I'm not sure what you mean.  I thought you meant that what you wanted was for a new table to be created every 10 rows, is that not the case?

D
0
 
LVL 5

Expert Comment

by:dasmaer
ID: 18041821
A refinement of my last would be:

<table>
<c:set var="counter" scope="session" value="1"/>
<c:forEach …>
<c:if test=”{counter == ‘10’}”>
<!-- the counter is now at 10 so close off the current table -->
</table><table>
<tr>
<td>HEADING 1</td>
</tr>
</c:if>
<tr>
<td>
<c:if test="condition">
<c:set var="counter" value="${counter+1}" scope="session"/>
</td>
</tr>
</table>
</c:if>
</c:forEach>

As this would keep the structure in the new table.

D
0
 
LVL 10

Author Comment

by:jaggernat
ID: 18042366
>>>I thought you meant that what you wanted was for a new table to be created every 10 rows

you r right. what i was thinking was, can i reuse the same table code to replicate itself  instead of creating a new table.

the second table will have exactly the same structure(10 rows) and the same code for generating the values in the rows as the first table.

may be we can do a <c:forEach to LOOP through the counter value and generate a table for every 10 counter values.

thats just a abstract idea in my mind.

thanks for the input.
J

0
 
LVL 5

Expert Comment

by:dasmaer
ID: 18042438
I guess you could do that... expect it would only work if you want almost the exact same table to be reproduced. Providing that is the case then using a JSTL for loop would be an acceptable way to reproduce the same table X amount of times.

D
0
 
LVL 10

Author Comment

by:jaggernat
ID: 18042478
ok,,thanks, but how should the code for the "for loop"  be ?
0
 
LVL 5

Expert Comment

by:dasmaer
ID: 18042530
Just had a think about how it should be done and started to get a headache... it's well tricky.

If you just want 3 tables (for example) then there would be no reason to do a counter to start a new one every 10 rows.  If you want it to swan a new table every 10 rows then you would need to end the current table and start a new one like I mentioned before.

What I'm sensing you want to do is have a for loop iterate X amount of times, and then for a table to be printed out every time you get ten rows made.  This would be better implemented through inline java, as you'd be able to have a method that starts a table with a heading, closes a table and puts rows in a table.  In JSTL the only way I can think of achieving this output is to have the following:

<c:set var="counter" scope="session" value="1"/>
<table> <!-- Start first table outside of loop -->
<tr>
      <td>HEADING 1</td>
</tr>
<c:forEach …>
<c:if test=”{counter == ‘9’}”><!-- if counter is 9 then spawn new table (start counting at 0?) -->
<c:set var="counter" value="0" scope="session"/>
</table>
<table>
      <tr>
            <td>HEADING 1</td>
      </tr>
</c:if>
<c:if test=”{counter != ‘9’}”><!-- if counter is not 9 then insert new row -->
<c:set var="counter" value="${counter+1}" scope="session"/>
      <tr>
            <td>
                  <c:if test="condition">
                        <!-- some cell content -->
                  </c:if>
            </td>
      </tr>      
</c:if>
</c:forEach>
<c:if test=”{counter != ‘9’}”>
      </table> <!-- close off table if not done by 9th row -->
      
</c:if>

So you would need it to create tables as its going, as oppose to calling a method or something - as this is something I don't think you can do in JSTL.

D
0
 
LVL 5

Expert Comment

by:dasmaer
ID: 18042539
where the forEach part reads:

forEach begin="0" step="1" end="END_VALUE"

(sorry, copied and pasted and forgot that bit)

D
0
 
LVL 10

Author Comment

by:jaggernat
ID: 18042728
>>should be done and started to get a headache

yea, i agree :) i had a headache too thats why i posted it here :) :)

thanks for all the code dasmaer, i'll try that tomorrw.

J

0
 
LVL 16

Assisted Solution

by:suprapto45
suprapto45 earned 175 total points
ID: 18042758
Just got a 5-mins break of my busy job ;).


<c:set var="counter" value="${0}" />
<c:forEach>
<c:set var="counter" value="${counter + 1}" />

<c:if test="${counter % 10 == 1}">
    <table>
    <tr>
    HEADING 1
    </tr>
</c:if>

<tr>
<td><c:out value="${whatever here}" /></td>
</tr>

<c:if test="${counter / 10 == 1}">
    </table>
    <br>
</c:if>
</c:forEach>


Expl: if counter % 10 == 1 then create a new table. It means that 1, 11, 21 are qualified.
      if counter / 10 == 1 then end the table. It means that 10, 20, 30 are qualified.
      in the middle of that, just print out the values. It will divide your tables into 10 each.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 10

Author Comment

by:jaggernat
ID: 18042800
another thing i was worried about is i want the tables to be placed side by side, table1 adjacent to table2 adjacent to table3 and so on....

0
 
LVL 16

Expert Comment

by:suprapto45
ID: 18042824
Then you have to play around with the <table> tag.

I assume that it will run (all my codes are not tested so please test it again later on).

<c:set var="counter" value="${0}" />

<table>
<tr>
<c:forEach>
<c:set var="counter" value="${counter + 1}" />

<c:if test="${counter % 10 == 1}">
    <td>
        <table>
        <tr>
        HEADING 1
        </tr>
</c:if>

<tr>
<td><c:out value="${whatever here}" /></td>
</tr>

<c:if test="${counter / 10 == 1}">
        </table>
    </td>
</c:if>
</c:forEach>

</tr>
</table>
0
 
LVL 10

Author Comment

by:jaggernat
ID: 18050000
ok, decided not to reuse the same table. its really confusing that way with all the jstl tags, more over its a problem when i try to put one table adjacent to another table.

so i have planned to do it the normal way by copy-pasting the table 2 times so that i get 2 tables. The two tables in my below code are
<!— Table1-->
<!—Table2 -->
And I am trying to do
Main table
________________________________________
|<tr>                                                             |
|<td><Table1></td> <td> <Table2></td>      |
|__</tr>________________________________|

Code


<c:set var="counter" value="0"/>  
<!—Main table which will contain Table1 -->
<table border="0">
<tr>
<td>
<!-- Table1 --  > <!-- Inside Table1 increment counter <c:set var="counter" value="${counter+1}"/>  -->

</td>
</tr>
</table>
<!—Main table ends-->

<c:if test="${counter == '10'}">   //checking if counter==10;break from the previous table and create a new table
<!—Main table which will contain Table2 --> //i thnk i should not have Main table here ..not sure!!
<table border="0">
<tr>
<td>
<!—Table2 --><!-- Inside Table2 increment counter <c:set var="counter" value="${counter+1}"/>  -->
</td>
</tr>
</table>
</c:if> //condition closed


Status: not working, just shows me Table1 with all the 15 rows in it.
So only when counter = 10 (number of rows reaches 10 in Table1) , create the new table other wise don’t. I am really not sure if that’s the right logic.

Any sugessions greatly appreciated.
Thanks
J

0
 
LVL 16

Expert Comment

by:suprapto45
ID: 18050527
Hi J,

I think that it should be
<c:if test="${counter >= 10}"> or <c:if test="${counter gt 10}">
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 18050610
However,

If you decided to go with that approach, if you have 35 rows of data, you will end up with 1st table - 10 rows and 2nd table - 25 rows.

David
0
 
LVL 10

Author Comment

by:jaggernat
ID: 18050629
yes, in that case i have to manually create Table3 and table4.
doesnt sound like a very decent approach .does it ?


0
 
LVL 16

Expert Comment

by:suprapto45
ID: 18050708
It does not really.

Why don't you use pagination? If pagination is not feasible then you have to go back to the JSTL approach (complex but it is dynamic).
0
 
LVL 10

Author Comment

by:jaggernat
ID: 18050723
yea, thats true. its not really pagination concept , its creating multiple tables side by side.

lets see if any other experts can input any idea...

thanks
0
 
LVL 10

Author Comment

by:jaggernat
ID: 18050743
if i do it using jstl (i.e  reusing the single table code multiple times), i dont think the tables will be aligned/placed side by side.
I am not even sure if that approach will work. i tried the above code today and i was getting some errors.

0
 
LVL 16

Expert Comment

by:suprapto45
ID: 18051042
I thought that it should be okay. Actually, what kind of errors are you getting?
0
 
LVL 10

Author Comment

by:jaggernat
ID: 18055862
thats okay david . after banging my head for couple of hours , i fixed it ;)

used the counter like we discussed above and used " begin="1" end="10" "  in the forLoop like  this.

<c:forEach items="${businessAreas}" var="businessArea" varStatus="iStatus" begin="1" end="25" >

and finally it worked.

thanks you both,
J
0
 
LVL 10

Author Comment

by:jaggernat
ID: 18058177
infact another kool thing i found out is that  in
<c:forEach items="${businessAreas}" var="businessArea" varStatus="iStatus" begin="1" end="25" >


varStatus variable is the counter which iterates through the loop.
so iStatus values are 1,2,3...so on

so instead of defining my own "counter", i am using iStatus and it works great



0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
library class in java 1 89
AlarmClock Challenge 35 145
Best practices to work on a spring MVC project 1 112
instanceof  operator in java 26 73
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now