adding sorting by name

This commit is contained in:
Mohammad Naghavi 2016-02-11 17:37:40 +01:00
parent e61c1805cd
commit 89cab0efb3
3 changed files with 76 additions and 30 deletions

View file

@ -7,6 +7,17 @@ a {
text-decoration: none; text-decoration: none;
} }
.list-sort {
background: #ddd;
border-radius: 1rem;
padding: .5rem 2rem;
}
.list-sort label {
display: inline-block;
margin: 0 1rem 0 0;
}
.file-list { .file-list {
list-style: none; list-style: none;
} }

View file

@ -16,6 +16,19 @@
<div class="container"> <div class="container">
<h1>Files</h1> <h1>Files</h1>
<div class="list-sort">
<h5>sort list</h5>
<label>
by date
<input type="radio" name="sort" value="date" checked>
</label>
<label>
by name
<input type="radio" name="sort" value="name">
</label>
</div>
<ul id="file-list" class="file-list"> <ul id="file-list" class="file-list">
<li> <li>
<a href="#" class="file-name file-link"></a> <a href="#" class="file-name file-link"></a>

View file

@ -69,21 +69,24 @@ $(document).ready(function () {
return parentDir; return parentDir;
} }
function navigateTo(path) { function renderFileList(filesData, path) {
console.log("navigateTo", path);
$.ajax({ var sortBy = $('input[name=sort]:checked').val();
url: filesBaseUrl + path, if (sortBy === "date") {
success: function (filesData) { console.log("sort by date");
filesData.map(function (fileData) {
return fileData.mtime = new Date(fileData.mtime);
});
filesData.sort(function (fileA, fileB) { filesData.sort(function (fileA, fileB) {
return fileB.mtime.getTime() - fileA.mtime.getTime(); return fileB.mtime.getTime() - fileA.mtime.getTime();
}); });
} else if (sortBy === "name") {
console.log("sort by name");
filesData.sort(function (fileA, fileB) {
return fileA.name.toLowerCase().localeCompare(fileB.name.toLowerCase());
});
}
fileListElement.empty(); fileListElement.empty();
var parentDir = getParentDir(path); var parentDir = getParentDir(path);
@ -105,7 +108,26 @@ $(document).ready(function () {
fileData.mtime fileData.mtime
)); ));
}); });
}
function navigateTo(path) {
console.log("navigateTo", path);
$.ajax({
url: filesBaseUrl + path,
success: function (filesData) {
filesData.map(function (fileData) {
return fileData.mtime = new Date(fileData.mtime);
});
renderFileList(filesData, path);
$('input[name=sort]')
.unbind("change")
.on("change", function () {
renderFileList(filesData, path);
});
} }
}); });
} }