[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]jquery动态调整表格行的顺序,上移,下移~~

上一篇:[备忘]mysql 存储过程的调试工具
下一篇:[备忘]mysql的row 1 was cut by group_concat()的问题。

添加日期:2014/11/8 21:12:56 快速返回   返回列表 阅读3678次
愁了半天怎么实现,结果一搜,人家写的真简单。

http://www.safnet.com/writing/tech/2012/07/manipulating-table-rows-with-jquery.html#


<table id="table1" style="background-color: Lime" class="displayTable">
      <caption>Table 1</caption>
      <thead>
          <tr>
              <th>One</th>
              <th>Two</th>
              <th>Three</th>
          </tr>
      </thead>
      <tbody>
          <tr id="row1">
              <td>R1 C1</td>
              <td>R1 C2</td>
              <td>
                  <a href="#" id="row1Link" class="rowLink">Move Me</a> |
                  <a href="#" id="row1Up" class="rowUp">Up</a> | 
                  <a href="#" id="row1Down" class="rowDown">Down</a>
              </td>
          </tr>
          <tr id="row2">
              <td>R2 C1</td>
              <td>R2 C2</td>
              <td>
                  <a href="#" id="row2Link" class="rowLink">Move Me</a> |
                  <a href="#" id="row2Up" class="rowUp">Up</a> | 
                  <a href="#" id="row2Down" class="rowDown">Down</a>
              </td>
          </tr>
          <tr id="row3">
              <td>R3 C1</td>
              <td>R3 C2</td>
              <td>
                  <a href="#" id="row3Link" class="rowLink">Move Me</a> |
                  <a href="#" id="row3Up" class="rowUp">Up</a> | 
                  <a href="#" id="row3Down" class="rowDown">Down</a>
              </td>
          </tr>
      </tbody>
  </table>




<script>
    
    // Setup the "Up" links
    $(".rowUp").click(function () {
        var row = $(this).closest("tr");
     
        // Get the previous element in the DOM
        var previous = row.prev();
     
        // Check to see if it is a row
        if (previous.is("tr")) {
            // Move row above previous
            row.detach();
            previous.before(row);
     
            // draw the user's attention to it
            row.fadeOut();
            row.fadeIn();
        }
        // else - already at the top
    });
    
    // Setup the "Down" links
    $(".rowDown").click(function () {
        var row = $(this).closest("tr");
     
        // Get the next element in the DOM
        var previous = row.next();
     
        // Check to see if it is a row
        if (previous.is("tr")) {
            // Move row after next
            row.detach();
            previous.after(row);
     
            // draw the user's attention to it
            row.fadeOut();
            row.fadeIn();
        }
        // else - already at the top
    });
</script>


===========================================
另一个版本:
http://stackoverflow.com/questions/16524497/jquery-to-move-row-up-and-down


<table>
    <tr>
        <td>One</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
            <a href="#" class="top">Top</a>
            <a href="#" class="bottom">Bottom</a>
        </td>
    </tr>
    <tr>
        <td>Two</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
            <a href="#" class="top">Top</a>
            <a href="#" class="bottom">Bottom</a>
        </td>
    </tr>
    <tr>
        <td>Three</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
            <a href="#" class="top">Top</a>
            <a href="#" class="bottom">Bottom</a>
        </td>
    </tr>
    <tr>
        <td>Four</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
            <a href="#" class="top">Top</a>
            <a href="#" class="bottom">Bottom</a>
        </td>
    </tr>
    <tr>
        <td>Five</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
            <a href="#" class="top">Top</a>
            <a href="#" class="bottom">Bottom</a>
        </td>
    </tr>
</table>
<script>
$(document).ready(function(){
    $(".up,.down,.top,.bottom").click(function(){
        var row = $(this).parents("tr:first");
        if ($(this).is(".up")) {
            row.insertBefore(row.prev());
        } else if ($(this).is(".down")) {
            row.insertAfter(row.next());
        } else if ($(this).is(".top")) {
            row.insertBefore($("table tr:first"));
        }else {
            row.insertAfter($("table tr:last"));
        }
    });
});
</script>


================================================
http://castever.wordpress.com/2010/11/19/jquery-move-table-rows-up-and-down/


<table>
<tbody>
<tr class="MoveableRow">
<td>Data 1</td>
<td><span class="down_button">down</span><span class="up_button">up</span></td>
</tr>
<tr></tr>
<tr class="MoveableRow">
<td>Data 2</td>
<td><span class="down_button">down</span><span class="up_button">up</span></td>
</tr>
<tr></tr>
</tbody>
</table>

<script>
$('.down_button').live('click', function () {
    var rowToMove = $(this).parents('tr.MoveableRow:first');
    var next = rowToMove.next('tr.MoveableRow')
    if (next.length == 1) { next.after(rowToMove); }
});
 
$('.up_button').live('click', function () {
    var rowToMove = $(this).parents('tr.MoveableRow:first');
    var prev = rowToMove.prev('tr.MoveableRow')
    if (prev.length == 1) { prev.before(rowToMove); }
});
</script>

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved