javascript:SpreadSheet.js

本文介绍了一种在网页表格中实现右键上下文菜单的方法,包括显示菜单、隐藏指定列的功能,以及如何通过JavaScript处理点击事件来控制表格单元格的可见性和样式。

//*************************************************//
//*********From here, are the part for ContextMenu
//*********Here are the menu list
//*********Switch
//************************************************//

var fn_rowColor = true;
var fn_rowColor_color1 = "";
var fn_rowColor_color2 = "";

function SalSpreadSheet(ssId)
{
    this.sectionId = ssId;
    this.fn_multipleColumnsSort = false;
    this.fn_singleColumnSort = false;
    this.fn_columnsHide = true;

    this.fn_rowColor = false;
    this.fn_rowColor_color1 = "red";
    this.fn_rowColor_color2 = "white";

    this.fn_changeWidth = false;
    this.fn_columnDraggable = false;
    this.fn_rowDraggable = false;
   
    this.fn_scroll = false;
}

SalSpreadSheet.prototype.initialize = function()
{
  var table = document.getElementById(this.sectionId);
  if(this.fn_columnsHide)
    {
        init_ContextMenu(table);
        //document.getElementById(this.sectionId + "_SELECT").style.display = "inline";
        //document.getElementById(this.sectionId + "_BtnShow").style.display = "inline";
    }
 
  //makeRowColor(table);
  //if(this.fn_changeWidth) init_Change_Columns_Width(table);
}

function makeColumnHide(tableObj)
{
 tableRows = tableObj.getElementsByTagName("TR");
 var objtableRows = tableObj.rows;
    for(var j=0; j< objtableRows.length; j++)
    {
        var strinstid = objtableRows[j].getAttribute("instanceid");
        if(strinstid=="headerinstance")
        {
             ////TODO when multiple header
             tableHeaderCells = objtableRows[j].getElementsByTagName("TD");
        }
    } 
 //tableHeaderCells = tableObj.getElementsByTagName("TH");
 tableCells = tableObj.getElementsByTagName("TD");
}

function init_ContextMenu(tableObj){
  var objtableRows = tableObj.rows;

    var strinstid = objtableRows[0].getAttribute("instanceid");
    if(strinstid==null) ////QA,DSP add event to table header
    {
         var objtableColumns = objtableRows[1].getElementsByTagName("TD");
         var columnNum = objtableColumns.length;     
         for(var i = 0; i< columnNum; i++)
         {
            objtableRows[1].cells[i].oncontextmenu = showContextMenu;
         }
         if(objtableRows.length>=3)
         {
             var strnextinstid = objtableRows[2].getAttribute("instanceid");
             if(strnextinstid=="headerinstance")
             {
                objtableColumns = objtableRows[2].getElementsByTagName("TD");
                columnNum = objtableColumns.length;     
                for(var i = 0; i< columnNum; i++)
                {
                  objtableRows[2].cells[i].oncontextmenu = showContextMenu;
                }
             }
          }
    }   
    if(strinstid=="headerinstance") ////DET add event to table header
    {
         var objtableColumns = objtableRows[0].getElementsByTagName("TD");
         var columnNum = objtableColumns.length;     
         for(var i = 0; i< columnNum; i++)
         {
            objtableRows[0].cells[i].oncontextmenu = showContextMenu;
         }
         if(objtableRows.length>=2)
         {
             var strnextinstid = objtableRows[1].getAttribute("instanceid");
             if(strnextinstid=="headerinstance")
             {
                objtableColumns = objtableRows[1].getElementsByTagName("TD");
                columnNum = objtableColumns.length;     
                for(var i = 0; i< columnNum; i++)
                {
                  objtableRows[1].cells[i].oncontextmenu = showContextMenu;
                }
             }
          }
    }
  // initial hide context menu
  document.body.onclick = hideContextMenu;
}
 
/**************************************************************

*                   Context Menu
*
***************************************************************/

var menuskin = "contextskin1";
var display_url = 0;
var hiddenObj =null;
var currentSectionID = null;
var showObj = null;
var showIndex = null;

