dhtmlxgrid 1.4功能自助增补(一)--右键菜单和行号

最近因为一个机会研究了一下dhtmlxgrid的1.4专业版,以下是自己的总结,有差漏或错误请各位网友积极指正!
(由于版权的问题,附件中只有我自己添加的js和css文件)

1. 添加自己的自定义右键菜单
在1.4的专业版中已经添加了自定义右键菜单并且也有示例,但是当在grid上可以选择多个单元格进行操作时,在所选区域上右击却不能显示右键菜单,
原因是grid在有区域被选择后没有修改自己的oncontextmenu属性;我用它自己的右键菜单尝试修改,没有成功。所以自己修改了一个右键菜单添加到了其
中;
以下是我自己修改后的一个右键菜单的js源码,其中添加了不少专业版中的功能;

代码
  1.        
  2. // contruct main menu object   
  3. function contextMenu() {   
  4.     this.items = new Array();   
  5.     this.addItem = function (item) {   
  6.         this.items[this.items.length] = item;   
  7.     };   
  8.     this.show = function (oDoc) {   
  9.         var strShow = "";   
  10.         var i;   
  11.         strShow = "<div id=/"rightmenu/" class=/"menudiv/">";   
  12.         strShow += "<table border=/"0/" height=/"";   
  13.         strShow += this.items.length * 20;   
  14.         strShow += "/" cellpadding=/"0/" cellspacing=/"0/">";   
  15.         strShow += "<tr height=/"3/"><td  width=/"2/"></td><td>";   
  16.         strShow += "<table border=/"0/" width=/"100%/" height=/"100%/" cellpadding=0 cellspacing=0 >";   
  17.         strShow += "<tr><td width=/"23/"></td><td><img src=/" /" height=/"1/" border=/"0/"></td></tr></table>";   
  18.         strShow += "</td><td width=/"2/"></td></tr>";   
  19.         strShow += "<tr><td></td><td>";   
  20.         strShow += "<table border=/"0/" width=/"100%/" height=/"100%/" cellpadding=3 cellspacing=0 >";   
  21.         oDoc.write(strShow);   
  22.         for (i = 0; i < this.items.length; i++) {   
  23.             this.items[i].show(oDoc);   
  24.         }   
  25.         strShow = "</table></td><td></td></tr>";   
  26.         strShow += "<tr height=/"3/"><td></td><td>";   
  27.         strShow += "<table border=/"0/" width=/"100%/" height=/"100%/" cellpadding=0 cellspacing=0>";   
  28.         strShow += "<tr><td width=/"23/"></td><td><img src=/" /" height=/"1/" border=/"0/"></td></tr></table>";   
  29.         strShow += "</td><td></td></tr>";   
  30.         strShow += "</table></div>/n";   
  31.         oDoc.write(strShow);   
  32.     };   
  33. }   
  34.   
  35. // contruct menu Item object   
  36. function contextItem(text, icon, id, type) {   
  37.     this.text = text ? text : "";   
  38.     this.icon = icon ? icon : "";   
  39.     this.id = id ? id : "";   
  40.     this.type = type ? type : "menu";   
  41.     this.show = function (oDoc) {   
  42.         var strShow = "";   
  43.         if (this.type == "menu") {   
  44.             strShow += "<tr id='" + this.id + "' ";   
  45.             strShow += "onmouseover=/"changeStyle(this, 'on');/" ";   
  46.             strShow += "onmouseout=/"changeStyle(this, 'out');/" ";   
  47.             //strShow += "onclick="";   
  48.             //strShow += this.cmd;   
  49.             strShow += ">";   
  50.             strShow += "<td class=/"ltdexit/" width=/"16/">";   
  51.             if (this.icon == "") {   
  52.                 strShow += " ";   
  53.             } else {   
  54.                 strShow += "<img border=/"0/" src=/"";   
  55.                 strShow += this.icon;   
  56.                 strShow += "/" width=/"16/" height=/"16/" style=/"POSITION: relative/"></img>";   
  57.             }   
  58.             strShow += "</td><td class=/"mtdexit/">";   
  59.             strShow += this.text;   
  60.             strShow += "</td><td class=/"rtdexit/" width=/"5/"> </td></tr>";   
  61.         } else {   
  62.             if (this.type == "separator") {   
  63.                 //strShow += "<tr><td class="ltdexit"> </td>";   
  64.                 strShow += "<td class=/"mtdexit/" style='height:10px;' colspan=/"3/"><hr class='hrLine' color=/"#7EC0EE/" size=/"1/"></td></tr>";   
  65.             }   
  66.         }   
  67.         oDoc.write(strShow);   
  68.     };   
  69. }   
  70. // change style of every menu item when mouse over   
  71. function changeStyle(obj, cmd) {   
  72.     if (obj) {   
  73.         try {   
  74.             var imgObj = obj.children(0).children(0);   
  75.             if (cmd == "on") {   
  76.                 obj.children(0).className = "ltdfocus";   
  77.                 obj.children(1).className = "mtdfocus";   
  78.                 obj.children(2).className = "rtdfocus";   
  79.                 if (imgObj) {   
  80.                     if (imgObj.tagName.toUpperCase() == "IMG") {   
  81.                         imgObj.style.left = "-1px";   
  82.                         imgObj.style.top = "-1px";   
  83.                     }   
  84.                 }   
  85.             } else {   
  86.                 if (cmd == "out") {   
  87.                     obj.children(0).className = "ltdexit";   
  88.                     obj.children(1).className = "mtdexit";   
  89.                     obj.children(2).className = "rtdexit";   
  90.                     if (imgObj) {   
  91.                         if (imgObj.tagName.toUpperCase() == "IMG") {   
  92.                             imgObj.style.left = "0px";   
  93.                             imgObj.style.top = "0px";   
  94.                         }   
  95.                     }   
  96.                 }   
  97.             }   
  98.         }   
  99.         catch (e) {   
  100.         }   
  101.     }   
  102. }   
  103. //show right menu on page   
  104. function showMenu() {   
  105.     var x, y, w, h, ox, oy;   
  106.     x = event.clientX;   
  107.     y = event.clientY;   
  108.     var obj = document.getElementById("rightmenu");   
  109.     if (obj == null) {   
  110.         return true;   
  111.     }   
  112.     ox = document.body.clientWidth;   
  113.     oy = document.body.clientHeight;   
  114.     if (x > ox || y > oy) {   
  115.         return false;   
  116.     }   
  117.     w = obj.offsetWidth;   
  118.     h = obj.offsetHeight;   
  119.     if ((x + w) > ox) {   
  120.         xx = x - w;   
  121.     }   
  122.     if ((y + h) > oy) {   
  123.         yy = y - h;   
  124.     }   
  125.     obj.style.posLeft = x + document.body.scrollLeft;   
  126.     obj.style.posTop = y + document.body.scrollTop;   
  127.     obj.style.visibility = "visible";   
  128.     return false;   
  129. }   
  130. //hide right menu   
  131. function hideMenu() {   
  132.     if (event.button == 0) {   
  133.         var obj = document.getElementById("rightmenu");   
  134.         if (obj == null) {   
  135.             return true;   
  136.         }   
  137.         obj.style.visibility = "hidden";   
  138.         obj.style.posLeft = 0;   
  139.         obj.style.posTop = 0;   
  140.     }   
  141. }   
  142. //the mothed of contruct right menu   
  143. dhtmlXGridObject.prototype.makeMenu = function () {   
  144.     var self = this;   
  145.     var myMenu, item;   
  146.     myMenu = new contextMenu();   
  147.     item = new contextItem("copy", "", "cellCopy", "menu");   
  148.     myMenu.addItem(item);   
  149.     item = new contextItem("cut", "", "cellCut", "menu");   
  150.     myMenu.addItem(item);   
  151.     item = new contextItem("paste", "", "cellPaste", "menu");   
  152.     myMenu.addItem(item);   
  153.     item = new contextItem("add row", "", "rowAdd", "menu");   
  154.     myMenu.addItem(item);   
  155.     item = new contextItem("", "", "", "separator");   
  156.     myMenu.addItem(item);   
  157.     item = new contextItem("delete cell(s)", "", "cellDelete", "menu");   
  158.     myMenu.addItem(item);   
  159.     item = new contextItem("delete row", "", "rowDelete", "menu");   
  160.     myMenu.addItem(item);   
  161.     myMenu.show(document);   
  162.     delete item;   
  163.     delete myMenu;   
  164.     if (this.objName) {   
  165.         this.objName = "mygrid";   
  166.     }   
  167.     //add action to every menu item    
  168.     document.getElementById("cellCopy").onclick = new Function(new String(this.objName + ".doRightMenuCopy();"));   
  169.     document.getElementById("cellCut").onclick = new Function(new String(this.objName + ".doRightMenuCut()"));   
  170.     document.getElementById("cellPaste").onclick = new Function(new String(this.objName + ".doRightMenuPaste()"));   
  171.     document.getElementById("rowAdd").onclick = new Function(new String(this.objName + ".doRightMenuAddRow()"));   
  172.     document.getElementById("cellDelete").onclick = new Function(new String(this.objName + ".doRightMenuDelete();"));   
  173.     document.getElementById("rowDelete").onclick = new Function(new String(this.objName + ".deleteSelectedItem();"));   
  174. };   
  175. function toggleMenu(isEnable) {   
  176.     if (isEnable) {   
  177.         document.oncontextmenu = showMenu;   
  178.     } else {   
  179.         document.oncontextmenu = new function () {   
  180.             return true;   
  181.         };   
  182.     }   
  183. }   
  184. //the mothed of show right menu in dhtmlXGridObject   
  185. dhtmlXGridObject.prototype.showRightMenu = function () {   
  186.     document.attachEvent("onclick", hideMenu);   
  187.     showMenu();   
  188.     document.attachEvent("oncontextmenu", new Function("return false"));   
  189. };   
  190. //set the name of grid object(it's necessary if you want to response the menu item click)   
  191. dhtmlXGridObject.prototype.setRightMenuObject = function (para) {   
  192.     this.objName = para;   
  193. };   
  194. //response to click copy item    
  195. dhtmlXGridObject.prototype.doRightMenuCopy = function () {   
  196.     //alert("doCopy");   
  197.     if (this._selectionArea) {   
  198.         this.setCSVDelimiter("/t");   
  199.         this.copyBlockToClipboard();   
  200.     } else {   
  201.         if (this.cell) {   
  202.             if (!(this.cellType[this.cell._cellIndex] == "linenumber") && !this.editor) {   
  203.                 window.clipboardData.setData("text", this.cells(this.getSelectedId(), this.cell._cellIndex).getValue());   
  204.             }   
  205.         }   
  206.     }   
  207. };   
  208. //response to click cut item    
  209. dhtmlXGridObject.prototype.doRightMenuCut = function () {   
  210.     //alert("doCut");   
  211.     if (this._selectionArea) {   
  212.         this.setCSVDelimiter("/t");   
  213.         this.copyBlockToClipboard();   
  214.     } else {   
  215.         if (this.cell) {   
  216.             if (!(this.cellType[this.cell._cellIndex] == "linenumber") && !this.editor) {   
  217.                 window.clipboardData.setData("text", this.cells(this.getSelectedId(), this.cell._cellIndex).getValue());   
  218.             }   
  219.         }   
  220.     }   
  221.     this.doRightMenuDelete();   
  222. };   
  223. //response to click paste item    
  224. dhtmlXGridObject.prototype.doRightMenuPaste = function () {   
  225.     //alert("doPaste");   
  226.     this.pasteBlockFromClipboard();   
  227. };   
  228. //add new row   
  229. dhtmlXGridObject.prototype.doRightMenuAddRow = function () {   
  230.     var cc = this.getColumnCount();   
  231.     var newnewrow = new Array(cc + 1);   
  232.     this.addRow((new Date()).valueOf(), newrow, this.getRowIndex(this.getSelectedId()));   
  233.     cc = null;   
  234.     newrow = null;   
  235. };   
  236. //response to click delete item    
  237. dhtmlXGridObject.prototype.doRightMenuDelete = function () {   
  238.     //alert("doDelete");   
  239.     if (this._selectionArea) {   
  240.         this.clearSelection();   
  241.         var startRow = this._selectionArea.LeftTopRow;   
  242.         var startCol = this._selectionArea.LeftTopCol;   
  243.         var endRow = this._selectionArea.RightBottomRow;   
  244.         var endCol = this._selectionArea.RightBottomCol;   
  245.         for (var i = startRow; i < endRow + 1; i++) {   
  246.             for (var j = startCol; j < endCol + 1; j++) {   
  247.                 if (!(this.cellType[j] == "linenumber")) {   
  248.                     this.cells(this.getRowId(i), j).setValue();   
  249.                 }   
  250.             }   
  251.         }   
  252.         startRow = null;   
  253.         startCol = null;   
  254.         endRow = null;   
  255.         endCol = null;   
  256.     } else {   
  257.         if (this.cell) {   
  258.             if (!(this.cellType[this.cell._cellIndex] == "linenumber") && !this.editor) {   
  259.                 this.cells(this.getSelectedId(), this.cell._cellIndex).setValue();   
  260.             }   
  261.         }   
  262.     }   
  263. };   
  264.   
