HTC代码

本文介绍了一个复杂的表格控件的实现细节,包括鼠标交互、排序、拖拽列等功能。通过JavaScript实现了丰富的用户交互体验,如行高亮、选择行、列排序及列的拖放等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<HTC URN="GridView">

<attach event="onmouseover" onevent="elementOnMouseOver()"/>
<attach event="onmouseout"  onevent="elementOnMouseOut()"/>
<attach event="onmousedown" onevent="elementOnMouseDown()"/>
<attach event="onmousemove" onevent="elementOnMouseMove()"/>
<attach event="onclick" onevent="elementOnMouseClick()"/>
<attach event="oncontentready"   onevent="initElement()"></ATTACH>

<attach method name="QuerySelected"></attach>
<attach method name="addSelectRow"></attach>
<attach method name="setRowStyle"></attach>
<attach method name="initsetRowColors"></attach>
<attach method name="hiliteHeader"></attach>
<attach method name="hitTest"></attach>
<attach method name="moveHeaderCols"></attach>
<attach method name="moveBodyCols"></attach>
<attach method name="SetOldRowStyle"></attach>

 

<script language="javascript">
                                         //标题行用的是css样式表  
var RowStyleColor="white";               //设置行交替颜色(偶数行)
var AlternatingRowStyleColor="yellow";  //设置行交替颜色(奇数行)oldlace
var checkBoxCol=3;                       //设置checkbox所在的列(可能在拖动列的时候改变)
var sortNoneImageUrl="sortBlank.gif";    //设置图片路径
var sortUpImageUrl="sortUp.gif";
var sortDownImageUrl="sortDown.gif";
var posUpImageUrl="upArrow.gif";
var posDownImageUrl="downArrow.gif";
var currentSort="DOWN";         //从小到大;

var headColumns;                //标题行的列集合
var currentRowIndex;            //当前行指针
var currentRow;                 //当前行
var selectRow = new Array();    //选择的行
var selectRowIndex=new Array(); //选择的行指针
var oldRow;
var length;                     //行的长度

var bDragCol;
var bDragChangeWidth;           //是否处于拖动改变列的宽度状态状态
var bDragMode;                  //是否处于拖动状态

var objDragline;                //改变列宽的虚线
var objDragItem;               
var objDragToItem1;
var objDragToItem2;

var dragHeadHitIndex=-1;        //最终拖动移动到列
var dragHeadIndex=-1;           //获得拖动在那列
var lastHeadIndex=-2;

var posOffSet=0;
var iDragCounter;              
var offset_x, offset_y;         //element绝对位置

var allowColMove=true;          //允许整列移动
var sortAllow=true;             //允许排序
var allowColChangeWidth=true;   //允许改变列宽

var ie6;
 

 
/*
 * 初始化表格
 ************/
function initElement()

  //theadRow=element.children(0).rows(0); //得到标题
  //alert(theadRow.cells(0).innerText);   //得到标题的列
  //ie6 = (instr(window.navigator.appVersion, "MSIE 6.") <> 0);
  length=element.children(0).rows.length;
  headColumns=element.children(0).rows(0).cells;
  //alert(headColumns[1].offsetLeft)
  initSetRowColors();
  initHeadColumns();
  setTableBorder();
  initAdditionalElements();
  window.document.attachEvent("onmouseup",elementOnMouseUp);
 
}

/*
 * 初始化标题列
 **************/
function initHeadColumns()
{
   var telement;
   var imgElement;
   var headRow=element.children(0).rows(0);
   for(var i=0;i<headRow.cells.length;i++)  
   {
       telement=headRow.cells(i);
       with(telement)
       {   
           if(children.length==0) //还没有孩子
           {  
              imgElement = document.createElement("IMG");
              with(imgElement)
              {     
          src = sortNoneImageUrl;
          id = "srtImg";
          width = 9;
          height = 9;
              }
              insertAdjacentElement("beforeEnd", imgElement);
             
           }
           else
              children(0).style.cursor = "default";
          attachEvent("onmousedown",elementHeadOnMouseDown);
          if(telement.innerText=="ID")                        //为第一列添加排序功能  
             attachEvent("ondblclick",elementHeadOnDbClick);
       }
   }

}

