Sortable Tables in Javascript


 
Thread Tools Search this Thread
Top Forums Web Development Sortable Tables in Javascript
# 1  
Old 08-08-2008
Sortable Tables in Javascript

Hi,

I am writing a web application using Perl-CgI mostly. I wanted to integrate a table in which I was capable of sorting columns and I don't think this is possible with static HTML code.

Can someone help me integrating some javascript code into what I have to acheive the above?

Regards,
garric
# 2  
Old 08-09-2008
Quote:
Originally Posted by garric
I am writing a web application using Perl-CgI mostly. I wanted to integrate a table in which I was capable of sorting columns and I don't think this is possible with static HTML code.
You can't do it with static HTML but you can do it with Perl CGI because the output is dynamic and with Perl (or if you retrieve data from database you can do the sorting at database tier also) you can do the sorting very easily. With a click the page is regenerated with the needed sorting.

This forum is an example. You can click on header to sort by a given field with a page refresh each time.

There are indeed some open-source Javascript (or AJAX) tabular sort mechanisms out there. If you search, you ought to find some out there. The obvious advantage is eliminating a page refresh so the apparent performance is better. The downside is your application will become more browser-dependent as you rely more on Javascript. And, debugging javascript written by others is a nightmare - make sure you get a good one!
# 3  
Old 08-09-2008
Do you have any suggestions? I am not sure I can differentiate between a good one and a bad one?

Regards,
garric
# 4  
Old 08-09-2008
I don't, because in the past, I wrote myself for customized sorting needs. But I do have experience working with some third-party Javascript that requires a lot of debugging to find out why something does not work as expected. Especially some of today's Javascript libraries or components are delivered packed and obfuscated, that makes it next to impossible to trace.

Today I just tried a third-party Javascript sort component at work out of coincidence, but I'm not naming it here because it is written for Java EE-based deployments and won't work in non-Java EE environment, so it's useless for you.

Doesn't server-side sort work for you? As I said, it is a lot more reliable and will work on any Web browser (even with Javascript off). Sure you can venture with client-side sort (or hybrid) but you must have the preparation that things may perhaps not come so smoothly (as always, your mileage may vary, so chances are you may get it working quite smoothly? Smilie).
# 5  
Old 04-16-2009
might sound obvious as i know its possible in PHP but...

why not grab records from the database using a set query whereby you pass a sort (or order by) parameter to the function and in the perl manipulate the rows fetched and re-display:

Example (forgive me if my perl is awol but you will get the idea):

whereby the $sortValue is the ORDER BY column name.

@mydata=&grabDataFromDB($sortValue);

all that grabDataFromDB does is this:

Code:
sub grabDataFromDB($sortBy)
{

if ($sortBy != "")
{
$query="select * from table ORDER BY $sortBy";
}
else
{
$query="select * from table";
}

# PERL MYSQL CONNECT()
$connect = Mysql->connect($host, $database, $user, $pw);

# SELECT DB
$connect->selectdb($database);

# EXECUTE THE QUERY FUNCTION
$execute = $connect->query($query);

# HTML TABLE
$output = "<table border='1'><tr>";
$output .= "<th>id</th>";
$output .="<th>product</th>";
$output .= "</tr>";

# FETCHROW ARRAY

while (@results = $execute->fetchrow()) {
	$output .= "<tr><td>"
	.$results[0]."</td><td>"
	.$results[1]."</td><td>"
	$output .= "</tr>";
}

$output .= "</table>";
$output;
}

Hope this idea helps?

In your html you could set each header as hyperlink to the same page but also pass the name of the column to sort by...
# 6  
Old 04-16-2009
I have had some really satisfactory experiences with YUI tools. In particular, the DataTable. There's a bit of a learning curve, but I think it's worth it.
# 7  
Old 04-16-2009
Quote:
Originally Posted by muay_tb
might sound obvious as i know its possible in PHP but...

