becoming sorted upon loading

F

fulio pen

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 = 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["ID"] + "</td>");
document.write(" <td class='pinyin'>" + sanjiao["Pinyin"] + "</
td>");

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

Randy Webb

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";
}
}
 
F

fulio pen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top