tags:

views:

48

answers:

3

Good Morning All,

I'm admittedly a DIV guy, but am working on a project that uses nested tables. I have a need to move some fields that already exist, and create new fields, into what I would describe as a grid (think gridview in .NET).

What is the most painless way to add columns to the nested HTML tables? Alternatively, should I investigate the idea of redesigning the whole page with a table-less design except for the fields I need in a gridview?

Gridview is something like this: Columns: Qty, Amt, Notes Rows: Materials, Labor, Miscellaneous

Thanks, Sid

+2  A: 

You would have to add <TD> cells to each row.

Since you said you also need to move things around, you should consider re-writing the output structure to a DIV format or a re-written table structure. I prefer table structures for data grids, personally.

Fosco
@Fosco: Thanks for your suggestion. I tried adding a nested table last night, and I must say that the outcome was very messy. I'll take your approach to rewrite in DIV format using a table only for the actual grid itself. Thanks again for your help and guidance!
SidC
A: 

First off, i don't know what Gridview looks like...but... If i understand the question correctly, you could try adding a <td> with a <table> inside it, then use that table as the column. When you are done with the column add in a </table></td> and things should be the same as before.

this method can get really messy though and im not 100% sure this is what you are trying to achieve.

Josh K
+1  A: 

jQuery has a great selection of DOM traversing functions.

Lets say that for instance, you start with

<html>
<body>
   <table id="aTable"></table>
</body>
</html>

You can then use jQuery to add to the table:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" />
<script type="text/javascript">
   $(document).ready(function(){
      $("#aTable > tbody").append('<tr id="row1"><td>some data</td></tr>');
   });
</script>
</head>
<body>
   <table id="aTable"></table>
</body>
</html>

This would add a single cell to the table. Note the tbody tag, I have found that this is needed as tbody is an implicit tag when rendered by browsers.

Now Let's go backwards. If you start with the cell already in the table, you can remove it very easily with the remove() function:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" />
<script type="text/javascript">
   $(document).ready(function(){
      $("#row1").remove();
   });
</script>
</head>
<body>
   <table id="aTable">
      <tr id="row1">
         <td>some data</td>
      </tr>
   </table>
</body>
</html>

And poof, it's gone. The good part about doing it this way is that you can use event handlers and all sorts of cool things to do all of this in an interactive way. There's no way that I can even scratch the surface of jQuery because I too am fairly new to it, but the docs are pretty good. Check it out

EDIT: this also assumes that you know what you'll be adding. If you need to do it on the fly, a little AJAX will go a long way (jQuery abstracts this to make it nice as well).

Tim
@Tim: Thanks much!! I hadn't considered JQuery as an option for this.
SidC