Table insertRow deleteRow definitions and usage summary
- 2020-03-30 02:57:49
- OfStack
The table has several rows: var trCnt = table.rows.length; (table Id)
Each row has several columns: for (var I =0; I< TrCnt; I++)
A table. Rows [I]. Cells. Length;
Javascript operating table:
InsertRow (), deleteRow(), insertCell(), deleteCell() methods
Table. InsertRow () is fine in IE but in firefox you have to change it to table. InsertRow (-1)
And the corresponding insertCell() is also going to be insertCell(-1)
InsertRow () method
Definition and usage
The insertRow() method is used to insert a new row at the specified location in the table.
grammar
TableObject. InsertRow (index)
The return value
Returns a TableRow representing the newly inserted row.
instructions
This method creates a new TableRow object to represent a new < Tr> And inserts it at the specified location in the table.
The new row is inserted before the index row. If index is equal to the number of rows in the table, the new row is appended to the end of the table.
If the table is empty, the new row is inserted into a new < Tbody> Segment, which itself is inserted into the table.
throw
If the index parameter is less than 0 or greater than or equal to the number of rows in the table, this method throws a DOMException with the code INDEX_SIZE_ERR.
example
DeleteCell ()
Definition and usage
The deleteCell() method is used to delete cells (< Td> Elements).
grammar
TablerowObject. DeleteCell (index)
instructions
The parameter index is the position in the row of the table element to be deleted.
This method deletes the table element at the specified location in the table row.
throw
If the index parameter is less than 0 or greater than or equal to the number of elements in the row, this method throws a DOMException with the code INDEX_SIZE_ERR.
example
InsertCell ()
Definition and usage
The insertCell() method is used to insert an empty < at the specified position on a row in the HTML table. Td> Elements.
grammar
TablerowObject. InsertCell (index)
The return value
A TableCell object representing a newly created and inserted object. Td> Elements.
instructions
This method will create a new < Td> Element, inserting it into the specified position in the row. The new cell will be inserted before the table element currently at the position specified by index. If index is equal to the number of cells in the row, the new cell is appended to the end of the row.
Note that this method can only insert < Td> Data table element. To add a header element to a row, you must create and insert an < with the document.createelement () method and the node.insertbefore () method (or a related method). Th> Elements.
throw
If the index parameter is less than 0 or greater than or equal to the number of elements in the row, this method throws a DOMException with the code INDEX_SIZE_ERR.
example
DeleteCell ()
Definition and usage
The deleteCell() method is used to delete cells (< Td> Elements).
grammar
TablerowObject. DeleteCell (index)
instructions
The parameter index is the position in the row of the table element to be deleted.
This method deletes the table element at the specified location in the table row.
throw
If the index parameter is less than 0 or greater than or equal to the number of elements in the row, this method throws a DOMException with the code INDEX_SIZE_ERR.
example
Application in the project:
Each row has several columns: for (var I =0; I< TrCnt; I++)
A table. Rows [I]. Cells. Length;
Javascript operating table:
InsertRow (), deleteRow(), insertCell(), deleteCell() methods
Table. InsertRow () is fine in IE but in firefox you have to change it to table. InsertRow (-1)
And the corresponding insertCell() is also going to be insertCell(-1)
InsertRow () method
Definition and usage
The insertRow() method is used to insert a new row at the specified location in the table.
grammar
TableObject. InsertRow (index)
The return value
Returns a TableRow representing the newly inserted row.
instructions
This method creates a new TableRow object to represent a new < Tr> And inserts it at the specified location in the table.
The new row is inserted before the index row. If index is equal to the number of rows in the table, the new row is appended to the end of the table.
If the table is empty, the new row is inserted into a new < Tbody> Segment, which itself is inserted into the table.
throw
If the index parameter is less than 0 or greater than or equal to the number of rows in the table, this method throws a DOMException with the code INDEX_SIZE_ERR.
example
<html>
< head>
< script type="text/javascript">
function insRow()
{
document.getElementById('myTable').insertRow(0)
}
< /script>
< /head>
< body>
< table id="myTable" border="1">
< tr>
< td>Row1 cell1</td>
< td>Row1 cell2</td>
< /tr>
< tr>
< td>Row2 cell1</td>
< td>Row2 cell2</td>
< /tr>
< /table>
< br />
< input type="button" onclick="insRow()"
value="Insert new row">
< /body>
< /html>
DeleteCell ()
Definition and usage
The deleteCell() method is used to delete cells (< Td> Elements).
grammar
TablerowObject. DeleteCell (index)
instructions
The parameter index is the position in the row of the table element to be deleted.
This method deletes the table element at the specified location in the table row.
throw
If the index parameter is less than 0 or greater than or equal to the number of elements in the row, this method throws a DOMException with the code INDEX_SIZE_ERR.
example
<html>
< head>
< script type="text/javascript">
function delRow()
{
document.getElementById('myTable').deleteRow(0)
}
< /script>
< /head>
< body>
< table id="myTable" border="1">
< tr>
< td>Row1 cell1</td>
< td>Row1 cell2</td>
< /tr>
< tr>
< td>Row2 cell1</td>
< td>Row2 cell2</td>
< /tr>
< /table>
< br />
< input type="button" onclick="delRow()"
value="Delete first row">
< /body>
< /html>
InsertCell ()
Definition and usage
The insertCell() method is used to insert an empty < at the specified position on a row in the HTML table. Td> Elements.
grammar
TablerowObject. InsertCell (index)
The return value
A TableCell object representing a newly created and inserted object. Td> Elements.
instructions
This method will create a new < Td> Element, inserting it into the specified position in the row. The new cell will be inserted before the table element currently at the position specified by index. If index is equal to the number of cells in the row, the new cell is appended to the end of the row.
Note that this method can only insert < Td> Data table element. To add a header element to a row, you must create and insert an < with the document.createelement () method and the node.insertbefore () method (or a related method). Th> Elements.
throw
If the index parameter is less than 0 or greater than or equal to the number of elements in the row, this method throws a DOMException with the code INDEX_SIZE_ERR.
example
<html>
< head>
< script type="text/javascript">
function insCell()
{
var x=document.getElementById('tr2').insertCell(0)
x.innerHTML="John"
}
< /script>
< /head>
< body>
< table border="1">
< tr id="tr1">
< th>Firstname</th>
< th>Lastname</th>
< /tr>
< tr id="tr2">
< td>Peter</td>
< td>Griffin</td>
< /tr>
< /table>
< br />
< input type="button" onclick="insCell()" value="Insert cell">
< /body>
< /html>
DeleteCell ()
Definition and usage
The deleteCell() method is used to delete cells (< Td> Elements).
grammar
TablerowObject. DeleteCell (index)
instructions
The parameter index is the position in the row of the table element to be deleted.
This method deletes the table element at the specified location in the table row.
throw
If the index parameter is less than 0 or greater than or equal to the number of elements in the row, this method throws a DOMException with the code INDEX_SIZE_ERR.
example
<html>
< head>
< script type="text/javascript">
function delCell()
{
document.getElementById('tr2').deleteCell(0)
}
< /script>
< /head>
< body>
< table border="1">
< tr id="tr1">
< th>Firstname</th>
< th>Lastname</th>
< /tr>
< tr id="tr2">
< td>Peter</td>
< td>Griffin</td>
< /tr>
< /table>
< br />
< input type="button" onclick="delCell()" value="Delete cell">
< /body>
< /html>
Application in the project:
<script type="text/javascript">
var trIndex = 0;
//Dynamically add rows
unction appendConvert(){
//var sel = document.getElementById("selectConvertName");
var sel = document.getElementsByName("selectConvertName")[0];
var className;
if(null!=sel){
for(var i = 0; i < sel.options.length; i++){
if(sel.options[i].selected)
className=sel.options[i].value;
}
}
//The data comes from ajax, in json form.
convert.getConvertBean2Json(className,
function(result) {
var obj = eval('('+result+')');
var table = document.getElementById("convertTable");
var newRow = table.insertRow(trIndex+1);
newRow.insertCell(0).innerHTML = obj.name+"<input type='button' value=' delete ' onclick='deleteRow(this)'>";
newRow.insertCell(1).innerHTML = "<input type='text' name='convertList["+trIndex+"].id'><input type='hidden' name='convertList["+trIndex+"].name' value='"+obj.name+"'>";
if(null!=obj.paramList){
var paramStr = "";
for(var i = 0; i < obj.paramList.length; i++){
paramStr = paramStr+
" Parameter name: "+obj.paramList[i].name+
" ; Parameter type: "+obj.paramList[i].type+
" ; Parameter values: <input name='convertList["+trIndex+"].paramList["+i+"].value' type='text'><br>"+
"<input type='hidden' name='convertList["+trIndex+"].paramList["+i+"].name' value='"+obj.paramList[i].name+"'>"+
"<input type='hidden' name='convertList["+trIndex+"].paramList["+i+"].type' value='"+obj.paramList[i].type+"'>";
}
newRow.insertCell(2).innerHTML = paramStr;
}
trIndex++;
});
}
//Delete rows
on deleteRow(r){
var i=r.parentNode.parentNode.rowIndex;
document.getElementById('convertTable').deleteRow(i);
trIndex--;
}
</script>