Connecting Tech Pros Worldwide Help | Site Map

becoming sorted upon loading

fulio pen
Guest
 
Posts: n/a
#1: Mar 5 '07
Please open the following page:

http://www.pinyinology.com/zhongwen/test3.html

When opened, the data are not sorted, either numerically or
alphabetically. A head in the table, the numeral or the letter, has
to be pressed to get the data sorted by the column. Is there a way
that when the page is opened, the data will become automatically
sorted by the numeral column without pressing the head? Thanks.

Following is the code:

<script type='text/javascript'>
<!--
var sanjiao = new Array ();

for (var i=0; i<6; i++)
{
sanjiao [i]= new Array();
}

sanjiao [0]["ID"] = "5";
sanjiao [0]["Pinyin"] = "d";

sanjiao [1]["ID"] = "2";
sanjiao [1]["Pinyin"] = "f";

sanjiao [2]["ID"] = "4";
sanjiao [2]["Pinyin"] = "b";

sanjiao [3]["ID"] = "1";
sanjiao [3]["Pinyin"] = "e";

sanjiao [4]["ID"] = "6";
sanjiao [4]["Pinyin"] = "a";

sanjiao [5]["ID"] = "3";
sanjiao [5]["Pinyin"] = "c";


function sortByID(a, b)
{
var A = a["ID"].toLowerCase().replace(/\d/g,""),
B = b["ID"].toLowerCase().replace(/\d/g,""),
C = a["ID"].replace(/\D/g,""),
D = b["ID"].replace(/\D/g,"");
if (A < B) return -1;
else if (A B) return 1;
else if (C - D < 0) return -1;
else if (C - D 0) return 1;
else return 0;
}

function sortByPinyin (a, b)
{
var A = a["Pinyin"].toLowerCase();
var B = b["Pinyin"].toLowerCase();
if (A < B) return -1;
if (A B) return 1;
return 0;
}

var sortCriteria = location.search.substring(1);
if (sortCriteria && typeof window[sortCriteria] == "function")
sanjiao.sort(window[sortCriteria]);
//-->
</script>
</head>
<body>


<table border='1' align='left' style='margin-right:15px;'>

<caption>Numerals and Letters</caption>

<tr>
<th><a href="?sortByID">Numeral</a></th>
<th><a href="?sortByPinyin">Letter</a></th>

</tr>

<script type='text/javascript'>
<!--
for (var i=0; i<sanjiao.length; i++)
{
document.write(" <tr>");

document.write(" <td>" + sanjiao[i]["ID"] + "</td>");
document.write(" <td class='pinyin'>" + sanjiao[i]["Pinyin"] + "</
td>");

document.write(" </tr>");
}
//-->
</script>
</table>

Randy Webb
Guest
 
Posts: n/a
#2: Mar 5 '07

re: becoming sorted upon loading


fulio pen said the following on 3/4/2007 8:52 PM:
Quote:
Please open the following page:
>
http://www.pinyinology.com/zhongwen/test3.html
Write a script that checks for a ? in the URL. If none is present, then
you reload it with ?sortByID or ?sortByPinyin and it will get sorted.

window.onload = checkSort;

function checkSort(){
var currentHREF = document.location.href;
if (currentHREF.indexOf('?') == (-1)){
document.location.href = currentHREF + "?sortByID";
}
}

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
fulio pen
Guest
 
Posts: n/a
#3: Mar 5 '07

re: becoming sorted upon loading


On Mar 4, 9:26 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
Quote:
fulio pen said the following on 3/4/2007 8:52 PM:
>
Write a script that checks for a ? in the URL. If none is present, then
you reload it with ?sortByID or ?sortByPinyin and it will get sorted.
>
window.onload = checkSort;
>
function checkSort(){
var currentHREF = document.location.href;
if (currentHREF.indexOf('?') == (-1)){
document.location.href = currentHREF + "?sortByID";
>
}
}
>
It works. Thanks a lot.

fulio pen

Quote:
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

Closed Thread


Similar JavaScript / Ajax / DHTML bytes