function showCol(){
  if(this.sectionId != null){
    var tableObj = document.getElementById(this.sectionId);
  }
  var tableRows = tableObj.getElementsByTagName("TR");
  for (var x=0; x<tableRows.length; x++) {
    tableRows[x].cells[showIndex].style.visibility="visible";
    tableRows[x].cells[showIndex].style.width = "120";
  }
}

function hideCol(ssId)
{
     var cIndex = null;
     var tableDrag = null;
     var tableRows = null;
     var selectObj = null;
        var isHidden = null;
       
        cIndex = hiddenObj.cellIndex;
      
        tableDrag= document.getElementById(ssId);
        tableRows = tableDrag.getElementsByTagName("TR");
        //tableRows = tableDrag.rows;
        for(var j=0; j< tableRows.length; j++)
        {
            cIndex = hiddenObj.cellIndex;
            var strinstid = tableRows[j].getAttribute("instanceid");
            if(strinstid=="headerinstance")
            {
                if(tableRows[j].cells[cIndex].sourceIndex==hiddenObj.sourceIndex)break;
                while(cIndex<tableRows[j].cells.length && tableRows[j].cells[cIndex].sourceIndex!=hiddenObj.sourceIndex)
                {
                     cIndex=cIndex+1;
                }
                if(tableRows[j].cells[cIndex].sourceIndex==hiddenObj.sourceIndex)break;
            }
        }
        var headrowindex=0;
        if(tableRows[0].getAttribute("instanceid")==null)headrowindex=1;
           
        var colspan=tableRows[headrowindex].cells[cIndex].colSpan;
        var colspansum=0;
        var colspancount=0;
        var rowspan=0;
        var maxrowspan=0;
        for(var introwindex=0;introwindex<tableRows[headrowindex].cells.length;introwindex++)
        {
            rowspan=tableRows[headrowindex].cells[introwindex].rowSpan;
            if(maxrowspan<rowspan)maxrowspan=rowspan;
           
            if(introwindex<cIndex)
            {
                var rowcolspan=tableRows[headrowindex].cells[introwindex].colSpan;
                if(rowcolspan!=1){colspansum+=rowcolspan;colspancount+=rowcolspan-1;}
            }
        }
       
        var intXindex=0; //DET
        if(tableRows[0].getAttribute("instanceid")==null)
        {
            intXindex=1; ///QA,DSP
            if(colspan!=1) ////// QA,DSP hide the row[0] when row[1] colspan!=1
            {
                 for(var intindex=0;intindex<colspan;intindex++)
                 {
                    if(tableRows[0].cells[intindex+cIndex]!=undefined)
                    {
                       if(colspancount>0)
                       {
                            tableRows[0].cells[intindex+cIndex+colspancount].style.borderStyle = "none";
                            tableRows[0].cells[intindex+cIndex+colspancount].style.padding="0";
                            tableRows[0].cells[intindex+cIndex+colspancount].style.width="0";
                            tableRows[0].cells[intindex+cIndex+colspancount].style.display = "none";//Justin for BN33198
                            if(tableRows[0].cells[intindex+cIndex+colspancount].firstChild && tableRows[0].cells[intindex+cIndex+colspancount].firstChild.nodeType!=3)
                                tableRows[0].cells[intindex+cIndex+colspancount].firstChild.style.display = "none";
                        }
                        else
                        {
                            tableRows[0].cells[intindex+cIndex].style.borderStyle = "none";
                            tableRows[0].cells[intindex+cIndex].style.padding="0";
                            tableRows[0].cells[intindex+cIndex].style.width="0";
                            tableRows[0].cells[intindex+cIndex].style.display = "none";//Justin for BN33198
                            if(tableRows[0].cells[intindex+cIndex].firstChild && tableRows[0].cells[intindex+cIndex].firstChild.nodeType!=3)
                                tableRows[0].cells[intindex+cIndex].firstChild.style.display = "none";                       
                        }
                    }
                }
            }
            else
            {
                ///hide QA,DSP row[0] when row[1] colspan ==1
               if(colspancount>0)
               {
                    tableRows[0].cells[cIndex+colspancount].style.borderStyle = "none";
                    tableRows[0].cells[cIndex+colspancount].style.padding="0";
                    tableRows[0].cells[cIndex+colspancount].style.width="0";
                    tableRows[0].cells[cIndex+colspancount].style.display = "none";//Justin for BN33198
                    if(tableRows[0].cells[cIndex+colspancount].firstChild && tableRows[0].cells[cIndex+colspancount].firstChild.nodeType!=3)
                        tableRows[0].cells[cIndex+colspancount].firstChild.style.display = "none";
                }
                else
                {
                    tableRows[0].cells[cIndex].style.borderStyle = "none";
                    tableRows[0].cells[cIndex].style.padding="0";
                    tableRows[0].cells[cIndex].style.width="0";
                    tableRows[0].cells[cIndex].style.display = "none";//Justin for BN33198
                    if(tableRows[0].cells[cIndex].firstChild && tableRows[0].cells[cIndex].firstChild.nodeType!=3)
                        tableRows[0].cells[cIndex].firstChild.style.display = "none";                       
                }
            }
        } 
      //hide column which selected.
      for (var x=intXindex; x<tableRows.length; x++)
      {     
            if(x>intXindex && x<maxrowspan+intXindex && colspan!=1) /////hide the table title and the title colspan !=1
            {
                for(var intindex=0;intindex<colspan;intindex++)
                {
                    if(tableRows[x].cells[intindex+colspansum]!=undefined)
                    {
                        tableRows[x].cells[intindex+colspansum].style.borderStyle = "none";
                        tableRows[x].cells[intindex+colspansum].style.padding="0";
                        tableRows[x].cells[intindex+colspansum].style.width="0";
                        tableRows[x].cells[intindex+colspansum].style.display = "none";//Justin for BN33198
                        if(tableRows[x].cells[intindex+colspansum].firstChild && tableRows[x].cells[intindex+colspansum].firstChild.nodeType!=3)
                        tableRows[x].cells[intindex+colspansum].firstChild.style.display = "none";
                    }
                }
            }
            else if(x>=maxrowspan+intXindex && colspan!=1)
            {   /////hide the table body row which colspan !=1
                 for(var intindex=0;intindex<colspan;intindex++)
                 {
                    if(tableRows[x].cells[intindex+cIndex]!=undefined)
                    {
                       if(colspancount>0)
                       {
                            tableRows[x].cells[intindex+cIndex+colspancount].style.borderStyle = "none";
                            tableRows[x].cells[intindex+cIndex+colspancount].style.padding="0";
                            tableRows[x].cells[intindex+cIndex+colspancount].style.width="0";
                            tableRows[x].cells[intindex+cIndex+colspancount].style.display = "none";//Justin for BN33198
                            if(tableRows[x].cells[intindex+cIndex+colspancount].firstChild && tableRows[x].cells[intindex+cIndex+colspancount].firstChild.nodeType!=3)
                                tableRows[x].cells[intindex+cIndex+colspancount].firstChild.style.display = "none";
                        }
                        else
                        {
                            tableRows[x].cells[intindex+cIndex].style.borderStyle = "none";
                            tableRows[x].cells[intindex+cIndex].style.padding="0";
                            tableRows[x].cells[intindex+cIndex].style.width="0";
                            tableRows[x].cells[intindex+cIndex].style.display = "none";//Justin for BN33198
                            if(tableRows[x].cells[intindex+cIndex].firstChild && tableRows[x].cells[intindex+cIndex].firstChild.nodeType!=3)
                                tableRows[x].cells[intindex+cIndex].firstChild.style.display = "none";                       
                        }
                    }
                }
            }
            else
            {  /////hide the table body row which colspan ==1
                if(tableRows[x].cells[cIndex]!=undefined &&(x>=maxrowspan+intXindex || tableRows[x].cells[cIndex].sourceIndex==hiddenObj.sourceIndex)) //&& tableRows[x].cells[cIndex].sourceIndex==hiddenObj.sourceIndex
                {
                    if(colspancount>0 && x>=maxrowspan+intXindex)
                    {
                        tableRows[x].cells[cIndex+colspancount].style.borderStyle = "none";
                        tableRows[x].cells[cIndex+colspancount].style.padding="0";
                        tableRows[x].cells[cIndex+colspancount].style.width="0";
                        tableRows[x].cells[cIndex+colspancount].style.display = "none";//Justin for BN33198
                        if(tableRows[x].cells[cIndex+colspancount].firstChild && tableRows[x].cells[cIndex+colspancount].firstChild.nodeType!=3)
                            tableRows[x].cells[cIndex+colspancount].firstChild.style.display = "none";
                    }
                    else
                    {
                        tableRows[x].cells[cIndex].style.borderStyle = "none";
                        tableRows[x].cells[cIndex].style.padding="0";
                        tableRows[x].cells[cIndex].style.width="0";
                        tableRows[x].cells[cIndex].style.display = "none";//Justin for BN33198
                        if(tableRows[x].cells[cIndex].firstChild && tableRows[x].cells[cIndex].firstChild.nodeType!=3)
                            tableRows[x].cells[cIndex].firstChild.style.display = "none";
                    }
                }
            }              
      }
}

