/** * Created by lenovo on 2017/1/24. */ /* * id:当前这个表格的id * hiddenRowArr:表示也隐藏的列的arr * selectMore:true||false默认值为false * */ function eventTable(id, hiddenRowArr, selectMore) { var self = this; this.id = document.getElementById(id); this.hiddenRowArr = hiddenRowArr || null; this.selectMore = selectMore || false; if (this.id) { this.onselectstart = function () { return false }; this._init(this.id); } } eventTable.prototype = { _init: function (id) { this._formatTable(id); id.className = "eventTable"; this.attachEvent() this._format(); }, _formatTable: function (tableObject, border, cellSpacing, cellPadding, align) { if (border == null && isNaN(parseInt(border))) { border = 0; } else { border = parseInt(border); } if (cellSpacing == null && isNaN(parseInt(cellSpacing))) { cellSpacing = 0 } else { cellSpacing = parseInt(cellSpacing); } if (cellPadding == null && isNaN(parseInt(cellPadding))) { cellPadding = 0 } else { cellPadding = parseInt(cellPadding); } tableObject.border = border; tableObject.cellPadding = cellSpacing; tableObject.cellSpacing = cellPadding; if (align) tableObject.align = align; }, _format: function () { var rowCount = this.id.rows.length; var tbHead = this.id.rows[0]; var cellCount = null; if (0 in tbHead.cells) { if (tbHead.cells[0].tagName.toUpperCase() == "TH") { } } for (var i = 1; i < rowCount; i++) { if (cellCount == null) { cellCount = this.id.rows[i].cells.length; } for (var cell = 0; cell < cellCount; cell++) { this.id.rows[i].cells[cell].className = ‘item-‘ + ((i + 1) % 2); } } }, //event attachEvent: function (event) { var self = this; var event = window.event || event; var rows = this.id.rows; var rowlength = rows.length; for (var i = 0; i < rowlength; i++) { rows[i].onclick = function () { self.selectRow(this) } rows[i].ondblclick = function () { self.DbClick(this) } } }, DbClick: function (row) { alert(111); }, Eclick: function () { return true }, selectRow: function (row) { var self = this; if (this.selectMore) { if (!this.RowArr) { this.RowArr = [] } if (this.judgeTrExit(row)) { this.RowArr.push(row); } if (this.RowArr.length == 0) { this.RowArr.push(row); } for (var i = 0, l = row.cells.length; i < l; i++) { if (row.cells[i].className.indexOf("selected") > -1) { row.cells[i].className = row.cells[i].className; } else { row.cells[i].className = ‘selected ‘ + row.cells[i].className; } } } else { this.RowArr = []; this.RowArr.push(row); for (var rows = 1, rowsl = this.id.rows.length; rows < rowsl; rows++) { for (var cells = 0, cellsL = this.id.rows[rows].cells.length; cells < cellsL; cells++) { self.delectClass(self.id.rows[rows].cells[cells], "selected") } } for (var i = 0, l = row.cells.length; i < l; i++) { if (row.cells[i].className.indexOf("selected") > -1) { row.cells[i].className = row.cells[i].className; } else { row.cells[i].className = ‘selected ‘ + row.cells[i].className; } } } }, judgeTrExit: function (row) { for (var j = 0; j < this.RowArr.length; j++) { if (this.RowArr[j].rowIndex == row.rowIndex) { return false; } } return true }, delectClass: function (el, css) { var attr = ""; var classNames = el.className.split(/\s+/); console.log(classNames) for (var i = 0, l = classNames.length; i < l; i++) { if (classNames[i]) { if (!(css == classNames[i])) { attr += classNames[i] + " "; } } } console.log(attr) el.className = attr; }, operateRow: function (canshu) { // switch (canshu) { case "up": this.upRow(); break; case "down": this.downRow(); break; case "delectRow": this.decectRow(); } }, upRow: function () { var row = this.RowArr[0] console.log("aaa") }, decectRow: function () { var row = this.RowArr[0]; this.id.deleteRow(row.rowIndex); this._format(); }, downRow: function () { } }
原文:http://www.cnblogs.com/heyinwangchuan/p/6347901.html