有一個小提琴(在http://jsfiddle.net/mjmitche/UhzfX/)下面的JavaScript,連同一些HTML和CSS。如果您點擊圖像應該位於提琴的位置,則雜貨商品列表按照字母順序和/或編號排序 - 具體取決於您點擊的圖像。JavaScript排序
此代碼取自我正在嘗試理解的教程(不在線提供)。我查看了排序文件,但仍然遇到了麻煩。
問題
1)內function insertSortControls
,對匿名函數調用另一個函數function() { sortTable('theList', 0, true)}
的調用。根據列表應該是升序還是降序,各種調用中的最後一個參數要麼設置爲true
,要麼設置爲false
。如果你低頭看看函數sortTable的定義是function sortTable(whichTable, whichCol, sortDir)
,你會看到true或false似乎是指sortDirection。我的問題是,設定真假如何影響分揀方向?你能解釋一下嗎?
2)內部的功能sortCallBack,你的代碼看起來像這樣
if (text1 < text2)
return gbAscending ? -1 : 1;
else if (text1 > text2)
return gbAscending ? 1 : -1;
else return 0;
什麼做1和-1是指和他們是如何聯繫,如果在所有的「真」和「假「設置在上面的問題1中描述。
謝謝。
var gbAscending = true;
var gbCol = 0;
function addEventHandler(oNode, sEvt, fFunc, bCapture) {
if (window.attachEvent)
oNode.attachEvent("on" + sEvt, fFunc);
else
oNode.addEventListener(sEvt, fFunc, bCapture);
}
function insertSortControls() {
var oLink;
oLink = document.getElementById('ItemDescend');
oLink.removeAttribute('href');
addEventHandler(oLink, "click", function() { sortTable('theList', 0, true) }, false);
oLink = document.getElementById('ItemAscend');
oLink.removeAttribute('href');
addEventHandler(oLink, "click", function() { sortTable('theList', 0, false) }, false);
oLink = document.getElementById('PriceDescend');
oLink.removeAttribute('href');
addEventHandler(oLink, "click", function() { sortTable('theList', 1, true) }, false);
oLink = document.getElementById('PriceAscend');
oLink.removeAttribute('href');
addEventHandler(oLink, "click", function() { sortTable('theList', 1, false) }, false);
}
function sortCallBack(a, b) {
// each of the arguments passed to this function is a TR node
// with one or more child TD nodes.
// get the child node of each TR element that corresponds
// to the column to be sorted.
var col1 = a.getElementsByTagName("TD")[gbCol];
var col2 = b.getElementsByTagName("TD")[gbCol];
// now get the text node for each col
var text1 = col1.firstChild.data;
var text2 = col2.firstChild.data;
// now that we have the text nodes, do the sorting
if (text1 < text2)
return gbAscending ? -1 : 1;
else if (text1 > text2)
return gbAscending ? 1 : -1;
else return 0;
}
function sortTable(whichTable, whichCol, sortDir) {
// get the table object that has the ID we were given as an argument
var oTable = document.getElementById(whichTable);
// begin by getting the node for the TBODY, since that
// node contains all the rows to be sorted
var oTBody = oTable.getElementsByTagName('TBODY')[0];
// get all of the TR tags within the tbody
var aTRows = oTBody.getElementsByTagName('TR');
// store the length of the TR array
var numRows = aTRows.length;
gbAscending = sortDir;
gbCol = whichCol;
// make an array to hold each TR tag in the body.
var theSortedRows = new Array(numRows);
// copy each TR tag into the array. Do a "deep clone" on
// each TR tag so that all of the child TD tags come along
// with it.
var i;
for (i = 0; i < numRows; i++) {
theSortedRows[i] = aTRows[i].cloneNode(true);
}
// now -- sort the array!
theSortedRows.sort(sortCallBack);
// now that the array has been sorted, we put back all of the
// table rows that we had copied out earlier.
// First, get rid of the the current TBODY.
oTable.removeChild(oTBody);
// make a new one in its place
oTBody = document.createElement("TBODY");
oTable.appendChild(oTBody);
// now insert all of the sorted TR tags from the sorted array
for (i = 0; i < numRows; i++) {
oTBody.appendChild(theSortedRows[i]);
}
}
addEventHandler(window, "load", insertSortControls, false);
謝謝。所以如果gbAscending被設置爲true,但是如果a在代碼中低於b,那麼當它返回[gbAscending? -1:1;]是gbAscending現在設置爲false?它返回gbAscending和-1? – Leahcim 2011-03-28 11:16:14
編號gbAscending不會在sortTable方法中被更改,它不會被返回。如果一個 Kamal 2011-03-28 11:21:23
o.k.,謝謝你解釋。 – Leahcim 2011-03-28 11:25:53