用javascript交换行 [英] swapping rows using a javascript

查看:197
本文介绍了用javascript交换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个for条件生成的多个行,上下有2个图标。点击向上选择的行应该向上移动一下,点击向下选择的行应该向下移动一步

I've a multiple number of rows which is generated by a for condition, with 2 icons up and down. On click of up the selected row should move one step up, and on click of down the selected row should move one step down

<html>
<head>
<body>
<form name="myForm">

 <table id="mainTable" border="1" width="100%">

   <script>
     document.write('<tr>')
     document.write('<td>')
     document.write('row1')
     document.write('</td>')
     document.write('</tr>')

     document.write('<tr>')
     document.write('<td>')
     document.write('row2')
     document.write('</td>')
     document.write('</tr>')
     document.write('</table>')

     document.write('<table>')
     document.write('<tr>')
     document.write('<td>')
     document.write('<input type="button" value=" move UP " onClick="swapRowUp(getRowIndex(this))"</input>')>
     document.write('<input type="button" value="move DOWN" onClick="swapRowDown(getRowIndex(this))"</input>')>
     document.write('</td>')
     document.write('</tr>')
     document.write('</table>')
</script>
 </table>

</form>
</body>
</head>
</html>
<script>
var mainTable = document.getElementById("mainTable"); 
function getRowIndex(el)
{
while( (el = el.parentNode) && el.nodeName.toLowerCase() !== 'tr' );

   if( el ) 
        alert(el.rowIndex);
        return el.rowIndex;
}

function swapRowUp(chosenRow) {
 if (chosenRow.rowIndex != 0) {
   moveRow(chosenRow, chosenRow.rowIndex-1); 
 }
} 
function swapRowDown(chosenRow) {
 if (chosenRow.rowIndex != mainTable.rows.length-1) {
   moveRow(chosenRow, chosenRow.rowIndex+1); 
 }
} 

function moveRow(targetRow, newIndex) {
if (newIndex > targetRow.rowIndex) {
   newIndex++; 
 }

 var mainTable = document.getElementById('mainTable'); 

 var theCopiedRow = mainTable.insertRow(newIndex); 


 for (var i=0; i<targetRow.cells.length; i++) {
   var oldCell = targetRow.cells[i]; 
   var newCell = document.createElement("TD"); 
   newCell.innerHTML = oldCell.innerHTML; 
   theCopiedRow.appendChild(newCell); 
   copyChildNodeValues(targetRow.cells[i], newCell);
 } 
//delete the old row 
 mainTable.deleteRow(targetRow.rowIndex); 
} 


function copyChildNodeValues(sourceNode, targetNode) {
 for (var i=0; i < sourceNode.childNodes.length; i++) {
   try{
     targetNode.childNodes[i].value = sourceNode.childNodes[i].value;
   }
   catch(e){

   }
 }
}

</script>

我无法执行交换操作,我将cellIndex设为null

i'm unable to perform the swap operation, i get cellIndex as null

推荐答案

您可以使用一些明智的JavaScript

You can use some sensible JavaScript

up.addEventListener("click", function moveRowUp() {
    var row = this.parentNode.parentNode,
        sibling = row.previousElementSibling,
        parent = row.parentNode;

    parent.insertBefore(row, sibling);
});

为一些合理的HTML

for some sensible HTML

<table>
    <tbody>
        <tr> 
            <td> 1 </td> 
            <td> filler </td> 
        </tr>
        <tr> 
            <td> 2 </td> 
            <td> <button id="up">up</button> </td> 
        </tr>
    </tbody>
</table>

现场示例

这篇关于用javascript交换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