P
philin007
Hi Guys,
Could any one help me out with codes to add rows to a table. Well I
kinda of got the codes from the following site
(http://www.interviewboard.com/DHTMLSamples/DHTMLGridSample.htm) the
problem is this example only shows input box. Now i have a list that
means a 'select box' for which the value is retrieved from Database and
populated into the select list box. Now according to the example if all
residing in the javascript how am i gng to do that!. For example I have
the default row which has 3 text box, a select box(populated from
database and a delete button). Outside the table i have add row
When i click on add row another row should be created with 3 text box,
a select box and a delete button. all these input box and select box
whould have unique names cas i need to pass them to another page to get
the values.
following is the code that I have written :
-***************************************
<script language="javascript">
//add a new row to the table
function addRow()
{
var tbl = document.getElementById("tblGrid");
var nextRow = tbl.tBodies[0].rows.length;
if (nextRow > 5)
{
return;
}
else
{
//add a row to the rows collection and get a reference to the newly
added row
var newRow = document.all("tblGrid").insertRow();
alert ('nextrow' + nextRow);
var popCal1 = 'javascript:cal1' + nextRow + '.popup();'
var popCal2 = 'javascript:cal2' + nextRow + '.popup();'
var nameBegDt = 'txtBeginDt'+ nextRow
var nameEndDt = 'txtEndDt'+ nextRow
var nameSubTyp = 'selSubTyp'+ nextRow
var nameLeaveAppl = 'txtLeaveAppl'+ nextRow
//add 3 cells (<td>) to the new row and set the innerHTML to contain
text boxes
var oCell = newRow.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><input
type='text' SIZE='10' name='" + nameBegDt + "'
onFocus='this.blur();'/><a href='" + popCal1 + "'><img
src='img/cal.gif' width='16' height='16' border='0' alt='Click Here to
Pick up the date'></a></TD>";
oCell = newRow.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><input
type='text' SIZE='10' name='" + nameEndDt + "'
onFocus='this.blur();'/><a href='" + popCal2 + "'><img
src='img/cal.gif' width='16' height='16' border='0' alt='Click Here to
Pick up the date'></a></TD>";
oCell = newRow.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><select
name='"+ nameSubTyp +"' >--Select--<option value='F'></option><option
value='AM'>AM (Half)</option><option value='PM'>PM
(Half)</option><option value='S'>Sat Leave</option><option
value='T'>1.5 days</option></select></TD>";
oCell = newRow.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><input
SIZE='4' maxlength='4' type='text' name='" + nameLeaveAppl + "'></TD>";
oCell = newRow.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><input
type='image' src='images/delete.jpg'
onclick='removeRow(this);'/></TD>";
}
}
//deletes the specified row from the table
function removeRow(src)
{
/* src refers to the input button that was clicked.
to get a reference to the containing <tr> element,
get the parent of the parent (in this case case <tr>)
*/
var tbl = document.getElementById("tblGrid");
var nextRow = tbl.tBodies[0].rows.length;
alert("here:" + nextRow);
if (nextRow < 3)
{
return;
}
else
{
var oRow = src.parentElement.parentElement;
//once the row reference is obtained, delete it passing in its
rowIndex
document.all("tblGrid").deleteRow(oRow.rowIndex);
}
}
</script>
****************************************************
can anyone help please.
Thanks in advance
Regards
philip
Could any one help me out with codes to add rows to a table. Well I
kinda of got the codes from the following site
(http://www.interviewboard.com/DHTMLSamples/DHTMLGridSample.htm) the
problem is this example only shows input box. Now i have a list that
means a 'select box' for which the value is retrieved from Database and
populated into the select list box. Now according to the example if all
residing in the javascript how am i gng to do that!. For example I have
the default row which has 3 text box, a select box(populated from
database and a delete button). Outside the table i have add row
When i click on add row another row should be created with 3 text box,
a select box and a delete button. all these input box and select box
whould have unique names cas i need to pass them to another page to get
the values.
following is the code that I have written :
-***************************************
<script language="javascript">
//add a new row to the table
function addRow()
{
var tbl = document.getElementById("tblGrid");
var nextRow = tbl.tBodies[0].rows.length;
if (nextRow > 5)
{
return;
}
else
{
//add a row to the rows collection and get a reference to the newly
added row
var newRow = document.all("tblGrid").insertRow();
alert ('nextrow' + nextRow);
var popCal1 = 'javascript:cal1' + nextRow + '.popup();'
var popCal2 = 'javascript:cal2' + nextRow + '.popup();'
var nameBegDt = 'txtBeginDt'+ nextRow
var nameEndDt = 'txtEndDt'+ nextRow
var nameSubTyp = 'selSubTyp'+ nextRow
var nameLeaveAppl = 'txtLeaveAppl'+ nextRow
//add 3 cells (<td>) to the new row and set the innerHTML to contain
text boxes
var oCell = newRow.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><input
type='text' SIZE='10' name='" + nameBegDt + "'
onFocus='this.blur();'/><a href='" + popCal1 + "'><img
src='img/cal.gif' width='16' height='16' border='0' alt='Click Here to
Pick up the date'></a></TD>";
oCell = newRow.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><input
type='text' SIZE='10' name='" + nameEndDt + "'
onFocus='this.blur();'/><a href='" + popCal2 + "'><img
src='img/cal.gif' width='16' height='16' border='0' alt='Click Here to
Pick up the date'></a></TD>";
oCell = newRow.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><select
name='"+ nameSubTyp +"' >--Select--<option value='F'></option><option
value='AM'>AM (Half)</option><option value='PM'>PM
(Half)</option><option value='S'>Sat Leave</option><option
value='T'>1.5 days</option></select></TD>";
oCell = newRow.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><input
SIZE='4' maxlength='4' type='text' name='" + nameLeaveAppl + "'></TD>";
oCell = newRow.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><input
type='image' src='images/delete.jpg'
onclick='removeRow(this);'/></TD>";
}
}
//deletes the specified row from the table
function removeRow(src)
{
/* src refers to the input button that was clicked.
to get a reference to the containing <tr> element,
get the parent of the parent (in this case case <tr>)
*/
var tbl = document.getElementById("tblGrid");
var nextRow = tbl.tBodies[0].rows.length;
alert("here:" + nextRow);
if (nextRow < 3)
{
return;
}
else
{
var oRow = src.parentElement.parentElement;
//once the row reference is obtained, delete it passing in its
rowIndex
document.all("tblGrid").deleteRow(oRow.rowIndex);
}
}
</script>
****************************************************
can anyone help please.
Thanks in advance
Regards
philip