function ediTable() { this.init_done = false; this.custom = false; this.table; this.tbody; this.input_names; this.columns = 0; this.rows = 0; var self = this; function update_names() { var rows = self.tbody.getElementsByTagName('TR'); for (var i = 0; i < rows.length; i++) { var inputs = rows[i].getElementsByTagName('INPUT'); for (var j = 0; j < inputs.length; j++) { inputs[j].name = self.input_names + '[' + i + ']' + '[' + j + ']'; } } } this.init = function (id, custom, input_names) { if (self.init_done) return; self.custom = custom; self.init_done = true; self.input_names = (input_names) ? input_names : id; self.table = document.getElementById(id); self.tbody = self.table.getElementsByTagName('TBODY'); self.tbody = (self.tbody[0]) ? self.tbody[0] : self.table; if (!self.table) return false; var rows = this.tbody.getElementsByTagName('TR'); self.rows = rows.length; for (var i = 0; i < rows.length; i++) { var tag_name = (i == 0) ? 'TH' : 'TD'; var cells = rows[i].getElementsByTagName(tag_name); if (i == 0) self.columns = cells.length; var cell = document.createElement(tag_name); var img = document.createElement('IMG'); cell.width = '20'; cell.align = 'center'; img.src = (i == 0) ? 'img/table_row_insert.png' : 'img/table_row_delete.png'; img.onclick = (i == 0) ? function () { self.add_row() } : function () { self.delete_row(this) } img.title = (i == 0) ? 'Добавить рядок' : 'Удалить рядок'; cell.appendChild(img); rows[i].insertBefore(cell, cells[0]); if (i == 0 && !self.custom) continue; for (var j = 1; j < cells.length; j++) { var title = cells[j].innerHTML; var input = document.createElement('INPUT'); //input.name = self.input_names + '[' + i + ']' + '[]'; input.type = 'text'; input.value = title; while (cells[j].firstChild) { cells[j].removeChild(cells[j].firstChild); } cells[j].appendChild(input); } if (self.custom) { for (var n = 0; n < 2; n++) { var cell = document.createElement(tag_name); if (i == 0) { var img = document.createElement('IMG'); cell.width = '20'; cell.align = 'center'; img.src = (n == 0) ? 'img/add.png' : 'img/delete.png'; img.onclick = (n == 0) ? function () { self.add_column() } : function () {self.delete_column()} img.title = (n == 0) ? 'Добавить колонку' : 'Удалить колонку'; cell.appendChild(img); } else { cell.innerHTML = ' '; } rows[i].appendChild(cell); } } } update_names(); } this.add_row = function () { self.rows++; var tr = document.createElement('TR'); var td = document.createElement('TD'); var img = document.createElement('IMG'); img.src = 'img/table_row_delete.png'; img.onclick = function () { self.delete_row(this) } td.align = 'center'; td.appendChild(img); tr.appendChild(td); for (var i = 0; i < self.columns; i++) { var td = document.createElement('TD'); var input = document.createElement('INPUT'); //input.name = self.input_names + '[' + self.rows + ']' + '[]'; input.type = 'text'; td.appendChild(input); tr.appendChild(td); } if (self.custom) { for (var i = 0; i < 2; i++) { var td = document.createElement('TD'); td.innerHTML = ' '; tr.appendChild(td); } } self.tbody.appendChild(tr); update_names(); } this.delete_row = function (o) { var tr = o.parentNode; var i = 10; // To avoid endless while... while (tr.nodeName != 'TR' && i > 0) { var tr = tr.parentNode; i--; } tr.parentNode.removeChild(tr); update_names(); } this.add_column = function () { var rows = this.tbody.getElementsByTagName('TR'); for (var i = 0; i < rows.length; i++) { var tag_name = (i == 0) ? 'TH' : 'TD'; var cell = document.createElement(tag_name); var cells = rows[i].getElementsByTagName(tag_name); var input = document.createElement('INPUT'); input.type = 'text'; cell.appendChild(input); rows[i].insertBefore(cell, cells[this.columns + 1]); } this.columns++; update_names(); } this.delete_column = function () { var rows = this.tbody.getElementsByTagName('TR'); for (var i = 0; i < rows.length; i++) { var tag_name = (i == 0) ? 'TH' : 'TD'; var cells = rows[i].getElementsByTagName(tag_name); //cells[this.columns + 1].parentNode.removeChild(cells[this.columns + 1]); rows[i].removeChild(cells[this.columns]); continue; } this.columns--; update_names(); } }