有一个小提琴(在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