开发者

How to Add Columns to a Nested HTML Table

开发者 https://www.devze.com 2023-01-07 01:13 出处:网络
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

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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消