views:

4356

answers:

3

What is the best plain javascript way of inserting X rows into a table in IE.

The table html looks like this:

<table><tbody id='tb'><tr><td>1</td><td>2</td></tr></tbody></table>

what i need to do, is drop the old body, and insert a new one with 1000 rows. i have my 1000 rows as a javascript string variable.

the problem is that table in IE has no innerHTML function. ive seen lots of hacks to do it, but i want to see your best one.

note: using jquery or any other framework does not count.

Thanks

A: 

Do as jQuery does it, eg. add <table> and </table> around it, insert into document and move the nodes you want to where you want them and ditch the temp-element.

svinto
+5  A: 

Here's a great article by the guy who implemented IE's innerHTML= on how he got IE to do tbody.innerHTML="<tr>...":

At first, I thought that IE was not capable of performing the redraw for modified tables with innerHTML, but then I remembered that I was responsible for this limitation!

Incidentally the trick he uses is basically how all the frameworks do it for table/tbody elements.

Edit: @mkoryak, your comment tells me you have zero imagination and don't deserve an answer. But I'll humor you anyway. Your points:

> he is not inserting what i need

Wha? He is inserting rows (that he has as an html string) into a table element.

> he also uses an extra hidden element

The point of that element was to illustrate that all IE needs is a "context". You could use an element created on the fly instead (document.createElement('div')).

> and also the article is old

I'm never helping you again ;)

But seriously, if you want to see how others have implemented it, take a look at the jQuery source for jQuery.clean(), or Prototype's Element._insertionTranslations.

Crescent Fresh
Hahaha, I just read that today! It's a good read. Hooray for reddit. :D
Daniel Lew
yeah i read that today too actually. he is not inserting what i need, he also uses an extra hidden element, and also the article is old
mkoryak
@crescentfresh yes: ive seen many versions of the code that does what he is talking about in my travels. i was hoping that someone would write some code, and then i could look at it and compare it to other code to see which is better. asking people to code always gets me in trouble :P
mkoryak
yikes man. im sorry that i have offended you with my asking. yes i can write this myself. i want to see how other people would do it, because i know that someone can do it better then me. please help me again, or ill cry.
mkoryak
@mkoryak: that last line was me being humorous ;) Read it with a pause, then a quick "I'm-never-helping-you-again". Actually re-reading it I realize I forgot the smiley ;)
Crescent Fresh
A: 

the code ended up being this:

    if($.support.scriptEval){
    //browser needs to support evaluating scripts as they are inserted into document
        var temp  = document.createElement('div');
 temp.innerHTML = "<table><tbody id='"+bodyId +"'>"+html;
 var tb = $body[0];
 tb.parentNode.replaceChild(temp.firstChild.firstChild, tb);
 temp = null;
 $body= $("#" + bodyId);
    } else {
    //this way manually evaluates each inserted script
        $body.html(html);
    }

Things that beed to exist beforehand: a table that has a body with id of 'bodyId'. $body is a global variable (or the function has a closure on it), and there is a bit of jquery in there too, because IE does not evalute scripts that are inserted into the html on the fly.

mkoryak