2011-03-28 57 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