From c15d348e7f2085467d8796dd507a1bdc8c7c5f08 Mon Sep 17 00:00:00 2001 From: Evangelos Vlachogiannis Date: Sat, 9 Aug 2014 01:43:57 +0200 Subject: [PATCH] sort table --- js/utils.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/js/utils.js b/js/utils.js index d975254..617721a 100644 --- a/js/utils.js +++ b/js/utils.js @@ -24,13 +24,13 @@ Utils.urlParam=function (s){ }; Utils.UIRoleAdapt = function () { var userRoles = accessdb.session.get("userRoles") || []; - $(".accessdbUserMessage").html(""); + $(".accessdbUserMessage").remove(); if (accessdb.session.isUserCollaborator()) { $(".roleExpertsOnly").show(); } else { $(".roleExpertsOnly").hide(); - var msg = $('

').addClass("accessdbUserMessage").append("You need Collaborator Role for this action!"); + var msg = $('

').addClass("accessdbUserMessage").append("You need Collaborator Role for this action!"); $(".roleExpertsOnly").parent().append(msg); } if (accessdb.session.isUserAdmin()) { @@ -38,7 +38,7 @@ Utils.UIRoleAdapt = function () { } else { $(".roleAdminOnly").hide(); - var msg = $('

').addClass("accessdbUserMessage").append("You need Admin Role for this action!"); + var msg = $('

').addClass("accessdbUserMessage").append("You need Admin Role for this action!"); $(".roleAdminOnly").parent().append(msg); } if(accessdb.session.isLoggedIn()){ @@ -158,4 +158,61 @@ Utils.arrayToSqlVal = function (arr) { val = val.substring(1, val.length - 1); val = val.replace(/\"/g, '\''); return val; -}; \ No newline at end of file +}; + +Utils.sortResultsTable = function (table) { + var cols1st = $(table).find("thead th"); + var cols = []; + var first = null; + var count = 1; + $.each(cols1st, function (key, value) { + count++; + if (first != null) { + var col = { + key: $(value).text(), + cell: value, + cells: $(table).find("td:nth-child(" + count + ")").get() + }; + // console.log(col ); + cols.push(col); + } + else { + first = value; + } + }); + cols = _.sortBy(cols, "key"); + cols.unshift({ + key: " ", + cell: first, + cells: $(table).find("th:nth-child(1)").get()} + ); + $(table).empty(); + var noRows = 0; + var currentRow = 0; + var noCols = cols.length; + var thead = $(""); + var trh = $(""); + for (i = 0; i < noCols; i++) { + var col = cols[i]; + $(trh).append(col.cell); + noRows = col.cells.length; + } + $(thead).append(trh); + $(table).append(thead); + var tbody = $(""); + for (k = 0; k < noRows; k++) { + var tr = $(""); + for (i = 0; i < noCols; i++) { + var col = cols[i]; + cells = col.cells; + var cell = col.cells[k]; + if($(cell).text().trim().length<1) { + $(cell).text("-"); + } + $(tr).append(cell); + } + $(tbody).append(tr); + } + $(table).append(tbody); + +}