2010-12-01 13 views

答えて

6

jQueryを試してみてください。あなたは次のようなことができます:

<table id="mytable"> 
    <tr> 
     <td>row 1</td> 
     <td><input type="button" value="move up" class="move up" /></td> 
     <td><input type="button" value="move down" class="move down" /></td> 
    </tr> 
    ... 
</table> 

$('#mytable input.move').click(function() { 
    var row = $(this).closest('tr'); 
    if ($(this).hasClass('up')) 
     row.prev().before(row); 
    else 
     row.next().after(row); 
}); 

それを見てくださいhere

また、jQueryのsortableを試すこともできます。much easierです。

2
$sel = "SELECT emp_name from emps"; 
$res = mysql_query($sel); 
?> 
<form id="ffff"> 
<table border="1"> 
<?php 
     $c =0; 
     while($row = mysql_fetch_object($res)) { 
     $c++; 
?> 
     <tr> 
      <td><div id="D<?php echo $c; ?>R"><?php echo $row->emp_name; ?></div></td> 
      <td><input type="button" id="D<?php echo $c; ?>" value="Down" class="move down" onClick="MoveDown('D<?php echo $c; ?>','D<?php echo $c+1; ?>');"/></td> 
      <td><input type="button" id="U<?php echo $c; ?>" value="Up" class="move up" onClick="MoveUp('D<?php echo $c; ?>','D<?php echo $c-1; ?>');"/></td> 
     </tr> 
<?php 
     } 
?> 
</table> 
</form> 
<script type="text/javascript"> 
    function MoveDown(roww,rowwN) 
    { 
     divv = document.getElementById(roww+'R').innerHTML; 
     if(roww) 
     { 
     ch = document.getElementById(rowwN+'R').innerHTML; 
     document.getElementById(rowwN+'R').innerHTML = document.getElementById(roww+'R').innerHTML; 
     document.getElementById(roww+'R').innerHTML = ch; 
     } 
    } 
    function MoveUp(roww,rowwN) 
    { 
     divv = document.getElementById(roww+'R').innerHTML; 
     if(roww) 
     { 
     ch = document.getElementById(rowwN+'R').innerHTML; 
     document.getElementById(rowwN+'R').innerHTML = document.getElementById(roww+'R').innerHTML; 
     document.getElementById(roww+'R').innerHTML = ch; 
     } 
    } 
</script> 
1
<table id="tableID" border="1" width="400"> 
     <tr id="1"> 
       <td>1</td><td>One<input type="button" class="up" value="up"/><input type="button" class="down" value="down"/></td> 
     </tr> 
     <tr id="2"> 
       <td>2</td><td>Two<input type="button" class="up" value="up"/><input type="button" class="down" value="down"/></td> 
     </tr> 
     <tr id="3"> 
       <td>3</td><td>Three<input type="button" class="up" value="up"/><input type="button" class="down" value="down"/></td> 
     </tr> 

</table> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    $("#tableID").delegate('input.up','click', function(e) { 

     var it = $(this).closest('tr'); 
     var prev = $(this).closest('tr').prev('tr'); 

     if(it.attr("id") != $("tr:first").attr("id")){ 
      it.remove(); 
      it.insertBefore(prev); 
     } 
    }); 
    $("#tableID").delegate('input.down','click', function(e) { 

     var it = $(this).closest('tr'); 
     var next = $(this).closest('tr').next('tr'); 

     if(it.attr("id") != $("tr:last").attr("id")){ 
      it.remove(); 
      it.insertAfter(next); 
     }   
    }); 
}); 
</script>