Focal Point
Sort by JavaScript?

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/5511058331

April 14, 2005, 09:39 AM
EK
Sort by JavaScript?
Any hints for WebFocus report with JavaSciprt table sort? In other words: Running webfocus once and then (by clicking title-text) sorting a list/table without webfocus? There's a lot of javascipt-function in net(sortable.js etc), but how to adopt them easily as a part of webfocus-reporting? Our reports are in htmltable-format. One problem is the reserved word 'table'.
April 14, 2005, 04:13 PM
HÃ¥kan
Here's one solution:

APP HOLD lidholh
FILEDEF HOLD2 hold2.htm
-* Please note that it should be javascript: and () instead of javascriptx and <>
TABLE FILE CAR
PRINT COUNTRY AS '<A href="javascriptx:sortTable<1>;">Country</A>'
BY CAR AS '<A href="javascriptx:sortTable<0>;">Car</A>'
ON TABLE HOLD FORMAT HTMTABLE
END
-RUN
-READ HOLD, &LINE
-*
-* Replacing the first line of the table with the following line, the name rsTable and the number of cols is needed
-WRITE HOLD2 <TABLE BORDER CELLPADDING=1 name="rsTable" id=rsTable cols=2>
-:read
-READ HOLD, &LINE
-IF &IORETURN NE 0 GOTO :end_read;
-WRITE HOLD2 &LINE
-GOTO :read
-:end_read
-RUN
-*
-:disp
-HTMLFORM BEGIN
your htmlcode here

the script was found at www.javascriptsource.com/forms/sort-data-table.html#source

I had to change the call from sortTable(col, tableToSort) to sortTable(col) since I put the the Javascript call in the column title and Focus converted that to a line break. Then I added the following line:

var tableToSort = rsTable;


end of head
body
!IBI.FIL.HOLD2;
end of body
end of html
-HTMLFORM END
April 18, 2005, 07:15 AM
EK
Thank's for the tip! I still have some problems with the JavaScript, but I'm sure it will work. The idea is clear. Write-command was the answer to my problem.
April 19, 2005, 08:33 PM
GCohen
In WebFOCUS 7, the next release, there will be a built-in control to make any column sortable.
Until then, the advice you are getting will help solve the immediate problem.
April 20, 2005, 01:34 PM
Boogarweed
Another technique that may not be the most desirable in this situation but is useful for getting at elements of the html table that WebFOCUS doesn't expose follows:

It's best if you create a single pixel image (but you can use any image) and include it in the first column of the report as defined below (MY_MODEL). This is so you can leverage the "onload" event of the image but since you don't want the image to show up on the report you give it a zero height and width. If the image cannot be found or for any reason is not loaded, the onload event will not fire. The onload event (called "WalkUp(element)") will then walk up the DOM, in this case, to find the "TR" tag and modify it's onmouseover and onmouseout events. You could have it walk up to the "TABLE" tag if you desire. Copy the code below and run it in your environment. Then mouseover the lines in the report to see the effect. NOTE: If you don't have the "century_corp.gif" file in the directory as shown, change the path to any image that exists in your environment.

DEFINE FILE CAR
MY_MODEL/A110 = '' || MODEL;
END

TABLE FILE CAR
PRINT MY_MODEL AS 'MODEL'
BODYTYPE
DEALER_COST
RETAIL_COST
ON TABLE HOLD AS CARFIL FORMAT HTMTABLE
END

-HTMLFORM BEGIN


<script>
var bgcolor = "";
function ChangeColor()
{
bgcolor = this.style.backgroundColor;
this.style.backgroundColor = "orange";
}

function ChangeBack()
{
this.style.backgroundColor = bgcolor;
}

function WalkUp(element)
{
while (true)
{
if (element.tagName == "TR")
{
break;
}
element = element.parentElement;
}
element.onmouseover = ChangeColor;
element.onmouseout = ChangeBack;
}



!IBI.FIL.CARFIL;



-HTMLFORM END

This message has been edited. Last edited by: <Mabel>,
April 20, 2005, 02:25 PM
EK
Good idea! Not to this particular problem, but to so many others:-)