remove the date-sorting in JS, use a raw-data attribute if available for sorting and sort numbers and issues numerically

git-svn-id: https://dev.finn.de/svn/cockpit/branches/reporting_merge@1917 7926756e-e54e-46e6-9721-ed318f58905e
pull/6827/head
t.felgentreff 14 years ago
parent c70048c356
commit 41a4766fda
  1. 4
      app/views/cost_reports/_cost_entry_table.rhtml
  2. 4
      app/views/cost_reports/_simple_cost_report_table.rhtml
  3. 133
      assets/javascripts/sortable.js

@ -24,8 +24,8 @@
<% @query.each_direct_result do |result| %>
<tr class='<%= cycle("odd", "even") %>'>
<% list.each do |field| %><td><%= show_field field, result.fields[field.to_s] %></td><% end %>
<td><%= show_result(result, result.fields['cost_type_id'].to_i) %></td>
<td class="right"><%= show_result(result, 0) %></td>
<td raw-data=<%= result.units %>><%= show_result(result, result.fields['cost_type_id'].to_i) %></td>
<td class="right currency" raw-data=<%= result.real_costs %>><%= show_result(result, 0) %></td>
<td style="width: 40px">
<%= link_to_if_authorized image_tag('edit.png'),
action_for(result, :action => 'edit'), :title => l(:button_edit) %>

@ -31,9 +31,9 @@ show_units = list.include? "cost_type_id"
<td><%= show_row result %></td>
<td><%= result.count %></td>
<% if show_units %>
<td><%= show_result result, result.fields[:cost_type_id].to_i %></td>
<td raw-data=<%= result.units %>><%= show_result result, result.fields[:cost_type_id].to_i %></td>
<% end %>
<td><%= show_result result %></td>
<td raw-data=<%= result.real_costs %>><%= show_result result %></td>
</tr>
<% if params[:debug] %>
<tr>

