i'm just starting out on javascript.
can anyone show me how to go about writing a code to sort a table
column?
something like the following:
<table border=3D"1">
<tr>
<td>
<a href=3D"" onClick=3D"">sort me</a>
</td>
<tr>
<tr>
<td>
2
</td>
<tr>
<tr>
<td>
3
</td>
<tr>
<tr>
<td>
1
</td>
<tr>
</table>
i'm guessing that the code would first read in the table entries as
documents.table.[1].??, assign it as a array, then sort, then delete
the table text, then print the table text.
How to delete the table text?
eventually this would be for a multi-column and multi-row table (a
matrix), and each row or column can be clicked to sort, without
disturbing existing order. The click would act as toggle or reverse
sorting.
Xah
xah@xahlee.org
=E2=88=91 http://xahlee.org/
|
|
0
|
|
|
|
Reply
|
xah (463)
|
8/27/2005 10:26:45 AM |
|
xah@xahlee.org wrote:
> i'm just starting out on javascript.
>
> can anyone show me how to go about writing a code to sort a table
> column?
>
That's the same as trying to ride the Tour de France the first time you are
learning to ride a bike.
Start reading the FAQ (http://jibbering.com/FAQ/) and try something simpler.
> something like the following:
>
> <table border="1">
> <tr>
> <td>
> <a href="" onClick="">sort me</a>
> </td>
> <tr>
> <tr>
Perhaps you should start to learn some basic HTML too; the above snippet is
not valid HTML because of the non-closed <tr>.
> i'm guessing that the code would first read in the table entries as
> documents.table.[1].??, assign it as a array, then sort, then delete
> the table text, then print the table text.
>
> How to delete the table text?
>
Just to give you an idea what's involved, here is an example of how you
could do it:
<script type="text/javascript">
function sortTable() {
var tds = document.getElementsByTagName('TD');
var content = [], val;
for (var i = 1; i < tds.length; i++) {
if (val = tds[i].innerHTML.replace('/[^\d]+/g','')) {
content[i - 1] = val;
}
}
content.sort();
for (var i = 1; i < tds.length; i++) {
tds[i].innerHTML = content[i - 1];
}
}
</script>
</head>
<body>
<table border="1">
<tr>
<td>
<a href="#" onClick="sortTable(); return false">sort me</a>
[...]
> eventually this would be for a multi-column and multi-row table (a
> matrix), and each row or column can be clicked to sort, without
> disturbing existing order. The click would act as toggle or reverse
> sorting.
>
As said before, learn the language first.
JW
|
|
0
|
|
|
|
Reply
|
Janwillem
|
8/27/2005 11:56:02 AM
|
|
<xah@xahlee.org> wrote in message
news:1125138405.860867.65330@g49g2000cwa.googlegroups.com...
i'm just starting out on javascript.
can anyone show me how to go about writing a code to sort a table
column?
I assume you don't really mean to "sort the column" but rather to sort ON
the table column, right? In other words when somebody clicks your control
all the rows should move as single units rather than JUST the column data,
right?
If so then don't both sorting the column - it's messy and annoying to work
with the DOM to fetch values and such.
Instead create a sortable data structure representing your table - probably
an array of objects. A single function would display the table from this
structure. Your control would perform the sort on the structure, then call
the "display" function.
I've an abstraction object that could help here (long URL):
http://www.depressedpress.com/depressedpress/Content/Development/JavaScript/Extensions/DP_ObCollectionOrdered/Index.cfm
It abstracts the processing of an array of objects allowing you to create
them, add and delete from them, sort them, shuffle the contents, etc.
There's an example at the bottom of that page that shows you how to create
and sort a table in this way. You may be able to modifiy to your needs
easily enough.
Hope this helps,
Jim Davis
|
|
0
|
|
|
|
Reply
|
Jim
|
8/27/2005 6:33:21 PM
|
|
xah@xahlee.org wrote:
> i'm just starting out on javascript.
sorting a table is not possible if you are starting ...
http://www.kryogenix.org/code/browser/sorttable/
http://www.mattkruse.com/javascript/sorttable/index.html
--
Stephane Moriaux et son [moins] vieux Mac
|
|
0
|
|
|
|
Reply
|
ASM
|
8/28/2005 12:22:58 AM
|
|
"ASM" <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> wrote in message
news:4311045e$0$7862$8fcfb975@news.wanadoo.fr...
> xah@xahlee.org wrote:
>> i'm just starting out on javascript.
>
> sorting a table is not possible if you are starting ...
Well... perhaps a better way to say it is that if you end up being able to
sort a table then you're not starting out anymore. ;^)
> http://www.kryogenix.org/code/browser/sorttable/
> http://www.mattkruse.com/javascript/sorttable/index.html
For what it's worth both of those example seem a lot more complex than they
need to be for the task - although they both, of course, worth just fine.
Regardless of how complex they are however they both provide decent
abstractions of that complexity so are useful to beginners.
Personally I don't see the utility of using the HTML of the table as the
data storage mechanism. I think thats what makes so many of these solutions
as complex as they are. Managing and sorting the data seprately from the
presentation is so very much simpler.
Jim Davis
|
|
0
|
|
|
|
Reply
|
Jim
|
8/28/2005 2:32:16 AM
|
|
Jim Davis wrote:
> "ASM" <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> wrote in message
> news:4311045e$0$7862$8fcfb975@news.wanadoo.fr...
>
>>xah@xahlee.org wrote:
>>
>>>i'm just starting out on javascript.
>>
>>sorting a table is not possible if you are starting ...
>
>
> Well... perhaps a better way to say it is that if you end up being able to
> sort a table then you're not starting out anymore. ;^)
:-)
>>http://www.kryogenix.org/code/browser/sorttable/
>>http://www.mattkruse.com/javascript/sorttable/index.html
>
> For what it's worth both of those example seem a lot more complex than they
> need to be for the task - although they both, of course, worth just fine.
> Regardless of how complex they are however they both provide decent
> abstractions of that complexity so are useful to beginners.
>
> Personally I don't see the utility of using the HTML of the table as the
> data storage mechanism.
Question was : how to sort a table
on my idea the table is pre-existing
and to sort only on TD.innerHTML or TH is it less complex ?
Of course if you display a JS matrix in a table via document.write() or
else this way, it is a little simplier :
http://perso.wanadoo.fr/stephane.moriaux/truc/tri_matrice.htm
I think thats what makes so many of these solutions
> as complex as they are. Managing and sorting the data seprately from the
> presentation is so very much simpler.
I continue to think that function to sort a matrix is not easy to understand
could be seen here:
<http://groups.google.com/group/fr.comp.lang.javascript/msg/1ca86e2439894ee6?rnum=1>
--
Stephane Moriaux et son [moins] vieux Mac
|
|
0
|
|
|
|
Reply
|
ASM
|
8/28/2005 10:01:52 PM
|
|
|
5 Replies
190 Views
(page loaded in 0.089 seconds)
Similiar Articles: Sorting JTable with fixed column - comp.lang.java.gui... first two columns fixed - It should sort ... books/tutorial/uiswing/components/table.html The discussion includes a way to sort ... previous versions can use the code from ... Unable to sort dataset with 6.7 Million records. ERROR: Utility ...... is the code, I am trying to run. proc sort data ... SQL; DROP TABLE TEMP; PROC SORT DATA = A (WHERE = (by-variable =: 'C')) OUT = temp; etc. Cutting down on the code ... smtp check response 500 - comp.lang.ruby> > *if unable to sort that out* hey pharrington, it is a ruby question after-all ... comp.mail.misc smtp check response 500 - comp.lang.ruby SMTP Server Response Codes Table ... use macro to import multiple excel files? - comp.soft-sys.sas ...... import datafile=&path out=&table replace; getnames=yes; run; proc sort data=&table; by ... four variables LOCATOR FILTER BOOKLET_1 and BOOKLET_2 then you could try code ... binning data using a table - comp.soft-sys.matlab> > Also, is the table in sorted order? You sort of imply it is, but you don't > actually say: you ... sys.matlab Hi, I'm just wondering if anyone is aware of a Matlab code ... Hibernate305: delete query fails with "must begin with SELECT or ...... following the Hibernate 3.0.5 docs to build a query to delete rowsfrom a table. My code ... hi-i use hibernate and the newsgroup over their had some sort of merit system.i ... awk challenge: sort array using only for ... in ... - comp.lang ...4) Awk arrays like Table normally start at "1 ... main script with a *lot* of ... Much has been written in comp.lang.awk and awk.info about using Awk code to sort Awk ... Open Cursor Error Reading Microsoft Access Table - comp.soft-sys ...Attempting to run the following code under SAS 8.2 on Windows XP (values = of the table and keys macro ... comp.soft-sys.sas... read ... error when attempting to sort ... How to generate a table with proc means - comp.soft-sys.sas ...... Percent using proc report - comp.soft-sys.sas proc sql; create table new_file as select code, A ... ... PROC SQL allows you to sort, summarize, merge and update data sets. With ... Simple code encryption (xor) problem - comp.lang.asm.x86 ...This sort of code is also frequently multi-threaded, running on as many CPUs as you have ... Global Offset Table of a ELF executable - comp.unix.programmer ... Simple code ... sorttable: Make all your tables sortablePretty simple. But if you saw that table in a client-side application, you'd expect to be able to click on the headers and have the table sort, would you not? Sortable Table JavaScript: Sort your tables easily!Unobtrusive table sorting javascript with alternating row colors, able to sort text, numbers, currencies and dates in multiple formats. 7/8/2012 8:00:22 PM
|