473,606 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ 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="JavaS cript">
function generate_compar e_rows(column)
{
return function compare_rows(ro w1, row2) {
var value1 = row1.cells[column].firstChild.nod eValue;
var value2 = row2.cells[column].firstChild.nod eValue;
if (value1 < value2) {
return -1;
} else if (value1 > value2) {
return 1;
} else {
return 0;
}
};
}

function sort_list(table _id, column)
{
var list_table = document.getEle mentById(table_ id);
// Exclude other tables
var dom_rows = list_table.getE lementsByTagNam e("tr");
var num_dom_rows = dom_rows.length ;
var table_rows = [];
var table_parent = list_table.pare ntNode;

for (var i=0; i < num_dom_rows; i++) {
table_rows[i] = dom_rows[i];
}
var header_row = table_rows.shif t();
var num_rows = table_rows.leng th;

table_rows.sort (generate_compa re_rows(column) );

var new_table = document.create Element("table" );
new_table.appen dChild(header_r ow);
for (var i=0; i < num_rows; i++) {
new_table.appen dChild(table_ro ws[i]);
}

if (document.addEv entListener) { // Test for standard.
new_table.setAt tribute("id", table_id);
table_parent.re placeChild(new_ table, list_table);
} else { // Must be IE :) Try something different.
new_table.id = table_id;
table_parent.re moveChild(list_ table);
table_parent.ap pendChild(new_t able);
}
}
</script>
</head>
<body>
<h1>My Table</h1>
<div id="list_div">
<table id="list">
<tr style="cursor:p ointer">
<th onclick="sort_l ist('list', '0')">Name</th>
<th onclick="sort_l ist('list', '1')">Descripti on</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 3035
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
2376
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 field... This grid displayes results based on users search.. public static int numberDiv; private void Page_Load(object sender, System.EventArgs e) {
4
10029
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 have a .getxml method (unlike the dataset). any ideas? thanks.
8
3504
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 the moment the printed output is usually going to Word. It's turning into an unholy mess, because I'm having to prepare umpteen different Word templates, and the queries that drive them, depending on what events a course has.
2
4057
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 supposed to take to graduate. This form also has a combo box for assigning grades for each class. I am trying to save information from the form to the student info table and to save each class on the form as separate records in the transcript line...
0
3227
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 way... here is my code on how I did it to help anyone starting out get an idea of how to use virtual mode in ..NET 2.0 Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared
7
4810
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 expandable row, the hidden row is made visible with css. The problem is when i sort the rows, the hidden rows get sorted as well which i don't want and want to be moved (while sorting) relative to their parent rows. The following is my complete html code...
0
2077
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 help........pls urgent ========================================================= <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm3.aspx.vb" Inherits="TestDatagrids.WebForm3"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">...
3
7323
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 uses unfamiliar terms and syntax. Intermediate and advanced Perl coders should find this article useful. The object of the article is to inform the reader, it is not about how to code Perl or how to write good Perl code, but to teach the Schwartzian...
5
4924
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 there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link: http://www.jaredmoore.com/tablesorter/docs/salestable.html Here is some jquery js that I think...
0
8009
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8432
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8428
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8078
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6753
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5456
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3964
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2442
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 we have to send another system
1
1548
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.