@ -97,15 +97,18 @@ function ts_resortTable(lnk, clid) {
for (var ci=0;ci<lnk.childNodes.length;ci++) {
if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
}
var spantext = ts_getInnerText(span);
var td = lnk.parentNode;
var column = clid || td.cellIndex;
var t = getParent(td,'TABLE');
// Work out a type for the column
// Do not sort single a row
if (t.rows.length <= 1) return;
var itm = "";
// Determine if all rows are equal
var first = ts_getInnerText(t.tBodies[0].rows[0].cells[column]);
var itm = first;
var i = 0;
while (itm == "" && i < t.tBodies[0].rows.length) {
while (itm == first && i < t.tBodies[0].rows.length) {
var itm = ts_getInnerText(t.tBodies[0].rows[i].cells[column]);
itm = trim(itm);
if (itm.substr(0,4) == "<!--" || itm.length == 0) {
@ -113,12 +116,20 @@ function ts_resortTable(lnk, clid) {
}
i++;
}
if (itm == "") return;
if (itm == first) return;
// Determine the sort type. You can set numeric=true on the header to force numeric sorting
sortfn = ts_sort_caseinsensitive;
if (itm.match(/^\d\d[\/\.-][a-zA-z][a-zA-Z][a-zA-Z][\/\.-]\d\d\d\d$/)) sortfn = ts_sort_date;
if (itm.match(/^\d\d[\/\.-]\d\d[\/\.-]\d\d\d{2}?$/)) sortfn = ts_sort_date;
if (itm.match(/^-?[<EFBFBD>$<EFBFBD>ۢ<EFBFBD>]\d/)) sortfn = ts_sort_numeric;
if (itm.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?%?$/)) sortfn = ts_sort_numeric;
if (thead) {
if (itm.match(/^-?\d+\.?d*/)) sortfn = ts_sort_numeric; // Normal number
if (itm.match(/#\d+:/)) sortfn = ts_sort_numeric; // Issue number
var numeric_flag = t.tHead.rows[0].cells[column].getAttribute("numeric")
if (numeric_flag == "true") {
sortfn = ts_sort_numeric;
}
}
// Do the sorting
SORT_COLUMN_INDEX = column;
var firstRow = new Array();
var newRows = new Array();
@ -184,74 +195,23 @@ function getParent(el, pTagName) {
}
}
function sort_date(date) {
// y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
dt = "00000000";
if (date.length == 11) {
mtstr = date.substr(3,3);
mtstr = mtstr.toLowerCase();
switch(mtstr) {
case "jan": var mt = "01"; break;
case "feb": var mt = "02"; break;
case "mar": var mt = "03"; break;
case "apr": var mt = "04"; break;
case "may": var mt = "05"; break;
case "jun": var mt = "06"; break;
case "jul": var mt = "07"; break;
case "aug": var mt = "08"; break;
case "sep": var mt = "09"; break;
case "oct": var mt = "10"; break;
case "nov": var mt = "11"; break;
case "dec": var mt = "12"; break;
// default: var mt = "00";
}
dt = date.substr(7,4)+mt+date.substr(0,2);
return dt;
} else if (date.length == 10) {
if (europeandate == false) {
dt = date.substr(6,4)+date.substr(0,2)+date.substr(3,2);
return dt;
} else {
dt = date.substr(6,4)+date.substr(3,2)+date.substr(0,2);
return dt;
}
} else if (date.length == 8) {
yr = date.substr(6,2);
if (parseInt(yr) < 50) {
yr = '20'+yr;
} else {
yr = '19'+yr;
}
if (europeandate == true) {
dt = yr+date.substr(3,2)+date.substr(0,2);
return dt;
} else {
dt = yr+date.substr(0,2)+date.substr(3,2);
return dt;
}
}
return dt;
}
function ts_sort_date(a,b) {
dt1 = sort_date(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));
dt2 = sort_date(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]));
if (dt1==dt2) {
return 0;
function ts_get_cell_data(a, b) {
acell = a.cells[SORT_COLUMN_INDEX]
bcell = b.cells[SORT_COLUMN_INDEX]
if ((aa = acell.getAttribute("raw-data")) == null) {
aa = ts_getInnerText(acell).toLowerCase();
}
if (dt1<dt2) {
return -1;
if ((bb = bcell.getAttribute("raw-data")) == null) {
bb = ts_getInnerText(bcell).toLowerCase();
}
return 1;
return [aa, bb]
}
function ts_sort_numeric(a,b) {
var aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
aa = clean_num(aa);
var bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
bb = clean_num(bb);
return compare_numeric(aa,bb);
var cells = ts_get_cell_data(a, b);
return compare_numeric(cells[0], cells[1]);
}
function compare_numeric(a,b) {
var a = parseFloat(a);
a = (isNaN(a) ? 0 : a);
@ -259,34 +219,20 @@ function compare_numeric(a,b) {
b = (isNaN(b) ? 0 : b);
return a - b;
}
function ts_sort_caseinsensitive(a,b) {
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
if (aa==bb) {
return 0;
}
if (aa<bb) {
return -1;
}
return 1;
}
function ts_sort_default(a,b) {
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
if (aa==bb) {
return 0;
}
if (aa<bb) {
return -1;
}
var cells = ts_get_cell_data(a, b);
if (cells[0] == cells[1]) return 0;
if (cells[0] < cells[1]) return -1;
return 1;
}
function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+, NS6 and Mozilla
// By Scott Andrew
{
if (elm.addEventListener){
if (elm.addEventListener) {
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent){
@ -297,7 +243,8 @@ function addEvent(elm, evType, fn, useCapture)
}
}
function clean_num(str) {
str = str.replace(new RegExp(/[^-?0-9.]/g),"");
str = str.replace(/^[^-?\d]+/, "");
str.replace(/(\d),(\d)/g, "$1$2")
return str;
}
function trim(s) {

Loading…
Cancel
Save