Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to sort onload

Status
Not open for further replies.

Inyokern

Technical User
Feb 20, 2006
8
US
I have a database-generated (.asp) list and I want to display it in alphabetical order when it loads up on a page. It does not have to be resortable.

Is there a simple way to do this in javascript?
 
Someone gave me a great function she called "doCoolAttachThing" that took all hyperlinks added when the page loaded and set the onClick event dynamically. I used this function in the body onLoad event of the page. Does that sound like something that might help? I'll go get the code and post it in a few minutes...perhaps you can taylor it to your needs...

 
Code:
function doCoolAttachThing() {
    var a = document.getElementsByTagName("a");
   for ( var i = 0; i < a.length; i++ )
        a[i].onclick = function() {cancelscreen();alert("You are leaving the Tee Time Signup section.");};

}

and in my body onLoad I have

Code:
<body text="#ffffff" bgcolor="#FFFFFF" onLoad="doCoolAttachThing()">


Now I just noticed at the beginning of this page my associate loads and sorts an array that he brings in through his asp. This is what he has:

Code:
var firstarray = new Array()
var secondarray = new Array()
var thirdarray = new Array()
var fourtharray = new Array()

<%for i = 0 to ubound(p1ret,2)%>
 firstarray[<%=i%>] = <%=p1ret(1,i)%>
 secondarray[<%=i%>] = <%=p1ret(2,i)%>
 thirdarray[<%=i%>] = '<%=p1ret(3,i)%>'
 fourtharray[<%=i%>] = '<%=p1ret(4,i)%>'
 
<%next%>

This both loads and sorts his array. I am not sure if any of this helps but I hope so. If you need more info let me know!

 
One more note - that part that my associate does to load and sort his array is at the very beginning of the javascript at the bottom of our page. Before any functions. Don't know if it has to be that way but thought it might be worth mentioning.
 
Thanks for the quick response.

Is "firstarray" the ID of the table?
 
firstarray is just the name of one of his 4 arrays he's loading in and sorting. It is an array of names, and each array has another item of info related. But no it's not a table name. Can you show your asp code where you load your list you want to sort?

-trix
"Wiggle your big toe.
 
Code:
<%
For Each Category In categories
%>

<table>
<tr><td><%=Category.CategoryName%></td>
<td><%=Category.ItemCount%></td></tr>
</table>

This is a simplified version of it. I want the left column to display alphabetically.
 
So you have a table with how many columns? And you want the farthest left column to control the sort order alphabetically? If you bring your data into an array, you can use the array elements in the table cells instead of having to refer to the table directly. I bring data for my array into the form, put it in an array in the asp section, then use the array elements for display. Maybe I'm not following how you're trying to do it exactly...

-trix
"Wiggle your big toe.
 
There are only two columns, CategoryName and ItemCount and, yes, I want to sort alphabetically by CategoryName.

Could you give me an example of how to bring .asp data into a javascript array? I am not a programmer.
 
Okay I can get some code and show you but, we might not be working with the same thing. First tell me where does your data come from? I'm using a SQL Server database and have a dll that calls information from that database for me. Where does your data come from?

-trix
"Wiggle your big toe.
 
The data comes from an SQL server database.

I am not a programmer. I bought this program "off the shelf" and I modify it with various scripts I have found. The sellers of the program could probably do this for me but it takes them 2 or 3 months to get to it.

I found a great table sorting script that works by clicking the headers but I just can't seem to find one that sorts a simple table onload. (I would think that would be easier)
 
It would be I understand. I think my code would overcomplicate it for you. Kind of a different setup. Can you post the table sorting script please? I'm looking online for some kind of simple sort script. There's got to be something simpler than where I was headed. Might help if I could look at the other script.

-trix
"Wiggle your big toe.
 
Did this solve your problem? That's what I was looking at earlier...

-trix
"Wiggle your big toe.
 
This program is great for tables I want to make resortable but it does not sort onload, at least not as far as I can tell.

If it could be modified to sort onload, that would be great but I don't know how to do that.
 
Dan,

>If you have access to server-side scripting, why on earth would you want to sort this client-side?<

Just because I have access to it doesn't mean I know how to use it :)

I have to sort the list different ways on different pages.

I was hoping there was simple Javascript that I could use to sort alphabetically. If such a thing does not exist, I guess I will go to the seller of the program and have him set it up server-side.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top