/*
 * 添加拖动时的虚拟div
 **********************/
function initAdditionalElements()
{  
    objDragline=document.createElement("DIV");    //虚线
    with(objDragline.style)
    {
       position = "absolute";
    backgroundColor="#000000";
    width=1;
    visibility = "hidden";
    zIndex = 3001;
    }
    window.document.body.insertAdjacentElement("afterBegin",objDragline);
    objDragItem = document.createElement("DIV");  //虚拟列
 with(objDragItem.style)
 {  
     font = "menu";
  backgroundColor = "buttonshadow";
  cursor = "default";
  position = "absolute";
  filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=45)";
  zIndex = 3001;
  visibility = "hidden";
 }
 window.document.body.insertAdjacentElement("afterBegin", objDragItem);
 objDragToItem1 = document.createElement("DIV");
 with(objDragToItem1)
 {
  style.height = 9;
  style.width  = 9;
  innerHTML = "<img src='"+posDownImageUrl+"'>";
  style.backgroundColor = "transparent";
  style.position = "absolute";
  style.zIndex = 3000;
  style.visibility = "hidden";
 }
 window.document.body.insertAdjacentElement("afterBegin", objDragToItem1);
 objDragToItem2 = document.createElement("DIV")
 with(objDragToItem2)
 {
  style.height = 9;
  style.width  = 9;
  innerHTML = "<img src='" + posUpImageUrl + "'>"
  style.backgroundColor = "transparent";
  style.position = "absolute";
  style.zIndex = 3000;
  style.visibility = "hidden";
 }
 window.document.body.insertAdjacentElement("afterBegin", objDragToItem2);
}

/*
 * 表格边框样式
 **************/
function setTableBorder()
{
 with(element)
 {
  border = "1px";
  rules = "rows";
  borderColor = "window";
  //alert(style.borderStyle);
  switch(style.borderStyle)
  {
   case "NotSet":
      style.border = "";
      posOffSet = 4;
      break;
   case "none":
      style.border = "1 solid black";
            posOffSet = 5;
            break;
   case "Dotted":
     style.borderLeft = "1 buttonshadow solid";
     style.borderTop = "1 buttonshadow solid";
     style.borderBottom = "1 buttonhighlight solid";
     style.borderRight = "1 buttonhighlight solid";
     posOffSet = 5;
     break;
   case "Dashed":
     style.border = "2 inset window";
     posOffSet = 6;
     break;
  }
 }
 //alert(posOffSet);
}

/*
 * 取得控件的绝对位置
 *********************/
function initOffsets() 
{   
    var el;
    offset_x = 0;
    offset_y = 0;
    el = element;
 while (el!=null)
 {
  offset_y = offset_y + el.offsetTop - el.scrollTop;
  offset_x = offset_x + el.offsetLeft - el.scrollLeft;
  el = el.offsetParent;
    }
    el=null;
    //window.status=offset_x+"   "+offset_y;
}


function getTop(e)
{
 var t=e.offsetTop;
 while(e=e.offsetParent)
 {
  t+=e.offsetTop-e.scrollTop;
 }
 return t;
}
  
function getLeft(e)
{
 var l=e.offsetLeft;
 while(e=e.offsetParent)
 {
  l+=e.offsetLeft-e.scrollLeft;
 }
 return l;
}

/*
 * 鼠标点击标题列按下事件(绑定在标题列)
 ****************************************/
function elementHeadOnMouseDown()                          //先触发down,后触发click
{
    if(!allowColMove&&!allowColChangeWidth) return false;  //不允许列移动和改变列宽
    var el;
 el = window.event.srcElement;
 while (el.tagName!="TH")
           el = el.parentElement;
    if(el.tagName!="TH") return false;
    if(allowColChangeWidth)          //如果允许改变列宽 
    {   
     if( event.offsetX<=2||(el.offsetWidth-event.offsetX)>0 &&(el.offsetWidth-event.offsetX)<=2)
     {  
         bDragCol=el;
         bDragChangeWidth=true;
         with(objDragline.style)
         {
            left = event.clientX;
            top = getTop(element);
            height = element.offsetWidth;
            }
            for(var i=0;i<headColumns.length;i++)
            {
               headColumns[i].style.cursor='col-resize';
            }
            objDragline.style.visibility = "visible";
            window.document.attachEvent("onmousemove",elementOnMouseMove); //为了能在整个文档中拖动
         return false;
     }
 }
 //-------------------------------------------------------------------
    if(allowColMove)             //允许拖动列
    {
        bDragMode = true;             //拖动开始
     iDragCounter = 0;             //拖动的距离
     initOffsets();                //取得绝对坐标 
     dragHeadIndex=el.cellIndex;
     element.setCapture();        //在整个文档外也可以运行一次
     element.style.cursor = "default";
     with(objDragItem)
     {
      innerHTML = "<center>" + el.innerHTML + "</center>"
      style.color = el.currentStyle.color
      style.height = el.offsetHeight - 2
      style.width  = el.offsetWidth - 2
        }
    }
    el = null;
}

