473,480 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sorting Tables/Manipulating DOM w/ IE

Hello -

I'm trying to implement a table that will allow the user to sort on
each column, like many applicaitons that have tabular data (e-mail,
song list, etc). It invovles grabbing the table, putting the rows into
an array, then sorting them based on which column was clicked. Then a
new table (DOM createElement) is constructed, adding in the sorted rows
with appendChild. Then the attempt is to replace the old table with
the new table. Of course, this works fine under DOM-compliant browsers
(at least, under Mozilla, Firefox, and Opera), but under IE the table
is removed and never replaced. I will include some basic code,
stripped down from the original that does demonstrate the problem. It
seems that when the rows from the old table are appended to the new one
(through DOM), they are removed from the client visual field. In
DOM-compliant browsers, they are replaced with the subsequent
replaceChild, but not in IE6. I even tried removeChild, then
appendChild; however it is done, IE doesn't re-display the data. The
table disappears. Anyway, I'm probably not explaining this very well,
and am most assuredly missing something, so without further delay,
here's my code for the experts to critique and laugh at :).. :

<html>
<head>
<title>This is a Sort Test</title>
<script type="text/javascript" language="JavaScript">
function generate_compare_rows(column)
{
return function compare_rows(row1, row2) {
var value1 = row1.cells[column].firstChild.nodeValue;
var value2 = row2.cells[column].firstChild.nodeValue;
if (value1 < value2) {
return -1;
} else if (value1 > value2) {
return 1;
} else {
return 0;
}
};
}

function sort_list(table_id, column)
{
var list_table = document.getElementById(table_id);
// Exclude other tables
var dom_rows = list_table.getElementsByTagName("tr");
var num_dom_rows = dom_rows.length;
var table_rows = [];
var table_parent = list_table.parentNode;

for (var i=0; i < num_dom_rows; i++) {
table_rows[i] = dom_rows[i];
}
var header_row = table_rows.shift();
var num_rows = table_rows.length;

table_rows.sort(generate_compare_rows(column));

var new_table = document.createElement("table");
new_table.appendChild(header_row);
for (var i=0; i < num_rows; i++) {
new_table.appendChild(table_rows[i]);
}

if (document.addEventListener) { // Test for standard.
new_table.setAttribute("id", table_id);
table_parent.replaceChild(new_table, list_table);
} else { // Must be IE :) Try something different.
new_table.id = table_id;
table_parent.removeChild(list_table);
table_parent.appendChild(new_table);
}
}
</script>
</head>
<body>
<h1>My Table</h1>
<div id="list_div">
<table id="list">
<tr style="cursor:pointer">
<th onclick="sort_list('list', '0')">Name</th>
<th onclick="sort_list('list', '1')">Description</th>
</tr>
<tr>
<td>John Michaels</td>
<td>Somone with another grudge.</td>
</tr>
<tr>
<td>Mike Smith</td>
<td>Another guy wanting a job.</td>
</tr>
<tr>
<td>Aaron Doe</td>
<td>Dude, this guy is crazy.</td>
</tr>
</table>
</div>
</body>
</html>

Mar 16 '06 #1
2 3025
Hi!

jab3 wrote:
Then a
new table (DOM createElement) is constructed, adding in the sorted rows
with appendChild.
[...]
but under IE the table
is removed and never replaced.


When using dom methods, IE requires you to explicitly create a tbody
element for the table. The tbody must be a child of the table and the
tr elements have to be childs of the tbody element.

Thus, the table is in fact replaced by IE, however, the new table is
empty, because it has no tbody.

Bye

Nils

Mar 16 '06 #2
Nils Bandener wrote:
Hi!

jab3 wrote:
Then a
new table (DOM createElement) is constructed, adding in the sorted rows
with appendChild.


[...]
but under IE the table
is removed and never replaced.


When using dom methods, IE requires you to explicitly create a tbody
element for the table. The tbody must be a child of the table and the
tr elements have to be childs of the tbody element.


Nice. Worked like a charm. Thanks for the info.

-jab3

Mar 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2362
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
4
10016
by: suzy | last post by:
hello. how can i sort data in a dataset? all the examples i have seen on msdn, etc are sorting a dataview. this works fine, but i want to return the results in xml and the dataview doesn't...
8
3478
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
2
4013
by: Tolu | last post by:
Hello I am trying to save information from one form to two tables. I have a table for Student info and Transcript line. I have a form that list all the classes (using text boxes) a student is...
0
3210
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
7
4791
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
0
2053
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any...
3
7299
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
5
4897
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6732
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6886
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4472
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.