你說什麼關於那個? 排序腳本檢查對象是否是文件夾,將它放在頂部,然後處理實際的排序(帶有文件的文件夾和文件夾)。
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);
也許我沒有正確理解它,但我沒有看到如何禁用一列,將實現上面解釋的情況。你能澄清一下嗎? – dsol828 2013-03-08 13:03:01
這個插件有這麼多選項,你甚至可以添加你的代碼,並使其工作,如你所願。如果你想要簡短的東西,同時你不想要短的東西,你可以使用jquery,這個插件只是做它做的。 – 2013-03-08 13:11:06