views:

65

answers:

3

I've to create a dynamic html table using the values configured in a table. I'm using JSP with MVC architecture.

I've a row number, column number and value fields in table. If the values are 1,5 and HELLO correspondingly, then I've to display this HELLO in row 1 and column 5.

The table structure is like the following.

row column   value
 1     5     value1
 2     8     value2  

Any ideas?

+1  A: 

Fetch the maximum column number:

SELECT MAX(column) AS max_column
  FROM xxx

Then fetch the data in row/column order:

SELECT *
  FROM xxx
 ORDER BY row, column

Then write an outer loop that iterates indefinitely, generating rows, and an inner loop that iterates over column numbers. For each cell, check whether the current result record matches the coordinates. If it does, output it and fetch the next record.

Here's some pseudo-code:

max_column = SELECT MAX(column) ...
data = SELECT * ...
if data.eof(): exit
output "<table>"
for row = 1..∞:
    output "<tr>"
    for col = 1..max_column:
        output "<td>"
        if data["row"] = row and data["column"] = col:
            output data["value"]
            data.next()
            if data.eof(): exit
        output "</td>"
    output "</tr>"
output "</table>"
Marcelo Cantos
+1  A: 

Going on your limited information (i'll edit this once you give more details):

use place holders:

for each cell insert '{rol,col}'

Use sql to return :

 id = {rolw,col}
 value = value

pseudo sql:

select  '{'+str(row)+','+str(col)+'}' as [id]
       ,value
from    table

then simply loop through each record, and replace the placeholders.

EG

Darknight
+1  A: 

You need to collect the data in a String[][] first:

int rows = getMaxRowNumberFromDB();
int cols = getMaxColNumberFromDB();
String[][] values = new String[rows][cols];

// ...

while (resultSet.next()) {
    int row = resultSet.getInt("row");
    int col = resultSet.getInt("col");
    String value = resultSet.getString("value");
    values[row][col] = value;
}

(note that array indexes are zero based, you may want to substract 1 from row and col first)

Then display it using JSTL c:forEach (or any data iterating tag your "MVC architecture" thing is using) in JSP:

<table>
    <c:forEach items="${values}" var="row">
        <tr>
            <c:forEach items="${row}" var="value">
                <td>${value}</td>
            </c:forEach>
        </tr>
    </c:forEach>
</table>
BalusC
that was easy.. thanks :)
coder247