tableDnD實現table裏行的拖拽排序

tableDnD是一個能使table裏的行能拖動排序的插件。這個插件的主頁在Github上,用谷歌去查,百度一下,你什麼都不會知道。使用這個插件也很簡單,直接給table節點綁定這個插件就行了。

$("#tableId").tableDnD({
      onDragClass:'highlight',
      onDrop:function(table,row){
          console.log('AAA');
      }
});

onDragClass屬性標識的是在選中拖動一行時候的樣式效果,onDrop方法是表示拖動成功後執行的方法,主要用着兩個屬性和方法就可以完成需要的功能。這裏就可以執行ajax將排序值重組,完成完整的拖動排序功能。如果某一些行不想拖動,只需要給這一行添加class nodrop和nodrag即可。

<tr class="nodrop nodrag">
    <td>10</td> 
    <td>The Lord of the Rings: The Return of the King</td>
    <td>2003</td>
</tr>


整體的效果如下:


完整代碼如下所示,裏面的tableDnD.js可以再github上下載到。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Table拖拽測試</title>
    <link href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
    <style>

body {
    width: 600px;
    margin: 40px auto;
    font-family: 'trebuchet MS', 'Lucida sans', Arial;
    font-size: 14px;
    color: #444;
}

table {
    *border-collapse: collapse; /* IE7 and lower */
    border-spacing: 0;
    width: 100%;    
}

.bordered {
    border: solid #ccc 1px;
    -moz-border-radius: 6px;
    -webkit-border-radius: 6px;
    border-radius: 6px;
    -webkit-box-shadow: 0 1px 1px #ccc; 
    -moz-box-shadow: 0 1px 1px #ccc; 
    box-shadow: 0 1px 1px #ccc;         
}

.bordered tr:hover {
    background: #fbf8e9;
    -o-transition: all 0.1s ease-in-out;
    -webkit-transition: all 0.1s ease-in-out;
    -moz-transition: all 0.1s ease-in-out;
    -ms-transition: all 0.1s ease-in-out;
    transition: all 0.1s ease-in-out;     
}
    
.bordered td, .bordered th {
    border-left: 1px solid #ccc;
    border-top: 1px solid #ccc;
    padding: 10px;
    text-align: left;    
}

.bordered th {
    background-color: #dce9f9;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9));
    background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image:    -moz-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image:     -ms-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image:      -o-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image:         linear-gradient(top, #ebf3fc, #dce9f9);
    -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset; 
    -moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;  
    box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;        
    border-top: none;
    text-shadow: 0 1px 0 rgba(255,255,255,.5); 
}

.bordered td:first-child, .bordered th:first-child {
    border-left: none;
}

.bordered th:first-child {
    -moz-border-radius: 6px 0 0 0;
    -webkit-border-radius: 6px 0 0 0;
    border-radius: 6px 0 0 0;
}

.bordered th:last-child {
    -moz-border-radius: 0 6px 0 0;
    -webkit-border-radius: 0 6px 0 0;
    border-radius: 0 6px 0 0;
}

.bordered th:only-child{
    -moz-border-radius: 6px 6px 0 0;
    -webkit-border-radius: 6px 6px 0 0;
    border-radius: 6px 6px 0 0;
}

.bordered tr:last-child td:first-child {
    -moz-border-radius: 0 0 0 6px;
    -webkit-border-radius: 0 0 0 6px;
    border-radius: 0 0 0 6px;
}

.bordered tr:last-child td:last-child {
    -moz-border-radius: 0 0 6px 0;
    -webkit-border-radius: 0 0 6px 0;
    border-radius: 0 0 6px 0;
}

</style>
    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="jQuery.tablednd.js"></script>
    <script type="text/javascript">
    $(function(){
        $("#tableId").tableDnD({
            onDragClass:'highlight',
            //回調函數
            onDrop:function(table,row){
               console.log('AAA');
            }
        }
        );
    });
    </script>
</head>
<body>
    <h2>Drag the table——Neven.Jr Production</h2>
<table class="bordered" id="tableId">
    <thead>

    <tr>
        <th>#</th>        
        <th>IMDB Top 10 Movies</th>
        <th>Year</th>
        <th>Action</th>
    </tr>
    </thead>
    <tr>
        <td>1</td>        
        <td>The Shawshank Redemption</td>

        <td>1994</td>
        <td>
            <a href="javascript:void(0);"><i class="icon-bookmark"></i>Mark</a>  
            <a href="javascript:void(0);"><i class="icon-edit">Detail</i></a>
        </td>
    </tr>        
    <tr>
        <td>2</td>         
        <td>The Godfather</td>
        <td>1972</td>
         <td>
            <a href="javascript:void(0);"><i class="icon-bookmark"></i>Mark</a>  
            <a href="javascript:void(0);"><i class="icon-edit">Detail</i></a>
        </td>
    </tr>
    <tr>

        <td>3</td>         
        <td>The Godfather: Part II</td>
        <td>1974</td>
         <td>
            <a href="javascript:void(0);"><i class="icon-bookmark"></i>Mark</a>  
            <a href="javascript:void(0);"><i class="icon-edit">Detail</i></a>
        </td>
    </tr>    
    <tr>
        <td>4</td> 
        <td>The Good, the Bad and the Ugly</td>
        <td>1966</td>
        <td>
            <a href="javascript:void(0);"><i class="icon-bookmark"></i>Mark</a>  
            <a href="javascript:void(0);"><i class="icon-edit">Detail</i></a>
        </td>
    </tr>
    <tr>
        <td>5</td> 
        <td>Pulp Fiction</td>
        <td>1994</td>
         <td>
            <a href="javascript:void(0);"><i class="icon-bookmark"></i>Mark</a>  
            <a href="javascript:void(0);"><i class="icon-edit">Detail</i></a>
        </td>
    </tr>
    <tr>
        <td>6</td> 
        <td>12 Angry Men</td>

        <td>1957</td>
         <td>
            <a href="javascript:void(0);"><i class="icon-bookmark"></i>Mark</a>  
            <a href="javascript:void(0);"><i class="icon-edit">Detail</i></a>
        </td>
    </tr>
    <tr>
        <td>7</td> 
        <td>Schindler's List</td>
        <td>1993</td>
         <td>
            <a href="javascript:void(0);"><i class="icon-bookmark"></i>Mark</a>  
            <a href="javascript:void(0);"><i class="icon-edit">Detail</i></a>
        </td>
    </tr>    
    <tr>

        <td>8</td> 
        <td>One Flew Over the Cuckoo's Nest</td>
        <td>1975</td>
         <td>
            <a href="javascript:void(0);"><i class="icon-bookmark"></i>Mark</a>  
            <a href="javascript:void(0);"><i class="icon-edit">Detail</i></a>
        </td>
    </tr>
    <tr>
        <td>9</td> 
        <td>The Dark Knight</td>

        <td>2008</td>
         <td>
            <a href="javascript:void(0);">Mark</a>  
            <a href="javascript:void(0);"><i class="icon-edit">Detail</i></a>
        </td>
    </tr>
    <tr class="nodrop nodrag">
        <td>10</td> 
        <td>The Lord of the Rings: The Return of the King</td>
        <td>2003</td>
         <td>
            <a href="javascript:void(0);"><i class="icon-bookmark"></i>Mark</a>  
            <a href="javascript:void(0);"><i class="icon-edit">Detail</i></a>
        </td>
    </tr> 

</table>
<br>
</body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章