473,395 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Table sorts question

I have been trying to use a couple of javascripts to sort a table by
clicking on column headings.
Sorttable.js and tablesort.js which I found on the web...

I am encountering one problem.
In my table, the content of the first cell in each row contains a link
to another location.

When my table is sorted the displayed table appears correct(sorted)
but when I click on the link in the first column it goes to the link
which was there before the sort took place.

When I view source of the page, it always appears as my original html
page.

Obviously I am a javascript beginner(a stretch at that) so I am having
a hard time understanding what is happening.

It looks to me like the screen display is being changed/sorted, but
the actual html in my html file is not and the click is taking the
values from the source file..

Am I correct?

TIA

Brian
Jul 20 '05 #1
7 2899
BRIAN <bb*****************@rogers.com> writes:
I have been trying to use a couple of javascripts to sort a table by
clicking on column headings.
Sorttable.js and tablesort.js which I found on the web...
Could be mine :)
<URL:http://www.infimum.dk/privat/sorttable.html>
It was made as an example for someone in this group, IIRC.
I am encountering one problem.
In my table, the content of the first cell in each row contains a link
to another location.

When my table is sorted the displayed table appears correct(sorted)
but when I click on the link in the first column it goes to the link
which was there before the sort took place.
Highly curious, and sounds mostly like a bug.
Can you show us the page (give a link, or make a small example
file with lines no longer than 72 characters and post it here).
Also specify which browser and version you use.
Obviously I am a javascript beginner(a stretch at that) so I am having
a hard time understanding what is happening.
I'm not, and so do I :)
It looks to me like the screen display is being changed/sorted, but
the actual html in my html file is not and the click is taking the
values from the source file..


The View Source always show the original source, not the current document
structure. I sometimes use this bookmarklet to show the value of innerHTML:

javascript:(document.documentElement||document.bod y).innerHTML.replace(/&/g,"&amp;").replace(/</g,"&lt;")

It's not pretty, but it shows me what I need. In Mozilla/Netscape 7
you can also use the DOM Inspector to see the current tree structure.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Lasse Reichstein Nielsen wrote:
BRIAN <bb*****************@rogers.com> writes:

I have been trying to use a couple of javascripts to sort a table by
clicking on column headings.
Sorttable.js and tablesort.js which I found on the web...

Could be mine :)
<URL:http://www.infimum.dk/privat/sorttable.html>
It was made as an example for someone in this group, IIRC.

I am encountering one problem.
In my table, the content of the first cell in each row contains a link
to another location.

When my table is sorted the displayed table appears correct(sorted)
but when I click on the link in the first column it goes to the link
which was there before the sort took place.

Highly curious, and sounds mostly like a bug.
Can you show us the page (give a link, or make a small example
file with lines no longer than 72 characters and post it here).
Also specify which browser and version you use.

Obviously I am a javascript beginner(a stretch at that) so I am having
a hard time understanding what is happening.

I'm not, and so do I :)

It looks to me like the screen display is being changed/sorted, but
the actual html in my html file is not and the click is taking the
values from the source file..

The View Source always show the original source, not the current document
structure. I sometimes use this bookmarklet to show the value of innerHTML:

javascript:(document.documentElement||document.bod y).innerHTML.replace(/&/g,"&amp;").replace(/</g,"&lt;")

It's not pretty, but it shows me what I need. In Mozilla/Netscape 7
you can also use the DOM Inspector to see the current tree structure.

/L


Like the original poster, I am a javascript beginner. I, too, have been
working with Stuart Langridge's sorttable.js:

http://www.kryogenix.org/code/browser/sorttable/

I like it because it's very simple to implement. Unfortunately, I'm
having two problems with it.

1. He created a class called "sortbottom" for rows which contain data
such as totals so that they'll always be placed in the bottom row. But
he didn't create a class called "sorttop" or "static" for those rows
that aren't the top row but should remain in place rather than getting
sorted. It's easier to show than explain:

http://www.elementarydesign.com/Baup...ryCompact.html

BTW, I got much of the (x)html/css from Elderweb.com's excellent "When
You Absolutely, Positively Need a Table." It's valid code and very
useful... but it would be more useful if I could get the sorttable.js to
support the markup.

2. Javascript seems to choke when it comes to sorting numbers with
recursive commas. I've seen some work in this area by a frequent poster

http://www.merlyn.demon.co.uk/js-maths.htm#RComma

but I lack the ability to take his work and make it work in sorttable.js

I've been working for a week or so on a number of ideas, none of them
even close to successfully. Any help would be greatly appreciated.

Thanks,

abc3

Jul 20 '05 #3
JRS: In article <YF***************@nwrdny02.gnilink.net>, seen in
news:comp.lang.javascript, aboycalled3 <ab*********@myNOrealSPAMbox.com>
posted at Fri, 12 Dec 2003 20:37:12 :-

2. Javascript seems to choke when it comes to sorting numbers with
recursive commas. I've seen some work in this area by a frequent poster

http://www.merlyn.demon.co.uk/js-maths.htm#RComma

but I lack the ability to take his work and make it work in sorttable.js

You need to be aware of the importance of the difference in javascript
between a Number and a String.

A Number is a 64-bit IEEE floating-point double, and can be used
arithmetically.

A String is an ordered arrangement of characters, and can be input or
output.

There is automatic conversion between Number and String, which quite
often does what is wanted.

If something contains commas, it cannot be a Number and AFAICS it must
be a String.

Using the default sorting functions :

Number sorts use the numerical value of the Number, sorting into
numerical order.

String sorts use lexical alphanumerical order, starting from the left.

Thus 3 < 2e4, but "3" > "2e4".

To get numerical-magnitude-order by default-sorting Strings, the strings
must be such that the numbers are correspondingly-formatted and the
"ones" are all in the same place; as it happens, it does not matter
whether the shorter numbers are padded with space or zero, provided that
all use the same. They must also not be signed numbers (generally).

You can define any comparison function you like for sorting; you could
even sort a list of numbers into alphabetical order after translation
into Danish, if you could count in Danish words.

Really, commas should only be used when numbers are presented for human
reading; they add no information.

--
© 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> Jsc maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.
Jul 20 '05 #4
Dr John Stockton wrote:
JRS: In article <YF***************@nwrdny02.gnilink.net>, seen in
news:comp.lang.javascript, aboycalled3 <ab*********@myNOrealSPAMbox.com>
posted at Fri, 12 Dec 2003 20:37:12 :-
2. Javascript seems to choke when it comes to sorting numbers with
recursive commas. I've seen some work in this area by a frequent poster

http://www.merlyn.demon.co.uk/js-maths.htm#RComma

but I lack the ability to take his work and make it work in sorttable.js


You need to be aware of the importance of the difference in javascript
between a Number and a String.

A Number is a 64-bit IEEE floating-point double, and can be used
arithmetically.

A String is an ordered arrangement of characters, and can be input or
output.

There is automatic conversion between Number and String, which quite
often does what is wanted.

If something contains commas, it cannot be a Number and AFAICS it must
be a String.

Using the default sorting functions :

Number sorts use the numerical value of the Number, sorting into
numerical order.

String sorts use lexical alphanumerical order, starting from the left.

Thus 3 < 2e4, but "3" > "2e4".

To get numerical-magnitude-order by default-sorting Strings, the strings
must be such that the numbers are correspondingly-formatted and the
"ones" are all in the same place; as it happens, it does not matter
whether the shorter numbers are padded with space or zero, provided that
all use the same. They must also not be signed numbers (generally).

You can define any comparison function you like for sorting; you could
even sort a list of numbers into alphabetical order after translation
into Danish, if you could count in Danish words.

Really, commas should only be used when numbers are presented for human
reading; they add no information.


Thank you, Dr Stockton, both for the website and for following up with
the above post. I have a much better sense of the philosophy than I
would without your assistance. But I'm still not sure how to apply your
suggestions.

1. Should I represent numbers like 10,234,567,890 as 10234567890? It
would be trivial in the spreadsheet I use to prepare the data. But while
I realize the commas don't add in any information, the end-product is
intended for human reading and I have a hard time reading the numbers
withouth them. I'm not sure I'd be comfortable with that solution.

2. Should I idenify numbers which use recursive commas, strip the
commas, sort them, and then reinsert the commas?

Sorttable.js works out types for data:

<code snippet>

// Work out a type for the column
if (table.rows.length <= 1) return;
var itm = ts_getInnerText(table.rows[1].cells[column]);
sortfn = ts_sort_caseinsensitive;
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;
if (itm.match(/^[£$]/)) sortfn = ts_sort_currency;
if (itm.match(/^\s*-?[\d\.]+\%?\s*$/)) sortfn = ts_sort_numeric;

</code snippet>

Would it make sense to add in a type for numbers with recursive commas,
e.g.,

if (itm.match(/^(.*\s)?([-+\u00A3\u20AC]?\d+)(\d{3}\b)/)) sortfn =
ts_sort_recursive_comma;

(pattern matching stolen directly from function RComma(S) at
http://www.merlyn.demon.co.uk/js-maths.htm#RComma)

and then strip the commas, sort the data, and add the commas back in
(which I can suggest but don't know how to accomplish -- has anyone
already written this function)?

3. Or is there a better way?
Jul 20 '05 #5
Re String/Number comparisons.

Why not create the table in Javascript from a 2D Array of Objects?

Use the DOM to create a table, and then sort using the Array rather than
Strings from the table cells.

I have a working version at work. It sorts correctly on Numbers,
Strings, and Dates.

Sort the Array, then reload the table.

Jul 20 '05 #6
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
You can define any comparison function you like for sorting; you could
even sort a list of numbers into alphabetical order after translation
into Danish, if you could count in Danish words.


For no apparent reason, I have made a converter from numbers to Danish:
<URL:http://www.infimum.dk/privat/toDanishNumbers.html>
(No work has been put into making it work in less than standard compliant
browsers, and it uses both "eval" and "toFixed" :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7
JRS: In article <_i*****************@nwrdny02.gnilink.net>, seen in
news:comp.lang.javascript, aboycalled3 <ab*********@myNOrealSPAMbox.com>
posted at Sat, 13 Dec 2003 17:49:46 :-
recursive commas.

BTW, 'recursive' applies only to the method of generation. The
resulting string is just as any other method would give.

Please do not quote more than is necessary.

1. Should I represent numbers like 10,234,567,890 as 10234567890? It
would be trivial in the spreadsheet I use to prepare the data. But while
I realize the commas don't add in any information, the end-product is
intended for human reading and I have a hard time reading the numbers
withouth them. I'm not sure I'd be comfortable with that solution.
The beginning of this thread is not at present available to me; I do not
see how the spreadsheet relates to the javascript. But AIUI
spreadsheets also store numbers as standard binaries, and strings as
rows of characters. If your sheet shows commas, that is a display
option.

You could have numbers shown with commas in one column, for reading, and
the same numbers without commas, for exporting.

2. Should I idenify numbers which use recursive commas, strip the
commas, sort them, and then reinsert the commas?
No. If they have commas, they are not numbers.

Would it make sense to add in a type for numbers with recursive commas,
You could do, but ISTM that you should know what you are sorting. A
date dd/mm/yyyy should probably not be sorted in the same way as a part
number kk/qq/aaaa.

3. Or is there a better way?


Preferably, sort on the binary numbers themselves.
Otherwise, sort on any string form, provided that it had been padded so
that the twos of 2, 12, 112, 1112, etc. are all at the same distance
from the left. Other constant characters make no difference; the
essential is that an alphanumeric short should give also numeric order.

--
© 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> Jsc maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.
Jul 20 '05 #8

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

Similar topics

34
by: Ian Rastall | last post by:
I'm doing a small project right now where I create a table from a database. This is the code, which Dreamweaver, for the most part, has written: *** <?php mysql_select_db($database_tdream,...
3
by: Raghuram Banda | last post by:
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...
6
by: Finn Newick | last post by:
By defining a style as follows: @media aural, handheld {td.layout {display:block;}} I'm hoping to linearise layout tables when viewed by screenreaders and handheld devices (it is also be in...
1
by: Jethro | last post by:
This may be an easy question for you experienced guys, but not for me. I have written lots of code in C, but not C++ and not in windows (old fart). I'm working on a simple program to learn the...
11
by: Norman L. DeForest | last post by:
Am I misunderstanding the CSS specifications or is Firefox (version 1.0.6) (and Opera) doing the wrong thing? It appears that Firefox 1.0.6 includes the border in width calculations for tables...
2
by: G.W. Lucas | last post by:
I apologize if this is a RTFM question, but I have not been able to find a definitive answer elsewhere. Does a "REINDEX TABLE" lock the table while it is working? Can applications write data to...
6
by: Rosy | last post by:
I am setting up a DB for our company and am at a loss on the best way to handle some of the records. I have a table (tblJobs) that houses the basic info on the job (when, where, what, who)....
2
by: Brian Mitchell | last post by:
Ok, I know this is an elementary question but I have a data grid that is bound to a data table and I can't seem to find a way to match the selected row in the grid with it's respective row in the...
13
by: Pablo | last post by:
Hi at all I'ld like to center a table crossbrowser therefore I wrote: <sryle> table {width:80%;margin-left:auto;margin-right:auto;} </style> firefox work fine but MSIE set the margin to zero...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
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
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...

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.