2013-11-14 28 views
0

我有一些表,例如:jquery-ui sortable:如何在拖動時添加單元格?

<table class="table table-hover table-striped" id="mytable"> 
    <thead> 
    <tr> 
     <th>#</th> 
     <th>Table heading 1</th> 
     <th>Table heading 2</th> 
     <th>Table heading 3</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td>1</td> 
     <td>Table cell</td> 
     <td>Table cell</td> 
     <td>Table cell</td> 
    </tr> 
    </tbody> 
</table> 

然後我要讓排序表中的行頭。

$('#mytable thead tr').sortable({ 
    axis: "x", 
    helper: "clone" 
}).disableSelection(); 

問題:

當我開始將正掉落,我有6 th -s代替4-:

<tr class="ui-sortable"> 
    <th>#</th> 
    <th style=" 
     display: none;">Table heading 1</th> 
    <th class="ui-sortable-placeholder" 
     style=" 
      visibility: hidden;"></th> 
    <th>Table heading 2</th> 
    <th>Table heading 3</th> 
    <th style=" 
      display: table-cell; 
      width: 343px; 
      height: 37px; 
      position: absolute; 
      z-index: 1000; 
      left: 184px;" 
     class="ui-sortable-helper">Table heading 1</th> 
</tr> 

..和所有的加價開始非常不穩定並且不確定:當我將th項目拖動到表格上時,我看到所有行都跳起來了。

很明顯,這是因爲計數th項(它不等於tr中的td項數)。

如何修復?

+0

這個庫做到了這一點:http://www.danvk.org/wp/dragtable/ – rusln

回答

1

每次開始拖動時,都會創建兩個新的th元素。一個不顯示,所以它似乎沒有影響任何東西。第二個是原始元素的佔位符,當您拖動它時。這個新元素的寬度沒有設置,所以它會自動調整到最大寬度,這似乎是導致它跳躍的原因。

爲了解決這個問題,我將佔位符元素的寬度更改爲我們在start函數中拖動的元素的寬度。希望這有助於

start: function(event, ui){ 
    $(".ui-sortable-placeholder").css({width: $(ui.item).width()}); 

下面是代碼,這裏是我的fiddle

$(function() { 
$('#mytable thead tr').sortable({ 
    axis: "x", 
    helper: "clone", 
    start: function(event, ui){ 
     $(".ui-sortable-placeholder").css({width: $(ui.item).width()});  
    } 
}).disableSelection(); 
}); 
+0

這讓生活更美好。但仍然不是解決方案:它看起來像是在拾取項目後執行width(),佔位符的寬度取決於tbody行的內容寬度。例如,第一行變得非常小。順便說一句,你的jsfiddle鏈接沒有任何代碼鏈接到頁面。 – akaRem

+0

對不起,剛更新我的小提琴鏈接。 – Matt

+0

呃。在不整潔的桌子上它運行得很好。但是,如果添加鏈接到引導CSS,這個拖放非常奇怪的行爲。 – akaRem

相關問題