<script type="text/javascript">render_code();</script>
下面是右键菜单的样式表:
代码
  1. TABLE {   
  2.     font-family: /"Tahoma/", /"Verdana/", /"/u5b8b/u4f53/";   
  3.     font-size: 9pt   
  4. }   
  5.   
  6. .mtdfocus {   
  7.     background-color: #EDF5FE;   
  8.     border-bottom: #B0E2FF 1px solid;   
  9.     border-top: #B0E2FF 1px solid;   
  10.     cursor: hand   
  11. }   
  12.   
  13. .mtdexit {   
  14.     background-color: whitesmoke;   
  15.     border-bottom: #ffffff 1px solid;   
  16.     border-top: #ffffff 1px solid;   
  17. }   
  18.   
  19. .ltdfocus {   
  20.     background-color: #EDF5FE;   
  21.     border-bottom: #B0E2FF 1px solid;   
  22.     border-top: #B0E2FF 1px solid;   
  23.     border-left: whitesmoke 1px solid;   
  24.     cursor: hand   
  25. }   
  26.   
  27. .ltdexit {   
  28.     background-color: whitesmoke;   
  29.     border-bottom: #ffffff 1px solid;   
  30.     border-top: #ffffff 1px solid;   
  31.     border-left: whitesmoke 1px solid   
  32. }   
  33.   
  34. .rtdfocus {   
  35.     background-color: #EDF5FE;   
  36.     border-bottom: #B0E2FF 1px solid;   
  37.     border-top: #B0E2FF 1px solid;   
  38.     border-right: whitesmoke 1px solid;   
  39.     cursor: hand   
  40. }   
  41.   
  42. .rtdexit {   
  43.     background-color: whitesmoke;   
  44.     border-bottom: #ffffff 1px solid;   
  45.     border-top: #ffffff 1px solid;   
  46.     border-right: whitesmoke 1px solid   
  47. }   
  48.   
  49. .menudiv {   
  50.     background-color: whitesmoke;   
  51.     border: #00B2EE 1px solid;   
  52.     left: 0px;   
  53.     position: absolute;   
  54.     top: 0px;   
  55.     visibility: hidden;   
  56.     z-index: 10;   
  57. }   
  58.   
  59. .hrLine {   
  60.     position: absolute;   
  61.     bottom: 52px;   
  62. }   
  63.   
