HighDots Forums  

Sorting HTML Table based on a column (Using JavaScript)

Javascript JavaScript language (comp.lang.javascript)


Discuss Sorting HTML Table based on a column (Using JavaScript) in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Raghuram Banda
 
Posts: n/a

Default Sorting HTML Table based on a column (Using JavaScript) - 09-12-2003 , 03:33 AM






Hi,

I was strucked in a problem, basically I'm working on Sortable Table and
the table is created using HTML Tags and one of the fields in the table
contain Date in unix format (something like Tue Sep 02 01:00:00 2003). I
wrote the script using DOM. I collected innerHTML of a particular cell
and checked whether it a string or not (using isNaN) and based on that I
sorted the rows based on a column. Coming to this date field my sort
technique is not working becauce it treats the cell value as a string
and sorts the table based string sorting. Can any one help me in this
regard.

Thanks in advance


Reply With Quote
  #2  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: Sorting HTML Table based on a column (Using JavaScript) - 09-12-2003 , 05:07 AM






Raghuram Banda <raghuram.banda (AT) nokia (DOT) sify.net> writes:

Quote:
I was strucked in a problem, basically I'm working on Sortable Table
and the table is created using HTML Tags and one of the fields in the
table contain Date in unix format (something like Tue Sep 02 01:00:00
2003). I wrote the script using DOM. I collected innerHTML of a
particular cell and checked whether it a string or not (using isNaN)
"a number or not" I assume. They are all strings.

Using DOM and (non DOM) "innerHTML" is somewhat at a conflict. You can
extract the contents of the cells with DOM as well.

function getInnerText(node) {
if (node.nodeType == 3) { // text node;
return node.nodeValue;
}
if (node.nodeType == 1) { // node
var res = "";
for (var cnode = node.firstChilde;cnode;cnode=cnode.nextSibling) {
res += getInnerText(cnode);
}
return res;
}
return ""; // not content node
}

Quote:
and based on that I sorted the rows based on a column. Coming to this
date field my sort technique is not working becauce it treats the cell
value as a string and sorts the table based string sorting. Can any
one help me in this regard.
The first question you need to answer, is:
What do you want to happen?

Sure, we can convert a date to a number, if we know it is a date. What
number do you want?

Should the above become 20030902 (optionally adding 010000 for the
time, as a direct coding of the date)?
Or 1062457200000 (the value of the corresponding Date object in local time)?
Or 1062464400000 (the value if it is UTC time)?

The obvious answer to your question is: Recognize it as a date and treat
it as such.

/L
--
Lasse Reichstein Nielsen - lrn (AT) hotpop (DOT) com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #3  
Old   
Da Costa Gomez
 
Posts: n/a

Default Re: Sorting HTML Table based on a column (Using JavaScript) - 09-12-2003 , 05:13 AM



Raghuram Banda wrote:
Quote:
Hi,

I was strucked in a problem, basically I'm working on Sortable Table and
the table is created using HTML Tags and one of the fields in the table
contain Date in unix format (something like Tue Sep 02 01:00:00 2003). I
wrote the script using DOM. I collected innerHTML of a particular cell
and checked whether it a string or not (using isNaN) and based on that I
sorted the rows based on a column. Coming to this date field my sort
technique is not working becauce it treats the cell value as a string
and sorts the table based string sorting. Can any one help me in this
regard.

Thanks in advance

Have a look at the following link:

http://webfx.eae.net/dhtml/sortablet...abletable.html

I believe this also deals with dates in a non-string manner.

Cheers,
Fermin DCG



Reply With Quote
  #4  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: Sorting HTML Table based on a column (Using JavaScript) - 09-12-2003 , 04:25 PM



JRS: In article <vfry74u9.fsf (AT) hotpop (DOT) com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lrn (AT) hotpop (DOT) com>
posted at Fri, 12 Sep 2003 11:07:10 :-
Quote:
Raghuram Banda <raghuram.banda (AT) nokia (DOT) sify.net> writes:

and the table is created using HTML Tags and one of the fields in the
table contain Date in unix format (something like Tue Sep 02 01:00:00
2003).
...
...
Should the above become 20030902 (optionally adding 010000 for the
time, as a direct coding of the date)?
Or 1062457200000 (the value of the corresponding Date object in local time)?
But where is local <g> ?

Quote:
Or 1062464400000 (the value if it is UTC time)?

The obvious answer to your question is: Recognize it as a date and treat
it as such.
Since the sort method works on strings, it may be worth adding that the
milliseconds counts can be used for dates in ranges such as (1 to 10 Gs)
2001-09-09 Sun 01:46:40 GMT to 2286-11-20 Sat 17:46:39 GMT.

If Date Objects are to be sorted by default comparison, all the Fridays
cone first ...

I believe function Fn(D1, D2) { return D1 - D2 } and .sort(Fn)
will sort chronologically.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.