/*
 * 拖动列移动事件
 ****************/
function elementOnMouseMove()
{
  
   if(!allowColMove&&!allowColChangeWidth) return false;  //不允许列移动和改变列宽
   if(bDragMode)               //处于拖动列状态时
   {
       var cliX, cliY
       if(iDragCounter < 10)        //当拖动的距离大于10时,才算真正拖动
       {  
            iDragCounter = iDragCounter + 1
      return false;
       }
       if (objDragItem!=null) 
     { 
         with(window.event)
      {
       cliX = clientX;           /*
                                  * 设置或获取鼠标指针位置相对于窗口客户区域的 x 坐标
                                  * 其中客户区域不包括窗口自身的控件和滚动条
                                  *
                                  ***/ 
       cliY = clientY;
       cancelBubble = false;     /*
                                  * cancelBubble   设置或获取当前事件是否要在事件句柄中向上冒泡
                                  * 设置cancelBubble=true后,就不会向上冒泡了
                                  *
                                  ***/
       
       returnValue = false;      /*
                                  *  returnValue   设置或获取事件的返回值。  
                                           *  通常所用的return false;是指函数的返回值为false
                                           *  而returnValue是指事件的返回值是false
                                     *
                                     ***/
      }
      with(objDragItem)
      {
       if (style.visibility = "hidden")
                style.visibility = "visible";
           style.posLeft = cliX - (offsetWidth / 2);  //以鼠标为中心显现
           style.posTop  = cliY - (offsetHeight - 3);
      }
      dragHeadHitIndex = hitTest(cliX, cliY);
     }
   }
   else
   {   
        var el;
        el = window.event.srcElement; 
        if(!bDragChangeWidth)
        {     
             
              if(el.tagName!="TH") return false;
              if( event.offsetX<=2||(el.offsetWidth-event.offsetX)>0 &&(el.offsetWidth-event.offsetX)<=2)                                                             
              { 
                   el.style.cursor='col-resize';
                   window.status="event.offsetX :"+event.offsetX+" el.width"+el.offsetWidth;
              }
              else
              {
                   el.style.cursor='default';
               
              } 
         }
         else
         {  
          
            //el.style.cursor='col-resize';
         with(objDragline.style)
         {
            left = event.clientX;
            top = getTop(element);
            height = height = element.offsetWidth;
            }
      
         }
    }
}

/*
 * 确定插入点的图片显示和位置
 ****************************/
function hiliteHeader(headIndex)
{  
 var o1Style, o2Style;
 if (headIndex == lastHeadIndex) return false;
 o1Style = objDragToItem1.style;
 o2Style = objDragToItem2.style;
 if(headIndex==-1)
 {
  if(o1Style.visibility!="hidden")  o1Style.visibility = "hidden";
  if(o2Style.visibility!="hidden")  o2Style.visibility = "hidden";
  //----------------------------if ie6 then------------------------------------
  if(element.style.cursor!="no-drop")
       element.style.cursor= "no-drop";
  lastHeadIndex = -1;
  o1Style = null;
  o2Style = null;
  return false ;
 }
 window.status=headIndex+"-(0)-"+lastHeadIndex;
 if(element.style.cursor!="default") element.style.cursor = "default";
 if(headIndex > headColumns.length-2)
 { 
     o1Style.posTop =  offset_y - o1Style.posHeight;
  o1Style.posLeft = headColumns[headIndex-1].offsetLeft + headColumns[headIndex-1].offsetWidth - posOffSet + offset_x;
  //window.status=headIndex+"-(1)-"+lastHeadIndex+"   "+o1Style.posLeft+"  "+o1Style.posTop ;
 }
 else
 {
  o1Style.posTop =  offset_y - o1Style.posHeight;
  o1Style.posLeft = headColumns[headIndex].offsetLeft - posOffSet + offset_x;
  //window.status=headIndex+"-(2)-"+lastHeadIndex+"   "+o1Style.posLeft+"  "+o1Style.posTop;
 }
 o2Style.posTop = headColumns[0].offsetHeight + offset_y;
 o2Style.posLeft = o1Style.posLeft;
 if(o1Style.visibility!="visible") o1Style.visibility = "visible";
 if(o2Style.visibility!="visible") o2Style.visibility = "visible";
 lastHeadIndex = headIndex;
 o1Style = null;
 o2Style = null;
}

