Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
THRUST SERVICES & TOOLS
CLOUD EDITIONS
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
sort table data with java script
vsub
Hi,
I have requirement to sort birt table data on click of columns. I found one customized java script for table and able to sort data properly. but when i add totals at the footer columns, its sorting the footer values also along with data rows. Is there any way to stop sorting the footer rows. attahced customized script used.
Thanks in advance
ub
Find more posts tagged with
Comments
thuston
There is nothing attached.
Why not just put the user in Interactive Viewer? Then they can sort on any column as needed.
mwilliams
Was the script like the script in the onPrepare of the table in the example in this forum post?
http://www.birt-exchange.org/org/forum/index.php/topic/17847-sorting-in-birt/
It uses a parameter, a drill through, and script to sort the table.
vsub
Hi,<br />
<br />
below sort java script, not able to attach the file. this customized java script which will work all tables without much change just you need provide bookmark name in table properties which should be similar to getElementById in script . but somehow its sorting the footer section also. <br />
<br />
Below is the script<br />
=====================================================<br />
<script type="text/javascript"><br />
var stIsIE = /*
@cc_on!
;
@*/false
;<br />
var thisTable = document.getElementById('table1');<br />
makeSortableWithAltColors(thisTable,"rgb(218,230,255)","rgb(255, 255, 255)");<br />
<br />
//The following function is to client-side-sort the table with alternating row colors<br />
//The aternating colors are the second and third parameters<br />
<br />
<br />
<br />
function init() {<br />
<br />
// quit if this function has already been called<br />
if (arguments.callee.done) return;<br />
// flag this function so we don't do the same thing twice<br />
arguments.callee.done = true;<br />
// kill the timer<br />
if (_timer) clearInterval(_timer);<br />
<br />
if (!document.createElement || !document.getElementsByTagName) return;<br />
<br />
sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;<br />
<br />
forEach(document.getElementsByTagName('table'), function(table) {<br />
if (table.className.search(/\bsortable\b/) != -1) {<br />
sorttable.makeSortable(table);<br />
}<br />
});<br />
<br />
}<br />
<br />
//The following function is to client-side-sort the table with alternating row colors<br />
//The aternating colors are the second and third parameters<br />
<br />
<br />
function makeSortableWithAltColors(table,evenColor,oddColor) {<br />
//init();<br />
if (table.getElementsByTagName('thead').length == 0) {<br />
the = document.createElement('thead');<br />
the.appendChild(table.rows[0]);<br />
table.insertBefore(the,table.firstChild);<br />
}<br />
if (table.tHead == null) table.tHead = table.getElementsByTagName('thead')[0];<br />
<br />
if (table.tHead.rows.length != 1) return; // can't cope with two header rows<br />
<br />
sortbottomrows = [];<br />
for (var i=0; i<table.rows.length; i++) {<br />
if (table.rows
.className.search(/\bsortbottom\b/) != -1) {<br />
sortbottomrows[sortbottomrows.length] = table.rows
;<br />
}<br />
}<br />
if (sortbottomrows) {<br />
if (table.tFoot == null) {<br />
tfo = document.createElement('tfoot');<br />
table.appendChild(tfo);<br />
}<br />
for (var i=0; i<sortbottomrows.length; i++) {<br />
tfo.appendChild(sortbottomrows
);<br />
}<br />
delete sortbottomrows;<br />
}<br />
<br />
headrow = table.tHead.rows[0].cells;<br />
for (var i=0; i<headrow.length; i++) {<br />
if (!headrow
.className.match(/\bsorttable_nosort\b/)) { // skip this col<br />
mtch = headrow
.className.match(/\bsorttable_([a-z0-9]+)\b/);<br />
if (mtch) { override = mtch[1]; }<br />
if (mtch && typeof sorttable["sort_"+override] == 'function') {<br />
headrow
.sorttable_sortfunction = sorttable["sort_"+override];<br />
if (override == "alpha") {<br />
<br />
headrow
.sorttable_sortfunction = sort_alpha;<br />
}<br />
} else {<br />
headrow
.sorttable_sortfunction = guessType(table,i);<br />
}<br />
headrow
.sorttable_columnindex = i;<br />
headrow
.sorttable_tbody = table.tBodies[0];<br />
dean_addEvent(headrow
,"click", function(e) {<br />
<br />
if (this.className.search(/\bsorttable_sorted\b/) != -1) {<br />
sorttable.reverse(this.sorttable_tbody);<br />
this.className = this.className.replace('sorttable_sorted',<br />
'sorttable_sorted_reverse');<br />
this.removeChild(document.getElementById('sorttable_sortfwdind'));<br />
sortrevind = document.createElement('span');<br />
sortrevind.id = "sorttable_sortrevind";<br />
sortrevind.innerHTML = ' ↑';<br />
this.appendChild(sortrevind);<br />
return;<br />
}<br />
if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) {<br />
sorttable.reverse(this.sorttable_tbody);<br />
this.className = this.className.replace('sorttable_sorted_reverse',<br />
'sorttable_sorted');<br />
this.removeChild(document.getElementById('sorttable_sortrevind'));<br />
sortfwdind = document.createElement('span');<br />
sortfwdind.id = "sorttable_sortfwdind";<br />
sortfwdind.innerHTML = ' ↓';<br />
this.appendChild(sortfwdind);<br />
return;<br />
}<br />
<br />
theadrow = this.parentNode;<br />
forEach(theadrow.childNodes, function(cell) {<br />
if (cell.nodeType == 1) { // an element<br />
cell.className = cell.className.replace('sorttable_sorted_reverse','');<br />
cell.className = cell.className.replace('sorttable_sorted','');<br />
}<br />
});<br />
sortfwdind = document.getElementById('sorttable_sortfwdind');<br />
if (sortfwdind) { sortfwdind.parentNode.removeChild(sortfwdind); }<br />
sortrevind = document.getElementById('sorttable_sortrevind');<br />
if (sortrevind) { sortrevind.parentNode.removeChild(sortrevind); }<br />
<br />
this.className += ' sorttable_sorted';<br />
sortfwdind = document.createElement('span');<br />
sortfwdind.id = "sorttable_sortfwdind";<br />
sortfwdind.innerHTML = ' ↓';<br />
this.appendChild(sortfwdind);<br />
row_array = [];<br />
col = this.sorttable_columnindex;<br />
rows = this.sorttable_tbody.rows;<br />
for (var j=0; j<rows.length; j++) {<br />
row_array[row_array.length] = [sorttable.getInnerText(rows[j].cells[col]), rows[j]];<br />
}<br />
row_array.sort(this.sorttable_sortfunction);<br />
<br />
tb = this.sorttable_tbody;<br />
for (var j=0; j<row_array.length; j++) {<br />
if (j%2 == 0) {<br />
row_array[j][1].style.background = evenColor;<br />
} else {<br />
row_array[j][1].style.background = oddColor; <br />
}<br />
tb.appendChild(row_array[j][1]);<br />
}<br />
delete row_array;<br />
});<br />
}<br />
}<br />
}<br />
<br />
function guessType(table, column) {<br />
// guess the type of a column based on its first non-blank row<br />
sortfn = sort_alpha;<br />
for (var i=0; i<table.tBodies[0].rows.length; i++) {<br />
text = sorttable.getInnerText(table.tBodies[0].rows
.cells[column]);<br />
if (text != '') {<br />
if (text.match(/^-?[?$?]?[\d,.]+%?$/)) {<br />
return sorttable.sort_numeric;<br />
}<br />
// check for a date: dd/mm/yyyy or dd/mm/yy <br />
// can have / or . or - as separator<br />
// can be mm/dd as well<br />
possdate = text.match(sorttable.DATE_RE)<br />
if (possdate) {<br />
// looks like a date<br />
firstDatePart = parseInt(possdate[1]);<br />
secondDatePart = parseInt(possdate[2]);<br />
if (firstDatePart > 12) {<br />
// definitely dd/mm<br />
return sorttable.sort_ddmm;<br />
} else if (secondDatePart > 12) {<br />
return sorttable.sort_mmdd;<br />
} else {<br />
// looks like a date, but we can't tell which, so assume<br />
// that it's dd/mm (English imperialism!) and keep looking<br />
sortfn = sorttable.sort_ddmm;<br />
}<br />
}<br />
}<br />
}<br />
return sortfn;<br />
}<br />
<br />
function getInnerText(node) {<br />
// gets the text we want to use for sorting for a cell.<br />
// strips leading and trailing whitespace.<br />
// this is *not* a generic getInnerText function; it's special to sorttable.<br />
// for example, you can override the cell text with a customkey attribute.<br />
// it also gets .value for <input> fields.<br />
<br />
hasInputs = (typeof node.getElementsByTagName == 'function') &&<br />
node.getElementsByTagName('input').length;<br />
<br />
if (node.getAttribute("sorttable_customkey") != null) {<br />
return node.getAttribute("sorttable_customkey");<br />
}<br />
else if (typeof node.textContent != 'undefined' && !hasInputs) {<br />
return node.textContent.replace(/^\s+|\s+$/g, '');<br />
}<br />
else if (typeof node.innerText != 'undefined' && !hasInputs) {<br />
return node.innerText.replace(/^\s+|\s+$/g, '');<br />
}<br />
else if (typeof node.text != 'undefined' && !hasInputs) {<br />
return node.text.replace(/^\s+|\s+$/g, '');<br />
}<br />
else {<br />
switch (node.nodeType) {<br />
case 3:<br />
if (node.nodeName.toLowerCase() == 'input') {<br />
return node.value.replace(/^\s+|\s+$/g, '');<br />
}<br />
case 4:<br />
return node.nodeValue.replace(/^\s+|\s+$/g, '');<br />
break;<br />
case 1:<br />
case 11:<br />
var innerText = '';<br />
for (var i = 0; i < node.childNodes.length; i++) {<br />
innerText += sorttable.getInnerText(node.childNodes
);<br />
}<br />
return innerText.replace(/^\s+|\s+$/g, '');<br />
break;<br />
default:<br />
return '';<br />
}<br />
}<br />
}<br />
<br />
function reverse(tbody) {<br />
// reverse the rows in a tbody<br />
newrows = [];<br />
for (var i=0; i<tbody.rows.length; i++) {<br />
newrows[newrows.length] = tbody.rows
;<br />
}<br />
for (var i=newrows.length-1; i>=0; i--) {<br />
tbody.appendChild(newrows
);<br />
}<br />
delete newrows;<br />
}<br />
<br />
/* sort functions<br />
each sort function takes two parameters, a and b<br />
you are comparing a[0] and b[0] */<br />
function sort_numeric(a,B) {<br />
aa = parseFloat(a[0].replace(/[^0-9.-]/g,''));<br />
if (isNaN(aa)) aa = 0;<br />
bb = parseFloat(b[0].replace(/[^0-9.-]/g,'')); <br />
if (isNaN(bb)) bb = 0;<br />
return aa-bb;<br />
}<br />
<br />
function sort_alpha(a,B) {<br />
if (a[0].toLowerCase()==b[0].toLowerCase()) return 0;<br />
if (a[0].toLowerCase()<b[0].toLowerCase()) return -1;<br />
return 1;<br />
}<br />
<br />
function sort_ddmm(a,B) {<br />
mtch = a[0].match(sorttable.DATE_RE);<br />
y = mtch[3]; m = mtch[2]; d = mtch[1];<br />
if (m.length == 1) m = '0'+m;<br />
if (d.length == 1) d = '0'+d;<br />
dt1 = y+m+d;<br />
mtch = b[0].match(sorttable.DATE_RE);<br />
y = mtch[3]; m = mtch[2]; d = mtch[1];<br />
if (m.length == 1) m = '0'+m;<br />
if (d.length == 1) d = '0'+d;<br />
dt2 = y+m+d;<br />
if (dt1==dt2) return 0;<br />
if (dt1<dt2) return -1;<br />
return 1;<br />
}<br />
<br />
function sort_mmdd(a,B) {<br />
mtch = a[0].match(sorttable.DATE_RE);<br />
y = mtch[3]; d = mtch[2]; m = mtch[1];<br />
if (m.length == 1) m = '0'+m;<br />
if (d.length == 1) d = '0'+d;<br />
dt1 = y+m+d;<br />
mtch = b[0].match(sorttable.DATE_RE);<br />
y = mtch[3]; d = mtch[2]; m = mtch[1];<br />
if (m.length == 1) m = '0'+m;<br />
if (d.length == 1) d = '0'+d;<br />
dt2 = y+m+d;<br />
if (dt1==dt2) return 0;<br />
if (dt1<dt2) return -1;<br />
return 1;<br />
}<br />
<br />
function shaker_sort(list, comp_func) {<br />
// A stable sort function to allow multi-level sorting of data<br />
// see: <a class='bbc_url' href='
http://en.wikipedia.org/wiki/Cocktail_sort'>http://en.wikipedia.org/wiki/Cocktail_sort</a><br
/>
// thanks to Joseph Nahmias<br />
var b = 0;<br />
var t = list.length - 1;<br />
var swap = true;<br />
<br />
while(swap) {<br />
swap = false;<br />
for(var i = b; i < t; ++i) {<br />
if ( comp_func(list
, list[i+1]) > 0 ) {<br />
var q = list
; list
= list[i+1]; list[i+1] = q;<br />
swap = true;<br />
}<br />
} // for<br />
t--;<br />
<br />
if (!swap) break;<br />
<br />
for(var i = t; i > b; --i) {<br />
if ( comp_func(list
, list[i-1]) < 0 ) {<br />
var q = list
; list
= list[i-1]; list[i-1] = q;<br />
swap = true;<br />
}<br />
} // for<br />
b++;<br />
<br />
} // while(swap)<br />
} <br />
<br />
<br />
</script><br />
<br />
========================================================================<br />
<br />
<br />
Thanks in advance<br />
ub
mwilliams
Can you recreate this with a simple report that I can run from the sample database? That way I can play around with the script to see if I can find the issue? Sorting within BIRT using drill throughs isn't probably what you're wanting to do, but you could avoid this issue by doing it that way.
vsub
<blockquote class='ipsBlockquote' data-author="'mwilliams'" data-cid="69226" data-time="1286809178" data-date="11 October 2010 - 07:59 AM"><p>
Can you recreate this with a simple report that I can run from the sample database? That way I can play around with the script to see if I can find the issue? Sorting within BIRT using drill throughs isn't probably what you're wanting to do, but you could avoid this issue by doing it that way.<br /></p></blockquote>
vsub
Hi,
i have created a sample report which will use javascript for sorting.the sorting option will work at the server level only, birt viewer it won't work. I am not able to upload .rptdesign file in birt exchange forum. i have copied the xml code wordpad and attached, please copy the same to notepad and save as Sort_sample.rptdesign. regarding usage of drill through option for sorting , we are using this in some of the reports. We have dashboard which will have around 8-10 sections need to provide sorting, due to this we are using javascript.
thanks in advance
vsub
mwilliams
So, do I need to deploy the birt engine and run this through a browser for it to work as you're saying? Nothing happens when I run it from the designer as HTML output.
johnw
Take a look at this example. Its not using a DOM resort of the table, but it re-runs the report with a specifier to tell BIRT where to apply the sort. This will take care of your issues with your counts, and is easier to maintain.
http://digiassn.blogspot.com/2007/05/birt-strong-suit-is-in-realm-of-online.html
John
vsub
Thanks all for your help.<br />
<br />
Regards<br />
vsub<br />
<br />
<blockquote class='ipsBlockquote' data-author="'johnw'" data-cid="69280" data-time="1286935888" data-date="12 October 2010 - 07:11 PM"><p>
Take a look at this example. Its not using a DOM resort of the table, but it re-runs the report with a specifier to tell BIRT where to apply the sort. This will take care of your issues with your counts, and is easier to maintain.<br />
<br />
<a class='bbc_url' href='
http://digiassn.blogspot.com/2007/05/birt-strong-suit-is-in-realm-of-online.html'>http://digiassn.blogspot.com/2007/05/birt-strong-suit-is-in-realm-of-online.html</a><br
/>
<br />
John<br /></p></blockquote>