2011-03-28 22 views
0

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

回答

1

该代码行将全局变量gbAscending设置为true或false。也许sortDir会更好地命名为SortAscending。

gbAscending = sortDir; 

因此,本质上你设置SortAscending = true或SortAscending = false。这是全局性的,所以它不需要传递给sortCallBack方法。

if (text1 < text2) 
    return gbAscending ? -1 : 1; 
else if (text1 > text2) 
    return gbAscending ? 1 : -1; 
else return 0; 

gbAscending? -1:1;

是一个简写,如果然后else语句。

所以

return (if gbAscending then -1 else 1); 

和碱箱子0,如果文本1 =文本2。

这1和-1与sortDir无关,因为它直接绑定到gbAscending。

基本上这里一个函数被传入到array.sort()中。数组元素根据每对元素“a”和“b”以及函数的返回值进行排序。

可能的返回数字是:

  • < 0 - 一个是具有较低的指数是b在列表
  • 0 - a和b相等,从而不排序执行
  • > 0 - a是在列表中有比b高的指数
+0

谢谢。所以如果gbAscending被设置为true,但是如果a在代码中低于b,那么当它返回[gbAscending? -1:1;]是gbAscending现在设置为false?它返回gbAscending和-1? – Leahcim 2011-03-28 11:16:14

+0

编号gbAscending不会在sortTable方法中被更改,它不会被返回。如果一个 Kamal 2011-03-28 11:21:23

+0

o.k.,谢谢你解释。 – Leahcim 2011-03-28 11:25:53

1

sortDir控制全局变量gbAscending是如何设置的,它控制哪些比较功能一样。

(即传入Array.sort的东西)被调用时Array.sort需要比较两个元素,以及它应该做的是返回的东西正,零或负取决于是否它的第一个参数应该是后整理,与之一起,或者在第二次论证之前。有关详细信息,请参阅https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort

因此,此代码中的函数会切换其比较结果,具体取决于gbAscending,这取决于sortDir

1

呵呵很简单: 1)

if (text1 < text2) 
    return gbAscending ? -1 : 1; 
else if (text1 > text2) 
    return gbAscending ? 1 : -1; 
else return 0; 

上面包含在回调函数返回-1表示项目shold下井名单,1是指项目应该上去

2 )实际上它取决于 - 根据该表将被排序升序或降序默认,据我所知

+0

非常感谢。请澄清。你怎么知道-1意味着项目应该在列表中?这是确定的?它是一个预先确定的JavaScript函数,还是它被设置在这个代码中的某个地方? – Leahcim 2011-03-28 11:07:54

+0

这是回调函数的意图: http://solutoire.com/2007/05/02/sorting-javascript-arrays/ http://www.improvedsource.com/view.php/How_to_do_custom_sorting_in_Javascript_on_arrays/44/ – Roman 2011-03-28 11:13:28

+0

并返回gbAscending AND 1还是返回设置gbAscending值为1? – Leahcim 2011-03-28 11:19:01