/*
 * 判断插入列
 ************/
function hitTest(x, y)

    var i, iHit;
 if(y - offset_y > (headColumns[0].offsetTop + ((headColumns[0].offsetHeight - 3) * 2)) || y - offset_y < headColumns[0].offsetTop - 3)
 {
  hiliteHeader(-1); //插入的位置
  return -1;
 }
 for(var i = 0;i< headColumns.length ; i++ )
 {
  if( x - offset_x > headColumns[i].offsetLeft && x - offset_x < headColumns[i].offsetLeft + headColumns[i].offsetWidth )
  {
   if( x - offset_x <= headColumns[i].offsetLeft + (headColumns[i].offsetWidth / 2) )
   { 
       hiliteHeader(i);
    if ( dragHeadIndex > i)
     iHit = i;
    else if ( dragHeadIndex == i )
     iHit = i;
    else if ( dragHeadIndex < i )
     iHit = i - 1;
   } 
   else if (x - offset_x >= headColumns[i].offsetLeft + (headColumns[i].offsetWidth / 2))
   { 
       hiliteHeader( i + 1);
    if (dragHeadIndex > i)
     iHit = i + 1;
    else if ( dragHeadIndex == i )
     iHit = i;
    else if ( dragHeadIndex < i )
     iHit = i;
         }
   if ( iHit < 0 ) iHit = 0;
   if ( iHit > headColumns.length-1 ) iHit = headColumns[i].length-1;
   return  iHit;
  }
 }
 return  -1;
}

function changeColWidth()
{
  
   var otdLeft = getLeft(bDragCol);
   var otdwidth =objDragline.style.pixelLeft - otdLeft;
   if(otdwidth < 0) return false;
   bDragCol.style.width = otdwidth;
}


/*
 * 鼠标按纽弹起事件(document)
 ******************************/
function elementOnMouseUp()
{
   
    if(!allowColMove&&!allowColChangeWidth) return false;  //不允许列移动和改变列宽
    if(bDragChangeWidth)
    { 
       changeColWidth();
       bDragChangeWidth=false;
       objDragline.style.visibility = "hidden";
       window.document.detachEvent("onmousemove",elementOnMouseMove); //取消事件绑定
     
    }
    else if(bDragMode)
    {
        bDragMode = false;
        objDragItem.style.visibility = "hidden";
        objDragToItem1.style.visibility = "hidden";
     objDragToItem2.style.visibility = "hidden";
     element.releaseCapture();
     element.style.cursor = "default";
     if(dragHeadHitIndex !=-1)
        moveCols(dragHeadIndex, dragHeadHitIndex);
     lastHeadIndex = -1;
     dragHeadHitIndex = -1;
 }
}

/*
 * 移动列
 *********/
function moveCols(fcol,tcol)
{
   if(fcol==tcol) return false;
   moveHeaderCols(fcol,tcol);   //移动标题列
   moveBodyCols(fcol,tcol);     //移动其他列
 
}

/*
 *  移动标题列
 *************/
