function dynamicTable(tableID,fields,fieldStyles,allowRemove,varName) { this.table = document.getElementById(tableID); this.fields = fields; this.fieldStyles = fieldStyles; this.allowRemove = allowRemove; this.numRows = 0; this.varName = varName; this.addRowIndex = 0; this.addRow = dtAddRow; this.dtAddRow = dtAddRow; this.dtRemoveRow = dtRemoveRow; this.dtRemoveAll = dtRemoveAll; this.dtCountDynRows = dtCountDynRows; this.countDynRows = dtCountDynRows; this.removeRow = dtRemoveRow; this.getCell = dtGetCell; this.replaceHTMLInRow = dtReplaceHTMLInRow; this.replaceInputNamesInRow = dtReplaceInputNamesInRow; this.rFields = new Array(); //needed to lookup cell index by field name for(i=0;i 1) { ret[ret.length] = new Array(); } for(x = 0; x < Math.min(limitPerCell, inputs.length); x++) { if(inputs[x]) { if(limitPerCell > 1) { ret[ret.length-1][x] = inputs[x]; } else { ret[ret.length] = inputs[x]; } } } } } } } } return ret; }; } function dtSetAddRowIndex(idx) { this.addRowIndex = 0; } function dtAddRow(values) { this.table.style.display='table'; this.numRows++; row = this.table.insertRow(this.table.rows.length + this.addRowIndex); cells = new Array(); for(i=0;i < this.fields.length;i++) { field = this.fields[i]; cell = cells[field] = row.insertCell(i); cell.id = this.varName+'_cell_'+this.numRows+'_'+field; cell.style.verticalAlign = 'top'; cell.style.textAlign = 'center'; if(style = this.fieldStyles[field]) { for(stylename in style) { cell.style[stylename] = style[stylename]; } } if(values[field]) { cell.innerHTML = values[field]; } } if(this.allowRemove) { cell = row.insertCell(i); cell.innerHTML = "" + "" +""; } } function dtRemoveRow(i) { this.table.deleteRow(i); this.numRows = this.table.rows.length - 1; } function dtRemoveAll() { while(this.numRows > 0) { this.table.deleteRow(this.numRows); this.numRows = this.table.rows.length - 1; } } /*function dtGetCellRefs(column) { if(cellindex = this.rFields[column]) { return this.table.rows[rowIndex].cells[cellindex]; } }*/ function dtGetCellContent(column, rowIndex) { if(cellindex = this.rFields[column]) { return this.table.rows[rowIndex].cells[cellindex].innerHTML; } } function dtCountDynRows() { return (this.table.rows.length + this.addRowIndex - 1); } function dtReplaceHTMLInRow(rowIdx, source, target) { if(row = this.table.rows[rowIdx]) { for(i=0; i < row.cells.length; i++) { oldHtml = row.cells[i].innerHTML; row.cells[i].innerHTML = row.cells[i].innerHTML.replace(source,target); //alert('(' + rowIdx + ') (' + source + ' => ' + target + ') ' + oldHtml + ' => ' + row.cells[i].innerHTML); } } } function dtReplaceInputNamesInRow(rowIdx, source, target) { if(row = this.table.rows[rowIdx]) { for(i=0; i < row.cells.length; i++) { inputs = row.cells[i].getElementsByTagName('input'); for(j=0;j < inputs.length;j++) { oldname = inputs[j].name; oldid = inputs[j].id; inputs[j].name = inputs[j].name.replace(source,target); inputs[j].id = inputs[j].id.replace(source,target); //alert(source + ' => ' + target + "\n" + oldid + ' => ' + inputs[j].id); } buttons = row.cells[i].getElementsByTagName('button'); for(j=0;j < buttons.length;j++) { buttons[j].id = buttons[j].id.replace(source,target); atts = buttons[j].attributes; onclickval = ''; for(a = 0; a < atts.length;a++) { if(atts[a].name == 'onclick') { onclickval = atts[a].value; } } buttons[j].setAttribute('onclick',onclickval.replace(source,target)); } } } } function dtGetCell(column, rowIndex) { if((cellindex = this.rFields[column]) != null) { return this.table.rows[rowIndex].cells[cellindex]; } }