平常用到的一些js代码


/*
标准程序块
Author:King Astar
Date: 2005-11-10
*/


/******************************************************
Desp: 调用远程数据
Date: 2005-11-9
Author: King astar
Params:改变的本地Div或者其它元素的ID,远程调用的URL,处理函数
如果处理函数为空,则自动将返回的HTML代码设置为元素内部的HTML
******************************************************
*/

function LoadDataInContainer(objeName,url,dealFunc)
{
  
  
   
this.ContentObj = document.getElementById(objeName);
   
if(this.ContentObj==null)
   
return;
   
   ShowProgress(
1);
   
   
   
if(dealFunc!=null)
      
this.DealFunction = dealFunc;
   
else
      
this.DealFunction = null;
   
      
    
if (window.XMLHttpRequest) {
        req 
= new XMLHttpRequest();
        req.onreadystatechange 
= processReqChange;
        req.open(
"GET", url, true);
        req.send(
null);
    
// branch for IE/Windows ActiveX version
    }
 else if (window.ActiveXObject) {
        isIE 
= true;
        req 
= new ActiveXObject("Microsoft.XMLHTTP");
        
if (req) {
            req.onreadystatechange 
= processReqChange;
            req.open(
"GET", url, true);
            req.send();
        }

    }

   
    
}