function moveHeaderCols(fCol,tCol)
{
   var saveHTML, saveWidth;
   var nCsol,flag;
   var check=true;           //checkbox  fCol,tCol 之间 check=true;
  
   if (fCol > tCol)
   {   
        flag=-1;            // 0123  3---->0  3012     
        nCol = fCol - tCol;
      
   }
   else                     // 0123  0---->3  1230
   {
        flag=1;
        nCol = tCol - fCol;
   }
  
   if(fCol==checkBoxCol)
   {
          checkBoxCol=tCol;
          check=false;
   }
  
   saveHTML = headColumns[fCol].innerHTML;
   saveWidth = headColumns[fCol].width;
   for(var i = 0;i<nCol;i++)
   {   
        var temp=fCol + flag;
        if(checkBoxCol==temp&&check)
            checkBoxCol=fCol;
        headColumns[fCol].innerHTML = headColumns[fCol + flag].innerHTML;
  headColumns[fCol].width = headColumns[fCol + flag].width;
  fCol = fCol + flag;
   }
   headColumns[tCol].innerHTML = saveHTML;
   headColumns[tCol].width = saveWidth;
   //window.status=checkBoxCol;
}

/*
 *  移动其他列
 *************/
function moveBodyCols(fcol,tcol)

   var fCol,tCol;
   var nCsol,flag;
   var saveHTML;
   var tempRow;
   if (fcol > tcol)
   {   
        flag=-1;            // 0123  3---->0  3012     
        nCol = fcol - tcol;
   }
   else                     // 0123  0---->3  1230
   {
        flag=1;
        nCol = tcol - fcol;
   }
  
   for(var iRow=1;iRow<length;iRow++)  //除标题行
   {   
       fCol=fcol;
       tCol=tcol;
       tempRow=element.children(0).rows(iRow);
       saveHTML = tempRow.cells(fCol).innerHTML;
       for(var i = 0;i<nCol;i++)
       {
         tempRow.cells(fCol).innerHTML =tempRow.cells(fCol + flag).innerHTML;
   fCol = fCol + flag;
       }
       tempRow.cells(tCol).innerHTML = saveHTML;
   }
   tempRow=null;
}

/****************************************************
 *                 标题列双击事件                   *
 ****************************************************/
 
function elementHeadOnDbClick()
{   
    //window.status="ondbclick";  
    //if(!sortAllow) return false;
    //if(bDragMode) return false;
    var el;
    var cellIndex;
 el = window.event.srcElement;
 while (el.tagName!="TH")
           el = el.parentElement;
    if(el.tagName!="TH") return false;
 if(el.children(0).id!="srtImg") return false;
 cellIndex=el.cellIndex;
 if(currentSort=="DOWN")
 {
  currentSort="UP";
  el.children(0).src = sortUpImageUrl;
 }
 else
 {
  currentSort="DOWN";
  el.children(0).src = sortDownImageUrl;
    }
    el = null;
    sortTable(cellIndex);
 }

/********************************************
 *               比较大小                   *
 ********************************************/
//function Compare(RowCurrent,RowTemp,strRowCurrent,strRowTemp,rowindex,tempindex)
//{
//   if(strRowCurrent>strRowTemp)            //从小到大排列
//               {
//                   element.children(0).insertBefore(RowTemp,RowCurrent);  //交换行
//                   var current=getSelectRow(rowindex);
//                   var temp=getSelectRow(tempindex);
//                   if(current!=-1)                    //当前行选中
//                   {
//                      selectRowIndex[current]=tempindex;
//                      SortChangeColor(selectRow[current],tempindex);
//                   }
//                   else
//                   {
//                      SortChangeColor(RowCurrent,tempindex);
//                   }
//                   if(temp!=-1)                       //当前行选中
//                   {
//                      selectRowIndex[temp]=rowindex;
//                      SortChangeColor(selectRow[temp],selectRowIndex[temp]);
//                   }
//                   else
//                   {
//                      SortChangeColor(RowTemp,rowindex);
//                   }
//                   rowindex=tempindex;
//               }
//               else
//               {
//                  RowCurrent=RowTemp;
//                  strRowCurrent=strRowTemp;
//                  rowindex=tempindex;
//               }
//}