function showContextMenu()
{
  var e = null;
  var contextmenu = document.getElementById("contextmenu");
  var containerID = null;
  var container = null;
  var strIframeID = "menuback1";

  e = window.event;

  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;
 
  hiddenObj = targ;
  while(hiddenObj.tagName!="TD"){
     hiddenObj = hiddenObj.parentElement;
 }
 
  hiddenTrObj=hiddenObj;
  while(hiddenTrObj.tagName!="TR"){
     hiddenTrObj = hiddenTrObj.parentElement;
 }
 
    var tmpTarg = targ;
 while(tmpTarg.tagName!="TABLE"){
     tmpTarg = tmpTarg.parentElement;
 }
 
 var objhiddenmenu=document.getElementById("hiddenmenuitem");
    if (objhiddenmenu)  objhiddenmenu.style.display = "inline";

 var objtableRows = tmpTarg.rows;
 var strinstid = objtableRows[0].getAttribute("instanceid");
 if(strinstid==null) ////QA,DSP
 {
     if(hiddenTrObj.rowIndex=="2") 
     {
          objhiddenmenu=document.getElementById("hiddenmenuitem");
             objhiddenmenu.style.display = "none";
     }
 } 
 if(strinstid=="headerinstance")//DET
 {
     if(hiddenTrObj.rowIndex=="1") 
     {
          objhiddenmenu=document.getElementById("hiddenmenuitem");
             objhiddenmenu.style.display = "none";
     }
 }
 
    if (document.getElementById(strIframeID))
    {
        var ifrm = document.getElementById(strIframeID);
    }
    else
 {
     var ifrm = document.createElement("IFRAME");
     ifrm.style.border = "none";
     ifrm.style.position = "absolute";
     ifrm.id = strIframeID;
     ifrm.style.filter = "Alpha(opacity=0)";
     ifrm.style.width = contextmenu.offsetWidth;
     ifrm.style.height = contextmenu.offsetHeight;

     document.body.appendChild(ifrm);
 }

//  window.status = tmpTarg.offsetLeft + " " + tmpTarg.offsetTop;   
//  contextmenu.style.left = targ.offsetLeft + targ.offsetWidth/2 - document.mainform.scrollLeft;
//  contextmenu.style.top = 250 + targ.offsetHeight/2 - document.mainform.scrollTop;
 
    var intWindowHeight = document.documentElement.clientHeight - 25;
 var intWindowWidth = document.documentElement.clientWidth - contextmenu.offsetWidth;
 //calculate where the menu can be displayed
 if(event.clientY < intWindowHeight)
 {
     contextmenu.style.top = e.clientY + document.body.scrollTop;//document.documentElement.scrollTop
     document.getElementById('menuback1').style.top = e.clientY + document.body.scrollTop + 1;
 }
 else
 {
     contextmenu.style.top = e.clientY + document.body.scrollTop - 25;
     document.getElementById('menuback1').style.top = e.clientY + document.body.scrollTop - 25 + 1;
 }
 if(event.clientX < intWindowWidth)
 {
     contextmenu.style.left = e.clientX + document.body.scrollLeft+25;
     document.getElementById('menuback1').style.left = e.clientX + document.body.scrollLeft+25+ 1;
    
 }
 else
 {
     contextmenu.style.left = e.clientX + document.body.scrollLeft +25 - contextmenu.offsetWidth;
     document.getElementById('menuback1').style.left = e.clientX + document.body.scrollLeft +25 - contextmenu.offsetWidth + 1;
    }
   
    //do not let menu out of the window ---by kid
    var rightedge = document.body.clientWidth - event.clientX;
    var bottomedge = document.body.clientHeight - event.clientY;
   
    if (rightedge < contextmenu.offsetWidth)
        contextmenu.style.left = document.body.scrollLeft + event.clientX - contextmenu.offsetWidth;//left one width
    else
        contextmenu.style.left = document.body.scrollLeft + event.clientX;

    if (bottomedge < contextmenu.offsetHeight)
        contextmenu.style.top = document.body.scrollTop + event.clientY - contextmenu.offsetHeight;//up one height
    else
        contextmenu.style.top = document.body.scrollTop + event.clientY;

    ifrm.style.top = contextmenu.offsetTop;
    ifrm.style.left = contextmenu.offsetLeft;
    ifrm.style.width = contextmenu.offsetWidth;
    ifrm.style.height = contextmenu.offsetHeight;

    contextmenu.style.visibility = "visible";
 
    return false;
}