//数据返回后处理
function processReqChange() {
    
// only if req shows "loaded"
    if (req.readyState == 4{
        
// only if "OK"
        if (req.status == 200
        
{
          
if(this.DealFunction!=null)
            
{
               
try
               
{
                 
this.DealFunction();
                }
catch(e){alert("调用方法失败:"+e.description);}
                 ShowProgress(
2);
                
                
                 
             }

          
else
              
{
                
//alert( req.responseText);
                try
                
{
                    
this.ContentObj.innerHTML = req.responseText;
                }

                
catch(e){alert(e.description);}
                ShowProgress(
2);
                
              }

         }
 else {
            alert(
"There was a problem retrieving the XML data:\n" +
                req.statusText);
         }

    }

}

//显示进程正在进行
function ShowProgress(status)
{

  
var progressDiv = document.getElementById("progressDiv");
  
if(progressDiv==null)
  
{
    progressDiv 
= document.createElement("div");    
    progressDiv.innerHTML 
="数据加载中";
    progressDiv.id 
= "progressDiv";
    progressDiv.name 
= "progressDiv";    
    document.body.appendChild(progressDiv);
   }

   
   
if(status==1)
        progressDiv.style.display 
="";   
   
else
        progressDiv.style.display 
="none";
        
}

       






/******************************************************
Desp: 点击设置表格颜色的程序块
Date: 2005-11-9
Author: King astar
Params:table,新颜色代码,是否包括table的题头也变
*****************************************************
*/


function AttachTableOnchangeEvent(table,NewColor,includingTitle)
{
    
    
this.newColor = NewColor;
    
this.includingtitle = includingTitle;
    table.attachEvent(
"onmousedown",OnClickChangeTdBackColor);
}

function OnClickChangeTdBackColor()
{

    
var srcElement=event.srcElement; 
    
//var Everselected ="whitesmoke";
    var TabElement =event.srcElement;
    
var oldcolor;
  
while(srcElement.tagName.toLowerCase()!="tr"
  

   srcElement
=srcElement.parentElement; 
  }
 
  
  
  
while(TabElement.tagName.toLowerCase()!="table")
  
{
          TabElement 
= TabElement.parentElement;
  }

  
//alert(srcElement.tagName);
  
  
    
for (var i=0;i<TabElement.rows.length;i++)
    
{
          
if(TabElement.rows[i].style.background!= this.newColor )
          
{
              oldcolor
=TabElement.rows[i].style.background;
              
break;
          }

            
    }

      
for(var i=0;i<TabElement.rows.length;i++)
      
{
        
          TabElement.rows[i].style.background
=oldcolor;
      }

  
  
//alert(srcElement.rowIndex==0&&this.includingtitle);
        if(!(srcElement.rowIndex==0&&!this.includingtitle))
          srcElement.style.background
=  this.newColor ;
         
           
      
  
}


/******************************************************
Desp: 取得当前URL地址
Date: 2005-11-11
Author: King astar
Params:其它url参数
Returns:当前的路径+其它url参数
*****************************************************
*/

function getVirtualPath(reUrl)
{
  
    
var ss,sss;
    
try
    
{
    ss 
= window.location;
    }

    
catch(e)
    
{
      alert(e.description);
    }

    ss 
+=" ";

    sss 
= ss.split("/");
    ss 
="";
    
if(sss.length>1)
    
{
        
for(i=0;i<sss.length-1;i++)
        
{
            ss
+=sss[i]+"/";
        }

    }

    
return ss+reUrl;
}






/******************************************************
Desp: An implementation of the debug log. 
Date: 2005-11-15
Author: google inc
*****************************************************
*/

var logging__ = false;
function Log() {};

Log.lines 
= [];

Log.write 
= function(s) {
  
if (logging__) {
    
this.lines.push(xmlEscapeText(s));
    
this.show();
  }

}
;

// Writes the given XML with every tag on a new line.
Log.writeXML = function(xml) {
  
if (logging__) {
    
var s0 = xml.replace(/</g, '\n<');
    
var s1 = xmlEscapeText(s0);
    
var s2 = s1.replace(/\s*\n(\s|\n)*/g, '<br/>');
    
this.lines.push(s2);
    
this.show();
  }

}


// Writes without any escaping
Log.writeRaw = function(s) {
  
if (logging__) {
    
this.lines.push(s);
    
this.show();
  }

}


Log.clear 
= function() {
  
if (logging__) {
    
var l = this.div();
    l.innerHTML 
= '';
    
this.lines = [];
  }

}


Log.show 
= function() {
  
var l = this.div();
  l.innerHTML 
+= this.lines.join('<br/>') + '<br/>';
  
this.lines = [];
  l.scrollTop 
= l.scrollHeight;
}


Log.div 
= function() {
  
var l = document.getElementById('log');
  
if (!l) {
    l 
= document.createElement('div');
    l.id 
= 'log';
    l.style.position 
= 'absolute';
    l.style.right 
= '10px';
    l.style.top 
= '20px';
    l.style.width 
= '250px';
    l.style.height 
= '150px';
    l.style.overflow 
= 'auto';
    l.style.backgroundColor 
= '#f0f0f0';
    l.style.border 
= '1px solid gray';
    l.style.fontSize 
= '10px';
    l.style.padding 
= '5px';
    document.body.appendChild(l);
  }

  
return l;
}


    /* 
Desp:产生下拉列过滤选择项;Attach the filter to a table. 
Date: 2005-11-9
Author: ?
Params:
filterRow specifies the rownumber at which the filter should be inserted.
Modify:
主要比较的是内容,所有把innerHTML换成了innerText
另外就是在特定的列创建
2.0
*/


function attachFilter2(table, filterRow)
{
     
    table.filterRow 
= filterRow;
    
    
// Check if the table has any rows. If not, do nothing
    if(table.rows.length > 0)
    
{
        
// Insert the filterrow and add cells whith drowdowns.
        var filterRow = table.insertRow(table.filterRow);
        
            table.fillFilters 
= fillFilters;
        table.inFilter 
= inFilter;
        table.buildFilter 
= buildFilter;
        table.showAll 
= showAll;
        table.detachFilter 
= detachFilter;
        table.filterElements 
= new Array();
        table.filterElements[
0]=2;
        
var colLen = table.rows[0].cells.length;
        alert(colLen);
        
        
        
for(var i = 0; i < table.rows[table.filterRow + 1].cells.length; i++)
        
{
            
var c = document.createElement("TH");
            table.rows[table.filterRow].appendChild(c);
            
var opt = document.createElement("select");
            opt.onchange 
= filter;

            
if(!table.inFilter( colLen-i))
            
{

                c.appendChild(opt);
            }

            
else
            
{
                c.innerText
="";              
            }

        }

        
// Set the functions
    
        
        
        
        
// Fill the filters
        table.fillFilters();
        table.filterEnabled 
= true;
    }

}


function detachFilter()
{
    
if(this.filterEnabled)
    
{
        
// Remove the filter
        this.showAll();
        
this.deleteRow(this.filterRow);
        
this.filterEnabled = false;
    }

}


// Checks if a column is filtered
function inFilter(col)
{
    
for(var i = 0; i < this.filterElements.length; i++)
    
{
        
//if(this.filterElements[i].index == col)
        if(this.filterElements[i] == col)
            
return true;
    }

    
return false;
}


// Fills the filters for columns which are not fiiltered
function fillFilters()
{
   
        
for(var col = 0; col < this.rows[this.filterRow].cells.length; col++)
        
{
            
if(!this.inFilter(col))
            
{
                
this.buildFilter(col, "(all)");
            }

        }

    
    
    
}


// Fills the columns dropdown box. 
//
 setValue is the value which the dropdownbox should have one filled. 
//
 If the value is not suplied, the first item is selected
function buildFilter(col, setValue)
{

    
// Get a reference to the dropdownbox.
    var opt = this.rows[this.filterRow].cells[col].firstChild;
    
    
// remove all existing items
    while(opt.length > 0)
        opt.remove(
0);
    
    
var values = new Array();
        
    
// put all relevant strings in the values array.
    for(var i = this.filterRow + 1; i < this.rows.length; i++)
    
{
        
var row = this.rows[i];
        
if(row.style.display != "none" && row.className != "noFilter")
        
{
            values.push(row.cells[col].innerText.toLowerCase());
        }

    }

    values.sort();
    
    
//add each unique string to the dopdownbox
    var value;
    
for(var i = 0; i < values.length; i++)
    
{
        
if(values[i].toLowerCase() != value)
        
{
            value 
= values[i].toLowerCase();
            opt.options.add(
new Option(values[i], value));
        }

    }


    opt.options.add(
new Option("(all)""(all)"), 0);

    
if(setValue != undefined)
        opt.value 
= setValue;
    
else
        opt.options[
0].selected = true;
}


// This function is called when a dropdown box changes
function filter()
{
    
var table = this// 'this' is a reference to the dropdownbox which changed
    while(table.tagName.toUpperCase() != "TABLE")
        table 
= table.parentNode;

    
var filterIndex = this.parentNode.cellIndex; // The column number of the column which should be filtered
    var filterText = table.rows[table.filterRow].cells[filterIndex].firstChild.value;
    
    
// First check if the column is allready in the filter.
    var bFound = false;
    
    
for(var i = 0; i < table.filterElements.length; i++)
    
{
        
if(table.filterElements[i].index == filterIndex)
        
{
            bFound 
= true;
            
// If the new value is '(all') this column is removed from the filter.
            if(filterText == "(all)")
            
{
                table.filterElements.splice(i, 
1);
            }

            
else
            
{
                table.filterElements[i].filter 
= filterText;
            }

            
break;
        }

    }

    
if(!bFound)
    
{
        
// the column is added to the filter
        var obj = new Object();
        obj.filter 
= filterText;
        obj.index 
= filterIndex;
        table.filterElements.push(obj);
    }

    
    
// first set all rows to be displayed
    table.showAll();
    
    
// the filter ou the right rows.
    for(var i = 0; i < table.filterElements.length; i++)
    
{
        
// First fill the dropdown box for this column
        table.buildFilter(table.filterElements[i].index, table.filterElements[i].filter);
        
// Apply the filter
        for(var j = table.filterRow + 1; j < table.rows.length; j++)
        
{
            
var row = table.rows[j];
            
            
if(table.style.display != "none" && row.className != "noFilter")
            
{
                
if(table.filterElements[i].filter != row.cells[table.filterElements[i].index].innerText.toLowerCase())
                
{
                    row.style.display 
= "none";
                }

            }

        }

    }

    
// Fill the dropdownboxes for the remaining columns.
    table.fillFilters();
}


function showAll()
{
    
for(var i = this.filterRow + 1; i < this.rows.length; i++)
    
{
        
this.rows[i].style.display = "";
    }

}

posted on 2005-11-16 14:19  一望无际的南  阅读(588)  评论(2)    收藏  举报

导航