今天偶然發現表格隱藏行和列挺好用的,覺得還不錯,下面是隱藏和顯示table行、隱藏table列(顯示的類同)的js函數,直接調用這些函數就可以實現表格行和列的顯示和隱藏。
1.顯示行
function setShowRow(oTable,iRow){
oTable.rows[iRow].style.display = oTable.rows[iRow].style.display = "block";
}
2.隱藏行
function setHiddenRow(oTable,iRow){
oTable.rows[iRow].style.display = oTable.rows[iRow].style.display = "none";
}
3.隱藏列
function setHiddenCol(oTable,iCol)
{
for (i=0;i < oTable.rows.length ; i++)
{
oTable.rows[i].cells[iCol].style.display =
oTable.rows[i].cells[iCol].style.display=="none";
//如果該列隱藏則讓其顯示,反之則讓其隱藏
//oTable.rows[i].cells[iCol].style.display=="none"?"block":"none";
}
}
oTable爲表的id,iRow和iCol是從0開始的,iRow=0表示的是第一行,iCol=0表示的是第一列。
例如:
<table>
<tr>
<td width="15%">Country</td>
<td width="35%" height="30"><select name="type" id="type"onchange="selectType();" style="height:20;width:150" >
<option value="China" <c:if test="${type == 'China'}">selected</c:if>>中國</option>
<option value="A" <c:if test="${type == 'A'}">selected</c:if>>美國</option>
</td>
</tr>
</table>
<table id="table1" width="100%">
<tr>
<td width="15%">時間</td>
<td width="35%" height="30"><s:textfield id="time" name="time" maxlength="20" cssStyle="width:150" /></td>
</tr>
<tr>
<td width="15%">地點</td>
<td width="35%" height="30"><s:select id="address" name="address" list="addressList" onblur="findStorage()" cssStyle="height:20;width:150" />
<td width="15%">人物</td>
<td width="35%" height="30"><s:textfield id="person" name="person" cssStyle="width:150" disabled="true" /></td>
</tr>
</table>
<script type="text/javascript">
//顯示某一行
function setShowRow(oTable,iRow){
oTable.rows[iRow].style.display = oTable.rows[iRow].style.display = "block";
}
//隱藏某一行
function setHiddenRow(oTable,iRow){
oTable.rows[iRow].style.display = oTable.rows[iRow].style.display = "none";
}
//選擇某一類型時,顯示所要顯示的行
function selectType(){
var obj = $('type').value;
var oTable = document.getElementByIdx_x('Table1');
if(obj=='China'){
setHiddenRow(oTable,0);//隱藏id=Table1的第1行數據
setShowRow(oTable,1);//顯示id=Table1的第2行數據
}
if(obj=='A'){
setShowRow(oTable,0);//顯示id=Table1的第1行數據
setHiddenRow(oTable,1);//隱藏id=Table1的第2行數據
}
}
1.顯示行
function setShowRow(oTable,iRow){
oTable.rows[iRow].style.display = oTable.rows[iRow].style.display = "block";
}
2.隱藏行
function setHiddenRow(oTable,iRow){
oTable.rows[iRow].style.display = oTable.rows[iRow].style.display = "none";
}
3.隱藏列
function setHiddenCol(oTable,iCol)
{
for (i=0;i < oTable.rows.length ; i++)
{
oTable.rows[i].cells[iCol].style.display =
oTable.rows[i].cells[iCol].style.display=="none";
//如果該列隱藏則讓其顯示,反之則讓其隱藏
//oTable.rows[i].cells[iCol].style.display=="none"?"block":"none";
}
}
oTable爲表的id,iRow和iCol是從0開始的,iRow=0表示的是第一行,iCol=0表示的是第一列。
例如:
<table>
<tr>
<td width="15%">Country</td>
<td width="35%" height="30"><select name="type" id="type"onchange="selectType();" style="height:20;width:150" >
<option value="China" <c:if test="${type == 'China'}">selected</c:if>>中國</option>
<option value="A" <c:if test="${type == 'A'}">selected</c:if>>美國</option>
</td>
</tr>
</table>
<table id="table1" width="100%">
<tr>
<td width="15%">時間</td>
<td width="35%" height="30"><s:textfield id="time" name="time" maxlength="20" cssStyle="width:150" /></td>
</tr>
<tr>
<td width="15%">地點</td>
<td width="35%" height="30"><s:select id="address" name="address" list="addressList" onblur="findStorage()" cssStyle="height:20;width:150" />
<td width="15%">人物</td>
<td width="35%" height="30"><s:textfield id="person" name="person" cssStyle="width:150" disabled="true" /></td>
</tr>
</table>
<script type="text/javascript">
//顯示某一行
function setShowRow(oTable,iRow){
oTable.rows[iRow].style.display = oTable.rows[iRow].style.display = "block";
}
//隱藏某一行
function setHiddenRow(oTable,iRow){
oTable.rows[iRow].style.display = oTable.rows[iRow].style.display = "none";
}
//選擇某一類型時,顯示所要顯示的行
function selectType(){
var obj = $('type').value;
var oTable = document.getElementByIdx_x('Table1');
if(obj=='China'){
setHiddenRow(oTable,0);//隱藏id=Table1的第1行數據
setShowRow(oTable,1);//顯示id=Table1的第2行數據
}
if(obj=='A'){
setShowRow(oTable,0);//顯示id=Table1的第1行數據
setHiddenRow(oTable,1);//隱藏id=Table1的第2行數據
}
}