function hideContextMenu() {
  contextmenu.style.visibility = "hidden";
  var frameback=document.getElementById('menuback1');
  if(frameback)document.body.removeChild(frameback);
}

function highlightContextMenu() {
  if (event.srcElement.className == "salSpv_menuitems")
  {  
    event.srcElement.style.backgroundColor = "highlight";
    event.srcElement.style.color = "white";
   
    if (display_url)window.status = event.srcElement.url;
  }
}

function lowlightContextMenu() {
  if (event.srcElement.className == "salSpv_menuitems")
  {
    event.srcElement.style.backgroundColor = "";
    event.srcElement.style.color = "black";
    window.status = "";
  }
}

function jumptoContextMenu() {
  var seltext=window.document.selection.createRange().text;
 
  if (event.srcElement.className == "salSpv_menuitems") //contextskin0 salSpv_menuitems
  {
    if (event.srcElement.getAttribute("target") != null)
      window.open(event.srcElement.url, event.srcElement.getAttribute("target"));
    else
      window.location = event.srcElement.url;
  }
  return;
}

function updHidden(ssId)
{
  if(hiddenObj){
     hideCol(ssId);
 //updHidden_callback({value:"success"});
  }
}

 function updShow(showButton)
 {
   var selectObjId = null;
   var selectObj = null;
   var selectedCol = null;
   var cIndex = null;
  
   currentSectionID = getSectionID(showButton);

   selectObjId = currentSectionID +"_SELECT";   
   selectObj = document.getElementById(selectObjId);
  
   // remind user when user didn't select any column.
   if ( selectObj.options.selectedIndex <= 0 ) {
     alert("Please select a column to display!");
     return;
   }
  
     tableObj = document.getElementById(currentSectionID);
   
    // remove option when select this item.
    for (var i=0; i < selectObj.options.length; i++){
         if(selectObj.options[i].selected){
            selectedCol = selectObj.options[i].text;
            selectObj.remove(i);
         }
    }
 
    // show the select column and initial the column width
    var objtableRows = tableObj.rows;
    for(var j=0; j< objtableRows.length; j++)
    {
        var strinstid = objtableRows[j].getAttribute("instanceid");
        if(strinstid=="headerinstance")
        {
             var objtableColumns = objtableRows[j].getElementsByTagName("TD");
             var columnNum = objtableColumns.length;     
             for(var i = 0; i< columnNum; i++)
             {
                 if (objtableRows[j].cells[i].firstChild.nodeValue.trim() == selectedCol){
                  showIndex = objtableRows[j].cells[i].cellIndex;
                  initialColWidth(objtableRows[j].cells[i],tableObj);
                  showObj = objtableRows[j].cells[i];
                  break;
             }
        }
    }
    
   if(showObj){
    updShow_callback({value:"success"});
   }
}
 
 function updShow_callback(result)
 {
    if(result.value == "success")
    {
        showCol();
    }else{
        alert("Error");
        return false;
    }
 }

/**************************************************************
*
*                      Change Column Width
*
***************************************************************/

function getPosition(obj)
{
    var left = 0;
    var top = 0;

    while (obj.offsetParent){
    left += obj.offsetLeft;
    top += obj.offsetTop;
    obj = obj.offsetParent;
    }
    left += obj.offsetLeft;
    top += obj.offsetTop;
    return {x:left, y:top};
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值