2

I am trying to take the below pre-element HTML, and convert it to the below post-element HTML. If using thead and tfoot for the post element doesn't make sense, then tbody would work. I am thinking of starting with $('#myTable') and wrapping it with a div. I am struggling with how to best remove the header and footer, and insert them in their appropriate tables. Thanks for any suggestions.

Pre-element HTML:

<table id="myTable">
    <thead>
        <tr>
            <th>H1</th>
            <th>H2</th>
            <th>H3</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>F1</th>
            <th>F2</th>
            <th>F3</th>
        </tr>
    </tfoot>
    <tbody>
        <tr>
            <th>B1</th>
            <th>B2</th>
            <th>B3</th>
        </tr>
    </tbody>
</table>

Post element:

<div>
    <table class="header">
        <thead>
            <tr>
                <th>H1</th>
                <th>H2</th>
                <th>H3</th>
            </tr>
        </thead>
    </table>
    <div>
        <table id="myTable" class="body">
            <tbody>
                <tr>
                    <th>B1</th>
                    <th>B2</th>
                    <th>B3</th>
                </tr>
            </tbody>
        </table>
    </div>
    <table class="footer">
        <tfoot>
            <tr>
                <th>F1</th>
                <th>F2</th>
                <th>F3</th>
            </tr>
        </tfoot>
    </table>
</div>
5
  • JavaScript should not be used for fixing the markup. Commented Nov 11, 2012 at 23:38
  • Why not? My purpose is to fix the header, and CSS makes it difficult. Commented Nov 11, 2012 at 23:40
  • @undefined ..can you please elaborate ? :) Commented Nov 12, 2012 at 1:08
  • @ubercooluk Does that sound weird? :) I meant we should edit the source/markup instead of using JS, JS should be the final option. Commented Nov 12, 2012 at 1:11
  • @ubercooluk. I wish to keep the header and footer stationary, and scroll the tbody. I've come across only css solutions, but they are complicated and don't work with IE9. There are plugins, but I also want sort functionality, and they don't work with IE7+ Commented Nov 12, 2012 at 1:12

3 Answers 3

1

Here:

var $table = $( '#myTable' ).detach();

$( '<div />' ).append(
    $( '<table class="header" />' ).append( $table.children( 'thead' ) ),
    $( '<div />' ).append( $table.addClass( 'body' ) ),
    $( '<table class="footer" />' ).append( $table.children( 'tfoot' ) )
).appendTo( 'body' );

Live demo: http://jsfiddle.net/MYbc6/4/

I explicitly made sure that the amount of DOM manipulation operations is minimized, i.e. the table is detached from the DOM, then the DIV is constructed, and lastly attached to the DOM.

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

7 Comments

And thank you Sime, Also, didn't know I got a response. Let me look at it. Thanks!
I've never used detach() before. Looks appropriate, however, it also looks like it might not go in the same location as the original #myTable.
@user1032531 Well, I've put the DIV in <body>. You have to make sure that it goes at the same location as the original table. That's just the downside of detaching.
Thanks. Trying but not successful figuring out how to put it back in the same location. Can you help>
@user1032531 Does the table in its original location have siblings? Or is it the only child?
|
1

You should selects thead, tbody and tfoot and wrap them with div or tables. And then move tfoot after tbody.

$table = $('#myTable');
$table.find('thead').wrap('<table class="header"></div>');
$table.find('tfoot').insertAfter($table.find('tbody'));
$table.find('tfoot').wrap('<table class="footer"></div>');
$table.find('tbody').wrap('<div><table id="myTable" class="body"></table></div>');
$table.wrap('<div></div>').replaceWith($table.html());

Demo: http://jsfiddle.net/F3MCv/2/

4 Comments

You didn't achieve the desired structure - jsfiddle.net/F3MCv/1 (only the "header" table is in the DIV, and the "body" table is not wrapped in a second DIV)
thanks for the notice. i edited it on here and jsfiddle, the output is as desired now.
Thanks Halil, Didn't know I got a response. Let me look at it. Thanks!
@HalilBilir You still need to wrap the entire structure in an additional DIV.
0

I still don't know whether this is any better than the above two great solutions, but it is currently what I am doing. Šime Vidas's use of detach() is probably better, but I do not know how to re-insert the modified HTML at the new location.

var $table = $('#myTable').addClass('body').wrap('<div />').wrap('<div />');
$table.parent()
    .before($('<table class="header" />').append($table.children('thead')))
    .after($('<table class="footer" />').append($table.children('tfoot')));

Demo: http://jsfiddle.net/zuHLX/

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.