0

I'm admittedly favour DIV, 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

3
  • Is this something that can be done on the client side or are you planning on only implementing this on the server side? jQuery helps with this sort of thing A LOT on the client side. Commented Jul 12, 2010 at 17:05
  • @Tim, I'm open to whatever is the smoothest solution. What do you suggest in terms of using JQuery? Thanks;) Commented Jul 12, 2010 at 17:49
  • If the data is currently being queried and put into a table on the server side, then I would have to recommend that you keep it that way. Ajax interaction after that point is fair game, but I fail to see any advantage involving jQuery earlier. Commented Jul 13, 2010 at 0:56

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
1

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).

1 Comment

Thanks much!! I hadn't considered JQuery as an option for this.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.