/********************************************
 *               排序                       *
 ********************************************/
 function sortTable(cellIndex)
 { 
    var RowCurrent,RowTemp,RowInsert;
    var strRowCurrent,strRowTemp;
    var rowindex;
    if(currentSort=="DOWN")
    { 
        for(var flag=1;flag<length;flag++)
        {
            RowCurrent=element.children(0).rows(1);
            strRowCurrent=parseInt(RowCurrent.cells(cellIndex).innerText);
            rowindex=1;
            for(var j=2;j<=length-flag;j++)
            {
               RowTemp=element.children(0).rows(j);
               strRowTemp=parseInt(RowTemp.cells(cellIndex).innerText);
               //Compare(RowCurrent,RowTemp,strRowCurrent,strRowTemp,rowindex,j);
               if(strRowCurrent>strRowTemp)            //从小到大排列
               {
                   element.children(0).insertBefore(RowTemp,RowCurrent);  //交换行
                   var current=getSelectRow(rowindex);
                   var temp=getSelectRow(j);
                   if(current!=-1)                    //当前行选中
                   {
                      selectRowIndex[current]=j;
                      SortChangeColor(selectRow[current],j);
                   }
                   else
                   {
                      SortChangeColor(RowCurrent,j);
                   }
                   if(temp!=-1)                       //当前行选中
                   {
                      selectRowIndex[temp]=rowindex;
                      SortChangeColor(selectRow[temp],selectRowIndex[temp]);
                   }
                   else
                   {
                      SortChangeColor(RowTemp,rowindex);
                   }
                   rowindex=j;
               }
               else
               {
                  RowCurrent=RowTemp;
                  strRowCurrent=strRowTemp;
                  rowindex=j;
               }
             }
         }
     }
     else if(currentSort=="UP")
     { 
        for(var flag=length-1;flag>=1;flag--)
        {
            RowCurrent=element.children(0).rows(length-1);
            strRowCurrent=parseInt(RowCurrent.cells(cellIndex).innerText);
            rowindex=length-1;
            for(var j=length-2;j>=1;j--)
            {    
                 RowTemp=element.children(0).rows(j);
                 strRowTemp=parseInt(RowTemp.cells(cellIndex).innerText);
                 //Compare(RowCurrent,RowTemp,strRowCurrent,strRowTemp,rowindex,j);
                 if(strRowCurrent>strRowTemp)            //从大到小排列
                 {
                   element.children(0).insertBefore(RowCurrent,RowTemp);  //交换行
                   var current=getSelectRow(rowindex);
                   var temp=getSelectRow(j);
                   if(current!=-1)                    //当前行选中
                   {
                      selectRowIndex[current]=j;
                      SortChangeColor(selectRow[current],j);
                   }
                   else
                   {
                      SortChangeColor(RowCurrent,j);
                   }
                   if(temp!=-1)                       //当前行选中
                   {
                      selectRowIndex[temp]=rowindex;
                      SortChangeColor(selectRow[temp],selectRowIndex[temp]);
                   }
                   else
                   {
                      SortChangeColor(RowTemp,rowindex);
                   }
                   rowindex=j;
                 }
                 else
                 {
                   RowCurrent=RowTemp;
                   strRowCurrent=strRowTemp;
                   rowindex=j;
                 }
            }
        }
    }
    //alert(selectRowIndex.length+"   "+selectRowIndex[0]);
 }

/********************************************
 *               鼠标经过事件               *
 ********************************************/

function elementOnMouseOver()
{  
   if(bDragMode) return false;
   var el=window.event.srcElement;
   while(el.tagName!="TR"&&el.tagName!="TABLE")
           el = el.parentElement;
   if(el.tagName!="TR") return false;  //因为表格有边框出发move事件将退出
   if(currentRowIndex!=el.rowIndex)
   {
      currentRow=el;
      currentRowIndex=el.rowIndex;
      oldRow=currentRow.cloneNode(true);
   }
   if(currentRowIndex!=0)   //不是标题行
   setRowStyle(currentRow,"windowtext","#C1D2EE","#316AC5");
}

/********************************************
 *               鼠标移出事件               *
 ********************************************/
function elementOnMouseOut()

   if(currentRowIndex==0) return false;
   setRowStyle(currentRow,"windowtext",oldRow.runtimeStyle.backgroundColor,oldRow.runtimeStyle.backgroundColor);
}
/********************************************
 *               鼠标按下事件               *
 ********************************************/
