473,404 Members | 2,137 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,404 software developers and data experts.

Best practices for sortable columns

I have several forms that display information from the database after
users log in. I would like the column titles to be sortable so that
when the user clicks on a column heading, the data re-displays in
sorted order of that column. Next time they click on it, it toggles
the order from ascending to descending, and then back to ascending
again.

Since I have many forms and each form has different columns, I was
wondering if there was a method that would be better than a brute force
approach. What are the best practices for this functionality?

Thanks!

Feb 12 '06 #1
3 1633
Following on from 's message. . .
I have several forms that display information from the database after
users log in. I would like the column titles to be sortable so that
when the user clicks on a column heading, the data re-displays in
sorted order of that column. Next time they click on it, it toggles
the order from ascending to descending, and then back to ascending
again.

Since I have many forms and each form has different columns, I was
wondering if there was a method that would be better than a brute force
approach. What are the best practices for this functionality?

Thanks!

ANSWER No 1
You may have noticed that you don't often see this style of UI in web
pages. The main reason is that users need to be educated in how to use
the method and (even with systems they use everyday for years) don't
seem to cotton on very quickly to the tricks often available with column
headings. The secondary reason is the delayed response when requesting
a new database query and web page. "I just clicked on the heading
hoping it would do something useful but I can't see any effect".

So web pages tend to use the more clunky (but (a) perfectly valid and
(b) much more obvious) method of a drop down box(es) saying something
like
Sorted by [Name v] [x] Ascending [go]
ANSWER No 2
You _may_ find search a more productive method than sort+browse.
Depending on the nature of your data and the calibre or your audience
you _may_ want to take them through a menu of
* - select this way
* - select that way
* - select the other way
It is really really really REALLY important to get all users actually
using the system on their own without either giving up or making a hash
or trying to get technical support. After all you (or perhaps the
system designer) should know what people want to use the system for and
farting around with every column under the sun is a waste of everybody's
time. Proper analysis should reduce "I want to look at the data" to
"which jobs are late" or "which of my jobs are late" or "my jobs" or
"job No X".
ANSWER No 3
There are two steps to displaying lists
(1) Selecting from the database
(2) Displaying a table of results

Item 2 is easy to package. 2-dim array to 2-dim table with column-based
function for say converting dates and adding links.

Item 1 can normally be done with one or two switch structures. These
tend to be pretty obvious when looking at the code and pretty specific
to the option chosen.
eg
switch (sortmode) {
case 'name'
$fields = 'name,age,status';
$where = '';
$sort = 'name';
....
}
and these variables get stuffed into a select.

What I personally have done is to create a class which takes a SQL
statement to fetch X records and then (as required) displays them with
all sorts of column variations and headings. The SQL is 'hand crafted'
but the display is half a dozen bog-standard lines.

So, to summarise: You might want to rethink your UI. Displaying is a
mechanical thing that can be stuffed into a class[1] but setting-up SQL
is more easily done in-line with a few switches.
Well, that's /my/ experience.
[1] With the benefit that each time you add functionality to the class
you can re-use it on other projects.

--
PETER FOX Not the same since the poster business went to the wall
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Feb 13 '06 #2
I often have this requested as a feature of the things i write.
I've yet to come up with a simple elegant solution.

Most often i simply hack it in. Pass a ?sort=name&dir=asc from a link,
store that in a session, and append a ORDER BY clause to all the
relevent queries.
It is a pain, but it gets the job done.

Other solutions:
Create a SortBy array and have each element an array of column name and
direction.
$sort[0] = array('col'=>'first_name', 'dir'=>'ASC');
Then implode it and concatenate it. Messy, but a quick fix if you have
a lot of subsorts needed.
Extending that, create a SortBy class, and append that into all the
relevent queries, propagating it through $SESSION.
$sort = new SortBy($col, $dir); $sort->addCol($col2, $dir2);
$query = "select bla bla $sort->toString()
This lets you easily manipulate the sorted direction and allows more
control over the sort. Basically the previous thing but with a
friendly interface you can reuse in other projects.

My only other solution is to abstract the query so that you can do
whatever the hell you want to it.
$query = new Query();
$query->Select($col1, $col2);
$query->SortBy($col1, $dir);
$query->toString();
Granted, that requires a lot of work, but is the easiest one once its
all said and done.
In fact, i think with the PDO objects you can do just that. I'm not
sure, though, I haven't really looked at those.

Finally, often enough they say they want sort, but they will never use
it. Really they just want to see specific results in specific ways.
I would say wringing as many details for reports as possible out of
them is a better way to go. Its easier for you to do, and they'll like
the final results better.

Feb 13 '06 #3
ne**********@yahoo.com wrote:
I have several forms that display information from the database after
users log in. I would like the column titles to be sortable so that
when the user clicks on a column heading, the data re-displays in
sorted order of that column. Next time they click on it, it toggles
the order from ascending to descending, and then back to ascending
again.

Since I have many forms and each form has different columns, I was
wondering if there was a method that would be better than a brute force
approach. What are the best practices for this functionality?

Thanks!


Another alternative is to do the sort in JavaScript (although it won't
work for those who have JavaScript turned off). One of the sites I've
seen, http://www.utopiapimp.com (a RPG support site) does this.

To see an example, go to the Home Page and click on "Demo 1". Then click
on the "General" after "Column Sets:".

This page uses JS to do the sorting. Just click on the column headers
such as "Acres", "NW", etc. to see what happens.

A very usable UI, IMHO.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 13 '06 #4

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

Similar topics

16
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
2
by: Diffident | last post by:
Hello All, I would like to set css class for datagrid's sortable columns. I cannot do it in the datagrid's header's CSSClass. Datagrid's sortable column headings are generated as <a> tags for...
0
by: cedoucette | last post by:
I just wrote code to support sortable columns in a datagrid. It seems to work fine; but, it doesn't look right. The problem is that I have a generic style for links and a different style for the...
0
by: Louis Aslett | last post by:
I hope this is the correct newsgroup for this query (if not please give me a pointer to where is best): I understand the theory of normalisation etc and am trying to follow best practices in the...
1
by: Henri Schomäcker | last post by:
Hi folks, what I need is a kind of 2D-Array which should, in the end, represent a typical html-table and which should be sortable by column. I thought of an implementation, where an vector...
3
by: TPhelps | last post by:
I have a sample of an unbound (autogeneratecolumns is true) sortable/pagable datagrid that works. I want to change one of the columns to a hyperlink. The examples I find use a bound column. I...
5
by: Robert W. | last post by:
My app runs perfectly when run in Canada or the U.S. But others are experiencing problems. So I switched my computer to the UK culture and immediately saw a problem. This line was failing: ...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
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,...

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.