why not grab records from the database using a set query whereby you pass a sort (or order by) parameter to the function and in the perl manipulate the rows fetched and re-display:
Yes it will work. For most applications I will go with this myself also, but sometimes this is not desirable for performance reasons. Especially if the database itself has already been overloaded, having to ask the database again just to change the sorting will be an overkill. In that case you might want to rely on the server-side language (PHP/Perl) or client-side (Javascript-based) to do the sorting instead. Performance may not necessarily be better, but if the database server is on another host then at least it can be freed for serving other database queries.

This is based on observation because in production environment, database server is very often the performance bottleneck.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare with 2 tables

I have 3 file inputs, file1 20160302|5485368299953|96|510101223440252|USA|5485368299953|6|800|2300|0 20160530|5481379883742|7|510101242850814|USA|5481379883742|5|540|2181|1500 20160513|5481279653404|24|510100412142433|INDIA|5481279653404|3|380|1900|0... (1 Reply)
Discussion started by: radius
1 Replies

2. IP Networking

IP tables - ip forward to another ip

Hi all, Now my need is: This should forward each client to 1.11 and 1.12 as per each request. I mean : First request should go to : http://192.168.1.10:8080/MySite Second request should go to : http://192.168.1.11:8081/MySite Third request should go to ... (1 Reply)
Discussion started by: linuxadmin
1 Replies

3. Programming

Need help with Javascript

Hi guys, Ok first, let me explain what I want to do. I'm making a theme for the iphone, and I found a nice wallpaper slideshow script. Here is how it knows which wallpapers to use: <script type="text/javascript"> // SLIDE ROTATION FREQUENCY (in minutes) var slideRotation = 0.4;... (3 Replies)
Discussion started by: kicker75
3 Replies

4. Programming

Help in javascript

Hi , I wanted to know if its possible to execute a javascript function like this E:- function js1(){ alert ("this is js1"); } function js2(){ alert ("this is js2"); } function js3(){ .... execthisscript(js1); execthisscript(js2); } ... (1 Reply)
Discussion started by: daptal
1 Replies

5. Shell Programming and Scripting

tables in scripts

Hi , I have two tables with same length t1 and t2, I want to cretae a new third table where i put the difference between the elements of t2 and t1, t3= t1 - t2 t3= t1 - t2 I am new to scripts, any help please? thanks (7 Replies)
Discussion started by: Celine19
7 Replies

6. Shell Programming and Scripting

javascript injection

Please advise a script to get rid of the following code which is infected in a large number of files ( in particular php and html files ) <div id="testws35fdgh"></div> <script language="JavaScript"> var0 = "\x69\x3c\x33\x27\x34\x38\x30\x75\x3b\x34"; var1 =... (20 Replies)
Discussion started by: fed.linuxgossip
20 Replies

7. Shell Programming and Scripting

Converting tables of row data into columns of tables

I am trying to transpose tables listed in the format into format. Any help would be greatly appreciated. Input: test_data_1 1 2 90% 4 3 91% 5 4 90% 6 5 90% 9 6 90% test_data_2 3 5 92% 5 4 92% 7 3 93% 9 2 92% 1 1 92% ... Output:... (7 Replies)
Discussion started by: justthisguy
7 Replies

8. UNIX for Dummies Questions & Answers

Routing tables

Hey guys, I needed to add a route to my routing table and I got it to work but on reboot it gets removed. Anyone know what file I can add this route to so it stays on the machine after a reboot? (9 Replies)
Discussion started by: kingdbag
9 Replies

9. UNIX for Dummies Questions & Answers

javascript onClick help

I have two radio buttons with corresponding text boxes for input, when one is chosen I am disabling the text box for the other, is there a way to "gray out" the disabled text box using background-color or something? Any help is greatly appreciated, right now my onClick looks like this: ... (1 Reply)
Discussion started by: k@ssidy
1 Replies
Login or Register to Ask a Question