function elementOnMouseDown()

   if(currentRowIndex==0) return false;
   setRowStyle(currentRow, "windowtext", "#98B5E2", "#316AC5");
}
/********************************************
 *               鼠标单击事件               *
 ********************************************/
function elementOnMouseClick()

   if(currentRowIndex==0) return false;
   if(window.event.shiftKey)
   {  
       var fristindex=currentRowIndex;
       var lastIndex=selectRowIndex[selectRowIndex.length-1];
       if(currentRowIndex<lastIndex)
       { 
          for(;fristindex<=lastIndex;fristindex++)
          { 
             if(isSelectRow(fristindex))
                continue;
             if(fristindex==currentRowIndex)  
             { 
               addSelectRow(oldRow.cloneNode(true),fristindex);  //将当前行变为选种状态
               setRowStyle(QuerySelected(fristindex),"white", "#316AC5", "#316AC5"); //设置选种颜色
               setCheckState(fristindex,true);
               oldRow=currentRow.cloneNode(true);
             }
             else
             {  
                 addSelectRow(QuerySelected(fristindex).cloneNode(true),fristindex);  //将当前行变为选种状态
                 setRowStyle(QuerySelected(fristindex),"white", "#316AC5", "#316AC5"); //设置选种颜色
                 setCheckState(fristindex,true);
             }
          }
       }
       else
       {
          for(;lastIndex<=fristindex;lastIndex++)
          { 
             if(isSelectRow(lastIndex))
                continue;
             if(lastIndex==currentRowIndex)  
             {
               addSelectRow(oldRow.cloneNode(true),lastIndex);  //将当前行变为选种状态
               setRowStyle(QuerySelected(lastIndex),"white", "#316AC5", "#316AC5"); //设置选种颜色
               setCheckState(lastIndex,true);  
               oldRow=currentRow.cloneNode(true);
             }
             else
             {
                addSelectRow(QuerySelected(lastIndex).cloneNode(true),lastIndex);  //将当前行变为选种状态
                setRowStyle(QuerySelected(lastIndex),"white", "#316AC5", "#316AC5"); //设置选种颜色
                setCheckState(lastIndex,true);
             }
          }
       }
      
   }
   else if(window.event.ctrlKey)
   {    
         if(isSelectRow(currentRowIndex))     //已经选种的话,就变为没选种  
         {
            SetOldRowStyle();
            setCheckState(currentRowIndex,false);                 
            removeSelectRow(currentRowIndex); //移除选种的行
         }
         else
         {
           addSelectRow(oldRow.cloneNode(true),currentRowIndex); 
           setRowStyle(currentRow,"white", "#316AC5", "#316AC5"); //设置选种颜色
           setCheckState(currentRowIndex,true);   
           oldRow=currentRow.cloneNode(true);
         }
   }
   else
   {    
          if(isSelectRow(currentRowIndex))     //已经选种的话,就变为没选种  
          { 
             window.status="click1:"+currentRowIndex;
             SetOldRowStyle();              
             removeSelectRow(currentRowIndex); //移除选种的行
             setCheckState(currentRowIndex,false);
             for(var i=0;i<selectRow.length;i++) //其他行变为未选种状态
             {
                 var tElement = selectRow[i];
           setRowStyle(QuerySelected(selectRowIndex[i]), "windowtext", tElement.runtimeStyle.backgroundColor, tElement.runtimeStyle.borderColor);
           setCheckState(selectRowIndex[i],false);
           tElement = null;
             }
             clearSelectRow();                      //数组清空
          }
          else                                
          {   window.status="click2:"+currentRowIndex;
              for(var i=0;i<selectRow.length;i++) //其他行变为未选种状态
              {
                 var tElement = selectRow[i];
           setRowStyle(QuerySelected(selectRowIndex[i]), "windowtext", tElement.runtimeStyle.backgroundColor, tElement.runtimeStyle.borderColor);
           setCheckState(selectRowIndex[i],false);
           tElement = null;
              }
             clearSelectRow();                      //数组清空
             addSelectRow(oldRow.cloneNode(true),currentRowIndex);  //将当前行变为选种状态
             setCheckState(currentRowIndex,true);                          
             setRowStyle(currentRow,"white", "#316AC5", "#316AC5"); //设置选种颜色
             oldRow=currentRow.cloneNode(true);
        }
   }
}