<script type="text/javascript">render_code();</script>
然后将右键菜单添加到grid中:
(1) 修改dhtmlXGridObject对象中的this.init 方法,这个方法在dhtmlXGrid.js文件中。
示例代码如下,注释之间的代码是新增代码:
代码
  1. this.init = function (fl) {   
  2.     if ((this.isTreeGrid()) && (!this._h2)) {   
  3.         this._aEx = new _dhtmlxArray();   
  4.         this._h2 = new dhtmlxHierarchy();   
  5.         if ((this._fake) && (!this._realfake)) {   
  6.             this._fake._h2 = this._h2;   
  7.         }   
  8.         this._tgc = {imgURL:null};   
  9.     }   
  10.     if (!this._hstyles) {   
  11.         return;   
  12.     }   
  13.     //daoger_start   
  14.     //constructe components of right menu   
  15.     this.makeMenu();   
  16.     //add rightclick action   
  17.     this.setOnRightClick(this.showRightMenu);   
  18.     //daoger_end   
  19.     this.editStop();   
  20.     this.lastClicked = null;   
  21.     this.resized = null;   
  22.     this.fldSorted = this.r_fldSorted = null;   
  23.     this.gridWidth = 0;   
  24.     this.gridHeight = 0;   
  25.     this.cellWidthPX = new Array(0);   
  26.     this.cellWidthPC = new Array(0);   
  27.     if (this.hdr.rows.length > 0) {   
  28.         this.clearAll(true);   
  29.     }   
  30.     ...... //方法的以后部分省略   
<script type="text/javascript">render_code();</script>
(2) 将右键菜单隐藏操作添加到grid的单击事件中,即修改this.obj.onclick 属性方法,也是在dhtmlXGrid.js文件中;
将原方法中添加hideMenu()方法;修改后的代码如下:
代码
  1. ......   
  2. this.obj.onmousemove = this._drawTooltip;   
  3. //daoger_start   
  4. //this.obj.onclick = new Function("e", "this.grid._doClick(e||window.event);if (this.grid._sclE)this.grid.editCell(e||window.event);(e||event).cancelBubble=true;");   
  5. //hide right menu when left click   
  6. this.obj.onclick = new Function("e""hideMenu();this.grid._doClick(e||window.event);if (this.grid._sclE)this.grid.editCell(e||window.event);(e||event).cancelBubble=true;");   
  7. //daoger_end   
  8. if (_isMacOS) {   
  9.     this.entBox.oncontextmenu = new Function("e""return this.grid._doContClick(e||window.event);");   
  10. }   
  11. ......   
<script type="text/javascript">render_code();</script>

(3) 修改dhtmlXGridObject对象中的_OnSelectionStop 方法,使得在单元格选择区域(选择区域的实现其实就是添加了一个div区域)上,也可以有
自定义的右键菜单,使得当单击区域时区域消失,右击区域显示菜单;这个方法在dhtmlXGrid_selection.js文件中,代码如下:
代码
  1. dhtmlXGridObject.prototype._OnSelectionStop = function (event) {   
  2.     var self = this;   
  3.     if (this._blsTimer) {   
  4.         window.clearTimeout(this._blsTimer);   
  5.     }   
  6.     this.obj.onmousedown = function (e) {   
  7.         e = e || event;   
  8.         //daoger_start   
  9.         //self._OnSelectionStart(e, this);   
  10.         //define the different operations of right click and left click    
  11.         if (e.button == 1) {   
  12.             self._OnSelectionStart(e, this);//do select   
  13.         } else {   
  14.             if (e.button == 2) {   
  15.                 self.showRightMenu();//show right menu   
  16.             }   
  17.         }   
  18.         //daoger_end   
  19.         return true;   
  20.     };   
  21.     this.obj.onmousemove = this.obj.onmmold || null;   
  22.     document.body.onmouseup = this._oldDMP || null;   
  23.     if (parseInt(this._selectionObj.style.width) < 2 && parseInt(this._selectionObj.style.height) < 2) {   
  24.         this._HideSelection();   
  25.     } else {   
  26.         var src = this.getFirstParentOfType(event.srcElement || event.target, "TD");   
  27.         if ((!src) || (!src.parentNode.idd)) {   
  28.             src = this._endSelectionCell;   
  29.         }   
  30.         while (src.tagName.toLowerCase() != "td") {   
  31.             src = src.parentNode;   
  32.         }   
  33.         this._stopSelectionCell = src;   
  34.         this._selectionArea = this._RedrawSelectionPos(this._startSelectionCell, this._stopSelectionCell);   
  35.         this.callEvent("onBlockSelected", []);   
  36.     }   
  37. };   
<script type="text/javascript">render_code();</script>
(4) 为了实现右键菜单的功能,还需要在表格初始化方法init()前添加mygrid.setRightMenuObject("mygrid"),其中参数就是声明的grid对象名称。

 

2. 表格左侧添加行号
行号其实就是在原有的grid上添加一个列,放置每一行的indexid就可以了;专业版中已经给出了添加列的属性方法。
(1) 声明两个变量:

代码
  1. var lineflag = false;//是否添加行号判定标志   
  2. var itemsid = new Array(0);//所有行的id数组   
<script type="text/javascript">render_code();</script>
(2) 添加一个linenumber列类型,它不接受编辑,只可以选择;要修改的文件是dhtmlXGridCell.js;
在该文件中添加以下代码:
代码
  1. //daoger_start   
  2. //create linenumber type of column   
  3. function eXcell_linenumber(cell) {   
  4.     this.base = eXcell_ed;   
  5.     this.base(cell);   
  6.     this.editable = false;   
  7.     this.edit = function () {   
  8.     };   
  9.     this.isDisabled = function () {   
  10.         return true;   
  11.     };   
  12. }   
  13. //take on all mothed of ed type   
  14. eXcell_linenumber.prototype = new eXcell_ed;   
  15. //daoger_end   
<script type="text/javascript">render_code();</script>
(3) 在dhtmlXGrid.js中添加设定标志位和添加行号的方法:
代码
  1.         //daoger_start_addLineNumber   
  2. //the mothed of add line number on the first column   
  3. dhtmlXGridObject.prototype.addLineNumber = function () {   
  4.     var linecell = null;   
  5.     itemsid = this.getAllItemIds().split(",");   
  6.     if (itemsid.length > 1) {   
  7.         for (var i = 0; i < itemsid.length; i++) {   
  8.             linecell = this.cells(itemsid[i], 0);   
  9.             linecell.setValue(this.getRowIndex(itemsid[i]) + 1);   
  10.         }   
  11.     }   
  12. };   
  13. dhtmlXGridObject.prototype.setLineFlag = function (para) {   
  14.     if (para) {   
  15.         lineflag = true;   
  16.     }   
  17.     if (lineflag) {   
  18.         //add line number column at the first index   
  19.         this.insertColumn(0" ""linenumber"30);   
  20.         //add line number on this column   
  21.         this.addLineNumber();   
  22.     }   
  23. };   
  24. //daoger_end   
  25.            
  26.       
<script type="text/javascript">render_code();</script>
(4) 因为在初始化构建表格时,dhtmlxgrid本身默认采用的是异步模式,这时候是取不到每一行的id的;在直接浏览html页面时可以成功添加行号,
但是在服务器中不行;并且采用异步模式还有很多隐患,所以修改dhtmlxgrid的数据加载模式:
代码
  1. //daoger_start   
  2.     this.xmlLoader = new dtmlXMLLoaderObject(this.doLoadDetails, window, falsethis.no_cashe);   
  3.     //this.xmlLoader = new dtmlXMLLoaderObject(this.doLoadDetails, window, true, this.no_cashe);   
  4.     //daoger_end   
<script type="text/javascript">render_code();</script>
(5) 在能够引起行号变化的地方,添加重新设置行号的方法:
代码
  1.     this._onHeaderClick = function (e) {   
  2.         var that = this.grid;   
  3.         var el = that.getFirstParentOfType(_isIE ? event.srcElement : e.target, "TD");   
  4.         if (!(this.grid.callEvent("onHeaderClick", [el._cellIndexS, (e || event)]))) {   
  5.             return false;   
  6.         }   
  7.         if (this.grid.resized == null) {   
  8.             that.sortField(el._cellIndexS, false, el);   
  9.         }   
  10.         //daoger_start   
  11.         //reset line number of the first column after sorting   
  12.         if (lineflag) {   
  13.             that.addLineNumber();   
  14.         }   
  15.         //daoger_end   
  16.     };   
  17.     dhtmlXGridObject.prototype.addRow = function (new_id, text, ind) {   
  18.     var r = this._addRow(new_id, text, ind);   
  19.     if (!this.dragContext) {   
  20.         this.callEvent("onRowAdded", [new_id]);   
  21.     }   
  22.     this.callEvent("onRowCreated", [r.idd, r, null]);   
  23.     if (this.pagingOn) {   
  24.         this.changePage(this.currentPage);   
  25.     }   
  26.     this.setSizes();   
  27.     r._added = true;   
  28.     this.callEvent("onGridReconstructed", []);   
  29.     //daoger_start   
  30.     //reset line number of the first column after add a new row   
  31.     if (lineflag) {   
  32.         this.addLineNumber();   
  33.     }   
  34.     //daoger_end   
  35.     return r;   
  36. };   
  37. dhtmlXGridObject.prototype.deleteRow = function (row_id, node) {   
  38.     if (!node) {   
  39.         node = this.getRowById(row_id);   
  40.     }   
  41.     if (!node) {   
  42.         return;   
  43.     }   
  44.     this.editStop();   
  45.     if (this.callEvent("onBeforeRowDeleted", [row_id]) == false) {   
  46.         return false;   
  47.     }   
  48.     if (this.cellType._dhx_find("tree") != -1) {   
  49.         this._removeTrGrRow(node);   
  50.     } else {   
  51.         if (node.parentNode) {   
  52.             node.parentNode.removeChild(node);   
  53.         }   
  54.         var ind = this.rowsCol._dhx_find(node);   
  55.         if (ind != -1) {   
  56.             this.rowsCol._dhx_removeAt(ind);   
  57.         } else {   
  58.             ind = this.rowsBuffer[0]._dhx_find(row_id);   
  59.             if (ind >= 0) {   
  60.                 this.rowsBuffer[0]._dhx_removeAt(ind);   
  61.                 this.rowsBuffer[1]._dhx_removeAt(ind);   
  62.             }   
  63.         }   
  64.         this.rowsAr[row_id] = null;   
  65.     }   
  66.     for (var i = 0; i < this.selectedRows.length; i++) {   
  67.         if (this.selectedRows[i].idd == row_id) {   
  68.             this.selectedRows._dhx_removeAt(i);   
  69.         }   
  70.     }   
  71.     this.callEvent("onGridReconstructed", []);   
  72.     if (this.pagingOn) {   
  73.         this.changePage();   
  74.     }   
  75.     this.setSizes();   
  76.     //daoger_start   
  77.     //reset line number of the first column after deleting a row   
  78.     if (lineflag) {   
  79.         this.addLineNumber();   
  80.     }   
  81.     //daoger_end   
  82.     return true;   
  83. };   
<script type="text/javascript">render_code();</script>
(6) 在页面中的init()方法后设置是否添加行号标志;
代码
  1. mygrid.setLineFlag(true);  
<script type="text/javascript">render_code();</script>
还有需要说明的一点是,添加行号后可能会影响到dhtmlxgrid自己的数据存储方法,这个我还没有修改它自带的数据存储方法,以后修改了再把代码贴上来,数据显示肯定没有问题!

 

这样右键菜单添加完成,看看示例的截图吧:

myDhtmlXGrid.rar
 描述: 右键菜单
下载
 文件名: myDhtmlXGrid.rar
 文件大小: 3 KB
 下载过的: 文件被下载或查看29 次
 
dhtmlXTree进行个小的扩展 需求1: 动态生成树形菜单,每个节点都有各自的URL地址,单击不同的节点框架页的右侧跳转到该节点所对应的URL。(框架页说明:左边是树形菜单;右边是显示页面相应信息的页面) 分析: dhtmlXTree提供了很好的添加,删除节点的方法,故选择dhtmlXTree。 但是dhtmlXTree不能满足"每个节点都有各自的URL地址,单击不同的节点框架页的右侧跳转到该节点所对应的URL"这点需求,因次想到了对dhtmlXTree进行个小的扩展,即在其节点对象原有属性的基础上,再添加两个扩展属性。具体操作如下: 1、找到定义节点对象的那个函数(或方法) function dhtmlXTreeItemObject(itemId,itemText,parentObject,treeObject,actionHandler,mode) 修改为 function dhtmlXTreeItemObject(itemId,itemText,parentObject,treeObject,actionHandler,mode,url,target) 并在方法体中添加赋值语句:this.itemURL=url;this.itemTarget=target; 2、然后修改所有与dhtmlXTreeItemObject有关(直接或者间接相关)的方法: _attachChildNode,insertNewItem,insertNewChild,insertNewNext,_recreateBranch,_parseXMLTree 注:_parseXMLTree方法是与loadXML,loadXMLString相关的。 在这些方法中生成节点的语句中添加相应的参数语句,以支持新添加的属性itemURL,itemTarget。 需求2: 为dhtmlXTree树上的每个节点添加右键菜单。附:在树上的节点上点右键时才会生成菜单,空白区域单击时不会生成菜单。 分析: 1、用 dhtmlXTree + dhtmlxmenu 实现。 2、 用dhtmlxmenu生成菜单的部分代码: var menu = new dhtmlXMenuObject(); menu.setImagePath("imgs/"); menu.setIconsPath("images/"); menu.renderAsContextMenu(); menu.loadXML("dhtmlxmenu.xml?e="+new Date().getTime()); menu.addContextZone("treeboxbox_tree"); menu.addContextZone方法是为了把菜单添加到指定区域。 3、dhtmlXTreeObject.prototype._createItem方法是构造树形菜单上元素的具体实现方法。看这个方法的具体操作,可以发现它为每个节点构建了table,节点的内容(即名字)放置在个span中。 4、考虑到dhtmlxmenu实在指定的区域构建菜单,所以可以为dhtmlXTree树上的每个节点添加右键事件,在这个右键事件里获得该节点对象所对应的区域,然后在这个区域内构建Menu菜单。 难点解决方案: 1、怎样获得dhtmlXTree树上的每个节点对象所对应的区域?(dhtmlXTreeObject.prototype._createItem方法没有为这个节点的span设置id) 解决方法: 在dhtmlXTreeObject.prototype._createItem方法中添加个为span设置id的语句: 即: 在itemObject.span=document.createElement('span'); itemObject.span.className="standartTreeRow"; 后,新添加句 itemObject.span.id="treeNode_"+itemObject.id;//为这个span新增个Id属性 2、为dhtmlXTree树上的每个节点添加右键事件,在这个右键事件里获得该节点对象所对应的区域,然后在这个区域内构建Menu菜单。 解决方法: a、为dhtmlXTree树上的每个节点添加右键事件: tree.setOnRightClickHandler(treeOnRegihtClick);//右键事件 b、构建Menu菜单: var menu = new dhtmlXMenuObject(); function treeOnRegihtClick(id){ alert("右键 "+" span.id:"+tree.getItem(id).span.id); menu.setImagePath("imgs/"); menu.setIconsPath("images/"); menu.renderAsContextMenu(); menu.loadXML("dhtmlxmenu.xml?e="+new Date().getTime()); menu.addContextZone(tree.getItem(id).span.id);alert("width:"+tree.getItem(id).span.clientWidth); //var X=tree.getItem(id).span.getBoundingClientRect().left; //var Y=tree.getItem(id).span.getBoundingClientRect().top; var X=document.getElementById('mouseXPosition').value;//获得鼠标的横坐标位置 var Y=document.getElementById('mouseYPosition').value;//获得鼠标的纵坐标位置 menu.showContextMenu(X,Y);//调用showContextMenu方法显示菜单 说明:如果这儿不加上这条语句的话,第次点击右键时只能生成菜单,但是显示不出菜单,下次点击右键Menu菜单才能弹出。 //menu._showContextMenu(X,Y,tree.getItem(id).span.id); } c、用 javascript 获取当页面上鼠标(光标)位置 <script type="text/javascript"> // 说明:获取鼠标位置 function mousePosition(ev){ if(ev.pageX || ev.pageY){ return {x:ev.pageX, y:ev.pageY}; } return { x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, y:ev.clientY + document.body.scrollTop - document.body.clientTop }; } document.onmousemove = mouseMove; function mouseMove(ev){ ev = ev || window.event; var mousePos = mousePosition(ev); document.getElementById('mouseXPosition').value = mousePos.x; document.getElementById('mouseYPosition').value = mousePos.y; } </script> 页面上放置两个隐藏域存放鼠标的位置:<input type="hidden" id=mouseXPosition><input type="hidden" id=mouseYPosition>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值