Resizable, sortable, findable and scrollable HTML table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • since
    New Member
    • May 2007
    • 14

    #1

    Resizable, sortable, findable and scrollable HTML table

    I figured I would post my solution to the following.

    Resizable column tables.
    Search and replace values in a table. (IE only)
    Scrollable tables.
    Sortable tables.

    It is based on a lot examples I found on the web. Works in IE and mozilla.





    Todo.
    Resize table.
    Search and replace values in Mozilla.

    Any suggestions or examples on the todo list would be greatly appreciated.

    find.html
    --------------------------------------
    [CODE=html]
    <HTML>
    <HEAD>
    <TITLE>Find/Replace</TITLE>
    <script type="text/javascript">
    <!--[/code]
    [code=javascript]var inputTags = null;
    var found = false;
    var currentInputIdx = 0;
    var currentTextRang e = null;
    var currentSearchVa l = null;

    function initFind(){
    inputTags = new Array();
    var tags = opener.document .getElementsByT agName("input") ;
    if(tags != null){
    for(i = 0; i < tags.length;++i ){
    if(tags[i].type == "text"){
    inputTags[inputTags.lengt h] = tags[i];
    }
    }
    }

    tags = opener.document .getElementsByT agName("textare a");
    if(tags != null){
    for(i = 0; i < tags.length;++i ){
    inputTags[inputTags.lengt h] = tags[i];
    }
    }
    }

    // returns a calculated value for matching case and
    // matching whole words
    function searchType(){
    var retval = 0;
    var matchcase = 0;
    var matchword = 0;
    if (document.forms[0]['blnMatchCase'].checked) matchcase = 4;
    if (document.forms[0]['blnMatchWord'].checked) matchword = 2;
    retval = matchcase + matchword;
    return(retval);
    }

    function replaceText(){

    // if no current tag then find one
    if(currentTextR ange == null) {
    findText();
    }
    // if we have one then replace
    else if(currentTextR ange != null){
    var beforeBookmark = currentTextRang e.getBookmark() ;
    var replaceStr = document.forms[0]['replaceStr'].value;
    currentTextRang e.text=replaceS tr;
    pushUndoNew(cur rentTextRange, beforeBookmark, currentSearchSt r, replaceStr);
    currentTextRang e.collapse(fals e);
    findText();
    }
    }

    function replaceAllText( ){
    found = false;
    if(currentTextR ange == null) findText();

    while(currentTe xtRange != null){
    replaceText();
    }
    }

    function findText(){
    if(inputTags == null) initFind();

    if (document.forms[0]['searchStr'].value.length < 1) {
    alert("Please enter text in the \"Find what:\" field.");
    return;
    }

    if(inputTags.le ngth == 0){
    alert("No input field(s) found on page");
    return;
    }

    var searchVal = currentSearchSt r = document.forms[0]['searchStr'].value;

    var useRegex = document.forms[0]['blnRegex'].checked;

    if( currentTextRang e == null) {
    currentTextRang e = inputTags[currentInputIdx].createTextRang e();
    if(useRegex) currentText = currentTextRang e.text;
    }
    else {
    currentTextRang e.collapse(fals e);
    // get the remaining search range for regex
    if(useRegex){
    var rng = currentTextRang e.duplicate();
    rng.collapse(fa lse);
    rng.moveEnd("te xtedit");
    currentText = rng.text;
    }

    }

    var match = true;

    if(useRegex){
    try{
    var matches = currentText.mat ch(new RegExp(searchVa l));
    if(matches){
    currentSearchSt r = matches[0];
    }
    else {
    match = false;
    }
    }
    catch(e){
    alert("Illegal Regex: '" + searchVal + "'");
    return;
    }
    }

    var type = searchType();

    // found a match within the current text field
    if( match && currentTextRang e.findText(curr entSearchStr, 10000, type)){
    currentTextRang e.select();
    inputTags[currentInputIdx].scrollIntoView (false);
    currentTextRang e.scrollIntoVie w();
    found = true;
    }
    // no match found and end of the document
    else if( currentInputIdx >= inputTags.lengt h-1 ){
    currentTextRang e = null;
    currentInputIdx = 0;
    if(found) alert("Done!");
    else alert("Can not find '" +searchVal +"'");
    found = false;
    }
    // no match found in the current text, look at the next text field
    else{
    currentTextRang e = null;
    ++currentInputI dx;
    findText();
    }
    }

    // BEGIN UNDO BUFFER CODE
    // buffer global variables
    var undoStack = new Array();

    // UndoState Object
    function UndoState(textR ange,searchStr, inputIdx,before Bookmark, afterBookmark){
    this.textRange = textRange;
    this.searchStr = searchStr;
    this.inputIdx = inputIdx;
    /* two bookmarks are necessary to get back to the before and after state */
    this.afterBookm ark = afterBookmark;
    this.beforeBook mark = beforeBookmark;
    }

    // store original search string and bookmarks of each replaced range
    function pushUndoNew(rng , beforeBookmark, searchStr, replaceStr) {
    currentTextRang e.moveStart("ch aracter", -replaceStr.leng th);
    undoStack[undoStack.lengt h] = new UndoState( currentTextRang e,searchStr,cur rentInputIdx,be foreBookmark,cu rrentTextRange. getBookmark() );
    }

    // perform the undo
    function undoReplace() {
    if (undoStack.leng th) {
    var newLength = undoStack.lengt h-1;
    currentTextRang e = undoStack[newLength].textRange;
    currentTextRang e.moveToBookmar k( undoStack[newLength].afterBookmark );

    currentTextRang e.text = undoStack[newLength].searchStr;
    currentTextRang e.moveToBookmar k( undoStack[newLength].beforeBookmark );

    // reselect the undo
    currentTextRang e.findText(undo Stack[newLength].searchStr, 10000);
    currentTextRang e.select();
    currentTextRang e.scrollIntoVie w();

    currentInputIdx = undoStack[newLength].inputIdx;
    undoStack.lengt h=newLength;
    }
    }

    //-->[/code]
    [code=html]</script>
    </HEAD>
    <BODY bgcolor="#EAF4F 3">
    <FORM NAME="frmSearch " method="post" action="">
    <table CELLSPACING="0" cellpadding="5" border="0">
    <tr><td VALIGN="top" align="left" nowrap
    style="font-family:Arial; font-size:11px;">
    <label for="searchStr" accesskey="n">< u>F</u>ind what:</label><br>
    <INPUT TYPE="TEXT" SIZE=40 NAME="searchStr "
    id="searchStr" style="width:28 0px;"><br>
    <label for="replaceStr " accesskey="n">< u>R</u>eplace with:</label><br>
    <INPUT TYPE="TEXT" SIZE=40 NAME="replaceSt r"
    id="replaceStr " style="width:28 0px;"><br>

    <INPUT TYPE="Checkbox" NAME="blnMatchC ase" id="blnMatchCas e">
    <label for="blnMatchCa se">Match case</label><br>

    <INPUT TYPE="Checkbox" NAME="blnMatchW ord" ID="blnMatchWor d">
    <label for="blnMatchWo rd">Match whole word only</label>

    <INPUT TYPE="Checkbox" NAME="blnRegex" ID="blnRegex">
    <label for="blnRegex"> Use regular expression</label>

    </td>
    <td rowspan="2" valign="top">
    <button name="btnFind" accesskey="f" onClick="findTe xt();"
    style="width:75 px; height:22px; font-family:Tahoma;
    font-size:11px; margin-top:15px"><u>F</u>ind Next</button><br>

    <button name="btnReplac e" accesskey="r" onClick="replac eText();"
    style="width:75 px; height:22px; font-family:Tahoma;
    font-size:11px; margin-top:15px"><u>R</u>eplace</button>&nbsp;

    <button name="btnReplac eAll" accesskey="a" onClick="replac eAllText();"
    style="width:75 px; height:22px; font-family:Tahoma;
    font-size:11px; margin-top:15px">Repla ce <u>A</u>ll</button><br>

    <button name="btnUndo" onClick="undoRe place();"
    style="width:75 px; height:22px; font-family:Tahoma;
    font-size:11px; margin-top:15px">Undo</button><br>

    <button name="btnCancel " onClick="window .close();"
    style="width:75 px; height:22px; font-family:Tahoma;
    font-size:11px; margin-top:7px">Close</button><br>
    </td></tr>
    </table>
    </FORM>
    </BODY>
    </HTML>
    [/CODE]
    main page.
    ------------------------------------
    [CODE=html]
    <HTML>
    <HEAD>
    <style>[/code][code=css]
    h1, h2{
    font-family:Verdana, Arial, Helvetica, sans-serif;
    font-size:13px;

    }

    </style>
    <!--[if IE]>
    <style type="text/css">
    div.scrollable {
    /* shrink the window */
    height:expressi on( this.getElement sByTagName('TAB LE')[0].clientHeight >= parseInt(this.g etElementsByTag Name('TBODY')[0].style.height) && parseInt(this.g etElementsByTag Name('TBODY')[0].style.height) > 0 ? this.getElement sByTagName('TBO DY')[0].style.height : "auto" );
    overflow-x: visible;
    overflow-y: auto;
    width: expression(this .getElementsByT agName('TABLE')[0].offsetWidth);
    }

    /* set the table row */
    div.scrollable table thead tr {
    position: relative;
    /* this fixes IE header jumping bug, adjust for the table border */
    top: expression( (this.parentNod e.parentNode.pa rentNode.scroll Top - 2)+ 'px' );
    }

    /* needed for IE if tbody.height is set */
    div.scrollable tr{
    height:auto;
    }

    </style>
    <![endif]-->

    <style type="text/css">
    /* if mozilla */
    html>body div.scrollable tbody {
    overflow: auto;
    }

    table.resizable th{
    text-align:center;
    overflow: hidden;
    }

    /* if mozilla, add 10 for the scrollbar */
    html>body th.scrollbarCol {
    width:10px;
    }

    table.resizable td{
    overflow: hidden;
    }

    table.resizable {
    table-layout:fixed;
    }

    table.resizable input{
    width:100%;
    }

    table.resizable textarea{
    width:100%;
    }

    .nowrap {
    white-space:nowrap;
    }

    /* needed for IE */
    table.tabular th.scrollbarCol {
    background-color:transpare nt;
    }

    table.tabular {
    FONT-SIZE: 13px;
    FONT-FAMILY: 'Verdana, Arial, Helvetica, sans-serif';
    COLOR: #336699;
    }

    table.tabular thead {
    COLOR: #ffffff;
    FONT-WEIGHT: bold;
    }

    table.tabular th{
    background-color:#c0c0c0;
    padding: 4px;
    }

    table.tabular td {
    background-color:#EAF4F3;
    padding: 2px;
    }[/code]

    [code=html] </style>

    <script type="text/javascript" src="http://www.kryogenix.o rg/code/browser/sorttable/sorttable.js" >
    </script>

    <SCRIPT type="text/javascript">[/code]
    [code=javascript]var ob;
    var over = false;
    var iEdgeThreshold = 10;

    function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetPare nt) {
    curleft = obj.offsetLeft;
    curtop = obj.offsetTop;
    while (obj = obj.offsetParen t) {
    curleft += obj.offsetLeft;
    curtop += obj.offsetTop;
    }
    }
    return [curleft,curtop];
    }

    /* Function that tells me if on the border or not */
    function isOnBorderRight (objTable,objTh ,event){
    var width = objTh.offsetWid th;
    var left = objTh.offsetLef t;
    var pos = findPos(objTabl e);
    var absRight = pos[0] + left + width;

    if( event.clientX > (absRight - iEdgeThreshold) ){
    return true;
    }

    return false;
    }

    function getNodeName(obj Reference,nodeN ame){
    var oElement = objReference;
    while (oElement != null && oElement.tagNam e != null && oElement.tagNam e != "BODY") {
    if (oElement.tagNa me.toUpperCase( ) == nodeName) {
    return oElement;
    }
    oElement = oElement.parent Node;
    }
    return null;
    }

    function doResize(objTh, event){
    if(!event) event = window.event;
    var objTable = getNodeName(obj Th,"TABLE");
    if( isOnBorderRight (objTable,objTh ,event)){
    over=true;
    objTh.style.cur sor="e-resize";
    }
    else{
    over=false;
    objTh.style.cur sor="";
    }
    return over;
    }

    function doneResizing(){
    over=false;
    }

    function MD(event) {
    if(!event) event = window.event;

    MOUSTSTART_X=ev ent.clientX;
    MOUSTSTART_Y=ev ent.clientY;

    if (over){
    if(event.srcEle ment)ob = event.srcElemen t;
    else if(event.target )ob = event.target;
    else return;

    ob = getNodeName(ob, "TH");
    if(ob == null) return;
    ob2 = getNodeName(ob, "TABLE");
    obwidth=parseIn t(ob.style.widt h);
    obwidth2=parseI nt(ob2.offsetWi dth);
    }
    }

    function MM(event) {
    if(!event) event = window.event;

    if (ob) {
    st=event.client X-MOUSTSTART_X+ob width;
    st2=event.clien tX-MOUSTSTART_X+ob width2;

    if(st>=10){
    ob.style.width= st;
    ob2.style.width =st2;
    }
    if(document.sel ection) document.select ion.empty();
    else if(window.getSe lection)window. getSelection(). removeAllRanges ();
    }
    }

    function MU(event) {
    if(!event) event = window.event;
    if(ob){
    if(document.sel ection) document.select ion.empty();
    else if(window.getSe lection)window. getSelection(). removeAllRanges ();
    ob = null;
    }
    }

    document.onmous edown = MD;
    document.onmous emove = MM;
    document.onmous eup = MU;[/code]
    [code=html]</script>


    <SCRIPT type="text/javascript">
    <!--
    function createFindWindo w(){
    // FIXME: we need to do a mozilla implementation
    //mozilla
    if(window.find) {
    window.find();
    }
    // IE
    else {
    window.open( "find.html" , "test","status= yes,width=500,h eight=250" );
    }
    }
    //-->
    </script>


    </HEAD>

    <BODY>
    <!--
    YOU MUST HAVE TO DEFINE WIDTH ON STYLE TAB.. if you leave NULL
    IT RETURNS .. ERROR..
    -->
    <H1>RESIZABLE TABLE COLUMN</H1>
    <form>
    <a href="javascrip t:createFindWin dow()">Find</a>

    <div class="scrollab le" >
    <TABLE id="mytable" style="width:51 0px" class="sortable resizable tabular">

    <THEAD>

    <TR>

    <TH onmousemove="do Resize(this,eve nt)" onmouseover="do Resize(this,eve nt)" onmouseout='don eResizing()' style='width:60 px'>Index</TH>

    <TH onmousemove="do Resize(this,eve nt)" onmouseover="do Resize(this,eve nt)" onmouseout='don eResizing()' style='width:17 0px'><span class="nowrap"> Parameter Name</span></TH>

    <TH onmousemove="do Resize(this,eve nt)" onmouseover="do Resize(this,eve nt)" onmouseout='don eResizing()' style='width:17 0px'><span class="nowrap"> Parameter Value</span></TH>

    <TH onmousemove="do Resize(this,eve nt)" onmouseover="do Resize(this,eve nt)" onmouseout='don eResizing()' style='width:11 0px'><span class="nowrap"> Page Name</span></TH>

    <TH class="sorttabl e_nosort scrollbarCol"></TH>

    </TR>
    </THEAD>
    <TBODY style="height:2 00px">
    <TR>

    <TD>0</TD>

    <TD>1_2</TD>

    <TD><input type="text" value="this is a very long text string, this is a very long text string"></TD>

    <TD>1_3</TD>

    </TR>

    <TR>

    <TD>0</TD>

    <TD>1_2</TD>

    <TD><input type="text" value="this is a very long text string, this is a very long text string"></TD>

    <TD>1_3</TD>

    </TR>

    <TR>

    <TD>0</TD>

    <TD>1_2</TD>

    <TD><input type="text" value="this is a very long text string, this is a very long text string"></TD>

    <TD>1_3</TD>

    </TR>
    <TR>

    <TD>0</TD>

    <TD>1_2</TD>

    <TD><input type="text" value="this is a very long text string, this is a very long text string"></TD>

    <TD>1_3</TD>

    </TR>
    <TR>

    <TD>0</TD>

    <TD>1_2</TD>

    <TD><input type="text" value="this is a very long text string, this is a very long text string"></TD>

    <TD>1_3</TD>

    </TR>
    <TR>

    <TD>0</TD>

    <TD>1_2</TD>

    <TD><input type="text" value="this is a very long text string, this is a very long text string"></TD>

    <TD>1_3</TD>

    </TR>
    <TR>

    <TD>0</TD>

    <TD>1_2</TD>

    <TD><input type="text" value="this is a very long text string, this is a very long text string"></TD>

    <TD>1_3</TD>

    </TR>
    <TR>

    <TD>0</TD>

    <TD>1_2</TD>

    <TD><input type="text" value="this is a very long text string, this is a very long text string"></TD>

    <TD>1_3</TD>

    </TR>

    <TR>

    <TD>1</TD>

    <TD><span class="nowrap"> this is a very long paramter name</span></TD>

    <TD>2_2</TD>


    <TD>2_3</TD>


    </TR>


    <TR>

    <TD>3_0</TD>
    <TD>3_2</TD>

    <TD><textarea >
    this is a test of
    this is a test of
    </textarea></TD>

    <TD>3_3</TD>


    </TR>


    <TR>

    <TD>4_0</TD>


    <TD>4_1</TD>


    <TD>4_2</TD>

    <TD>4_3</TD>

    </TR>

    </TBODY>
    </table>[/code]
    [code=javascript]<!-- needed for mozilla to shrink the window -->
    <script type="text/javascript">
    <!--
    if(window.navig ator && window.navigato r.userAgent.ind exOf("ecko") != -1 ){
    var tbody=document. getElementById( 'mytable').getE lementsByTagNam e('tbody')[0];
    if(tbody.scroll Height<=parseIn t(tbody.style.h eight)) tbody.style.hei ght="auto";
    }
    //-->[/code]
    [code=html]</script>
    </div>
    </form>
    </BODY>
    </HTML>

    [/CODE]
    Last edited by acoder; Nov 17 '07, 11:32 AM. Reason: Added code tags
  • ayilas
    New Member
    • Dec 2009
    • 5

    #2
    Please help me out. Your script is not working in Mozilla but good in IE.The problem is with DOCTYPE in mozilla.If i remove the doctype from the page it is working but making my page layout weird.But no problem with IE8 even if i keep DOCTYPE.
    Last edited by ayilas; Dec 16 '09, 06:16 AM. Reason: to detail the problem.

    Comment

    Working...