//--------------------------------------------------------------------------------------
//---------------------------------------Method-----------------------------------------
//--------------------------------------------------------------------------------------

/********************************************
 *               设置行的风格               *
 ********************************************/
function setRowStyle(objUnknown, fontColor, bgColor, borderColor)
{
  objUnknown.runtimeStyle.borderColor = borderColor;
  objUnknown.runtimeStyle.color = fontColor;
  objUnknown.runtimeStyle.backgroundColor = bgColor;
}
/********************************************
 *               初始行的风格               *
 ********************************************/
function initSetRowColors()
{
   for(var i=1;i<length;i++)
   {
       if(i%2==0)
       {
         element.children(0).rows(i).style.borderColor=RowStyleColor;
         element.children(0).rows(i).style.backgroundColor=RowStyleColor;
       }
       else
       {
         element.children(0).rows(i).style.borderColor=AlternatingRowStyleColor;
         element.children(0).rows(i).style.backgroundColor=AlternatingRowStyleColor;
       }
   }
}
/********************************************
 *         得到(table)选中的风格          *
 ********************************************/
function QuerySelected(index)     
{
  return  element.children(0).rows(index);
}
/********************************************
 *         设置行checkbox的状态             *
 ********************************************/
function setCheckState(index,bool)
{
  var tb=element.children(0).rows(index).cells(checkBoxCol).children(0);
  tb.checked=bool;
}
/********************************************
 *            判断当前行是否选种            *
 ********************************************/
function isSelectRow(index)
{
  var flag=false;
  for(var i=0;i<selectRowIndex.length;i++)
  { 
     if(selectRowIndex[i]==index)
     { 
        flag=true;
        break;
     }
  }
  return flag;
}
/*******************************************************
 *            返回选中(table)当前行在选中行的位置    *
 *******************************************************/
function getSelectRow(index) 
{
 
  for(var i=0;i<selectRowIndex.length;i++)
  { 
     if(selectRowIndex[i]==index)
         return i;
  }
  return -1;
}
/**************************************
 *             移除选种行             *
 **************************************/
function removeSelectRow(index)
{
  var flag;
  //查找在数组中的位置
  for(var i=0;i<selectRowIndex.length;i++)
  { 
     if(selectRowIndex[i]==index)
     { 
        flag=i;
        break;
     }
  }
  //remove  selectRowIndex
  for(var i=flag;i<selectRowIndex.length-1;i++)
  {
     selectRowIndex[i]=selectRowIndex[i+1];    
  }
  selectRowIndex.length--;
 
  //remove  selectRow
  for(var i=flag;i<selectRow.length-1;i++)
  {
     selectRow[i]=selectRow[i+1];    
  }
  selectRow.length--; 
   
  }
 
/**************************************
 *             添加选种行             *
 **************************************/

 function addSelectRow(obj,index) 
 {
   selectRow[selectRow.length]=obj;
   selectRowIndex[selectRowIndex.length]=index;
 }
 
/**************************************
 *             清空选种行             *
 **************************************/
 function clearSelectRow() //清空选种行
 {
    selectRow.length=0;
    selectRowIndex.length=0;
 }

/**************************************
 *             保存行的初始颜色       *
 **************************************/
function SetOldRowStyle()
{
   if(currentRowIndex%2==0)
   {
         oldRow.runtimeStyle.borderColor=RowStyleColor;
         oldRow.runtimeStyle.backgroundColor=RowStyleColor;
   }
   else
   {
         oldRow.runtimeStyle.borderColor=AlternatingRowStyleColor;
         oldRow.runtimeStyle.backgroundColor=AlternatingRowStyleColor;
   }

}
/**********************************************
 *           排序后更改行的初始颜色           *
 **********************************************/
function SortChangeColor(SortRow,rowindex)
{
   if(rowindex%2==0)
   {
         SortRow.runtimeStyle.borderColor=RowStyleColor;
         SortRow.runtimeStyle.backgroundColor=RowStyleColor;
   }
   else
   {
        SortRow.runtimeStyle.borderColor=AlternatingRowStyleColor;
        SortRow.runtimeStyle.backgroundColor=AlternatingRowStyleColor;
   }
}

</script>
</HTC>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值