2013-03-08 63 views
-1

我在查找表排序問題的解決方案時遇到了一些問題。在兩列上排序表

我的表是本質上的文件目錄:

Name   FileSize FileType 
folder1/  -   folder 
folder2/  -   folder 
abc.pdf  3.2MB  pdf 
def.jpg  1.2MB  jpg 
ghi.doc  1.5MB  doc 

我想爲目錄留在表的頂部,無論哪一列進行排序。例如,對「名稱」進行排序將按名稱對目錄進行排序,然後按名稱對文件進行排序。基本上,所有種類都需要首先在FileType上進行排序,「文件夾」是最高值,然後是名稱或文件大小。

我一直在使用臭名昭着的「頻率解碼器」排序腳本,但會歡迎另一個腳本,如果它使它更容易。

回答

0

有一個jquery tablesorter插件只是爲你。

http://tablesorter.com/docs/

有了這個插件,你可以禁用某些事情短路

http://tablesorter.com/docs/example-meta-headers.html

+0

也許我沒有正確理解它,但我沒有看到如何禁用一列,將實現上面解釋的情況。你能澄清一下嗎? – dsol828 2013-03-08 13:03:01

+0

這個插件有這麼多選項,你甚至可以添加你的代碼,並使其工作,如你所願。如果你想要簡短的東西,同時你不想要短的東西,你可以使用jquery,這個插件只是做它做的。 – 2013-03-08 13:11:06

0

對於每個文件夾名字符串使用String.fromCharCode(0)在前面,這些永遠是按字母順序之前任何其他字符串,但將顯示爲只是名稱。

String.fromCharCode(0)+"folder1/"; 
String.fromCharCode(0)+"folder2/"; 

在任何比較不要becareful但正如

使用String.fromCharCode(0)+ 「文件夾1 /」 不等於 「文件夾1 /」

0

你說什麼關於那個? 排序腳本檢查對象是否是文件夾,將它放在頂部,然後處理實際的排序(帶有文件的文件夾和文件夾)。

var files = [ 
    { 
    name : 'folder2/', 
    filesize: null, 
    filetype: 'folder' 
    }, 
    { 
    name : 'folder1/', 
    filesize: null, 
    filetype: 'folder' 
    }, 
    { 
    name : 'def.jpg', 
    filesize: '1.2', 
    filetype: 'jpg' 
    }, 
    { 
    name : 'abc.pdf', 
    filesize: '3.2', 
    filetype: 'pdf' 
    }, 
    { 
    name : 'ghi.doc', 
    filesize: '1.5', 
    filetype: 'doc' 
    }, 
    { 
    name : 'jkl.doc', 
    filesize: '1.1', 
    filetype: 'doc' 
    }, 
    { 
    name : 'pqr.pdf', 
    filesize: '3.5', 
    filetype: 'pdf' 
    }, 
    { 
    name : 'mno.pdf', 
    filesize: '3.5', 
    filetype: 'pdf' 
    } 
]; 

/** 
* Sort an array of files and always put the folders at the top. 
* @access public 
* @param {Array} array to sort 
* @param {String} column to sort 
* @param {Bool} asc 
* @return void 
*/ 
function sortBy(array, column, asc) { 
    if (asc == null) { 
    asc = true; 
    } 

    array.sort(function(a, b) { 

    // Put at the top the folders. 
    if (a.filetype == 'folder' && b.filetype != 'folder') { 
     return false; 

    // Sort between folders. 
    // The folders don't have a filesize and the type is always the same. 
    // Process as a sort by name. 
    // It doesn't need to respect the sens of sorting. 
    } else if ((column == 'filesize' || column == 'filetype') && a.filetype == 'folder' && b.filetype == 'folder') { 
     return a.name > b.name; 

    // Normal sort. 
    // The folders will be sorted together and the files togethers. 
    } else { 
     return asc ? a[column] > b[column] : a[column] < b[column]; 
    } 

    }); 
} 

sortBy(files, 'name', false); 
console.log('by name', files); 

sortBy(files, 'filesize', true); 
console.log('by size', files); 

sortBy(files, 'filetype', false); 
console.log('by type', files);