I have the following problem. I had some code that I wrote before, it got XML data from XMLHttPrequest and populated the table with rows. It worked like it should.
However, browser was lagging when the table was populated with large quantity of rows so i decided to rewrite it using document fragment.
I understand the function is not ideal, in fact xml is not passed as a parameter, and the items are fetched from it without any problems, I just rearranged it a bit to avoid the code that is not relevant to the problem at hand.
I also understand that I omit var in many places hwere I shouldn't, didnt get around to fixing this yet.
Problem is... the rows are filled. Images show up, input values, button text, everything is ok (the app is IE only).
However
1) I cannot set style attribute for tags. I can set any other attribute, but when i set style and alert tbl.innerHTML, it's not there.
2) class attribute doesn't work. titlecell class has some styling, but it doesn't show up.
3) javascript handlers don't work. As you see I add onclick attribute to button, so, when I click button, nothing happens even though when i alert tbl.innrHTML it's in place and everything looks good.
To get around this i tried extrancting innerHTML to a string and reassigning it it innerHTML - I get "Unknown error" popping up.
Here's the code
However, browser was lagging when the table was populated with large quantity of rows so i decided to rewrite it using document fragment.
I understand the function is not ideal, in fact xml is not passed as a parameter, and the items are fetched from it without any problems, I just rearranged it a bit to avoid the code that is not relevant to the problem at hand.
I also understand that I omit var in many places hwere I shouldn't, didnt get around to fixing this yet.
Problem is... the rows are filled. Images show up, input values, button text, everything is ok (the app is IE only).
However
1) I cannot set style attribute for tags. I can set any other attribute, but when i set style and alert tbl.innerHTML, it's not there.
2) class attribute doesn't work. titlecell class has some styling, but it doesn't show up.
3) javascript handlers don't work. As you see I add onclick attribute to button, so, when I click button, nothing happens even though when i alert tbl.innrHTML it's in place and everything looks good.
To get around this i tried extrancting innerHTML to a string and reassigning it it innerHTML - I get "Unknown error" popping up.
Here's the code
Code:
function buildResourceList(myxml)
{
var tbl = document.getElementById('newTable');
while (tbl.rows.length)
{
tbl.deleteRow(tbl.rows.length - 1);
}
var docFragment = document.createDocumentFragment();
var items = myxml.getElementsByTagName("ResResource");
for (var i = 0; i < items.length; i++)
{
attrTimed = items[i].getAttribute("Timed").toString();
id = items[i].getAttribute("resourceID").toString();
vname = items[i].getAttribute("Name").toString();
vname = vname.replace(/[']/g,'"').replace(/(\S[\/-])(\S)\w{10,}/g,'$1 $2');
var row = docFragment.getElementById('NewRow'+id);
if (!row)
{
row = docFragment.createElement('tr');
row.setAttribute('id','NewRow'+id);
var cellTitle = docFragment.createElement('td');
cellTitle.setAttribute('width','100%');
cellTitle.setAttribute('class','titlecell');
if (attrTimed == '1')
{
var img = docFragment.createElement('img');
img.setAttribute('src','img/period_small.gif');
cellTitle.appendChild(img);
}
cellTitle.appendChild(docFragment.createTextNode(' '+vname));
var cellRight = docFragment.createElement('td');
cellRight.setAttribute('width', '50');
input = docFragment.createElement('input');
input.setAttribute('name','txNewCount'+id);
input.setAttribute('type','text');
input.setAttribute('value','1');
input.setAttribute('maxlength','3');
hidden = docFragment.createElement('input');
hidden.setAttribute('name','hdTimed'+id);
hidden.setAttribute('id','hdTimed'+id);
hidden.setAttribute('type','hidden');
hidden.setAttribute('value',attrTimed);
cellRight.appendChild(input);
cellRight.appendChild(hidden);
var cellButton = docFragment.createElement('td');
cellButton.setAttribute('width', '20');
var button = docFragment.createElement('button');
button.setAttribute('onclick','javascript:addres('+id+',\''+vname+'\');');
button.setAttribute('style','width:100%');
button.appendChild(docFragment.createTextNode('+'));
cellButton.appendChild(button);
row.appendChild(cellTitle);
row.appendChild(cellRight);
row.appendChild(cellButton);
docFragment.appendChild(row);
}
}
tbl.getElementsByTagName('tbody')[0].appendChild(docFragment);
}