2014-08-27 68 views
1

Fiddle Example在jQueryUI的排序

移動列,包括日,表之間的我有與第一被攝體細胞標題兩個示例性的表。

<table class='sort connect'> 
    <thead> 
     <tr> 
      <th class='ui-state-disabled'></th> 
      <th>Person 1</th> 
      <th>Person 2</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td class='ui-state-disabled'>Age</td> 
      <td>18</td> 
      <td>23</td> 
     </tr> 
     <tr> 
      <td class='ui-state-disabled'>Job</td> 
      <td>Clerk</td> 
      <td>Policeman</td> 
     </tr> 
    </tbody> 
</table> 


<table class='sort connect'> 
    <thead> 
     <tr> 
      <th class='ui-state-disabled'></th> 
      <th>Person 3</th> 
      <th>Person 4</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td class='ui-state-disabled'>Age</td> 
      <td>17</td> 
      <td>46</td> 
     </tr> 
     <tr> 
      <td class='ui-state-disabled'>Job</td> 
      <td>Student</td> 
      <td>Firefighter</td> 
     </tr> 
    </tbody> 
</table> 

我做的thtd不可排序,因爲他們是冠軍的第一個孩子。有沒有辦法將其他列一次一個(td:nth-child,th:nth-child)移動到另一個使用jQueryUI排序的表? 如何在changestart事件中對整列進行排序?

這裏是我的預期輸出:

<table class='sort connect'> 
    <thead> 
     <tr> 
      <th class='ui-state-disabled'></th> 
      <th>Person 1</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td class='ui-state-disabled'>Age</td> 
      <td>18</td> 
     </tr> 
     <tr> 
      <td class='ui-state-disabled'>Job</td> 
      <td>Clerk</td> 
     </tr> 
    </tbody> 
</table> 


<table class='sort connect'> 
    <thead> 
     <tr> 
      <th class='ui-state-disabled'></th> 
      <th>Person 3</th> 
      <th>Person 2</th> // sorted 
      <th>Person 4</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td class='ui-state-disabled'>Age</td> 
      <td>17</td> 
      <td>23</td> //sorted 
      <td>46</td> 
     </tr> 
     <tr> 
      <td class='ui-state-disabled'>Job</td> 
      <td>Student</td> 
      <td>Policeman</td> //sorted 
      <td>Firefighter</td> 
     </tr> 
    </tbody> 
</table> 

JS代碼:

var fixHelperModified = function(e, tr) { 
    var $originals = tr.children(); 
    var $helper = tr.clone(); 
    $helper.children().each(function(index) 
    { 
     $(this).width($originals.eq(index).width()) 
    }); 
    return $helper; 
}; 

$(function() { 
    $(".sort").sortable({ 
    change: function(event, ui) { 
     var see = ui.item.index(); 
      console.log(see); 
     $(this).find('td:nth-child(see),th:nth-child(see)') 
    }, 
    helper: fixHelperModified, 
    cancel: ".ui-state-disabled", 
    connectWith: ".connect" 
    }).disableSelection(); 
    }); 
+0

您的HTML結構是否需要保持不變或可以編輯?即爲每一列創建一個表,然後將它們放在一個div中,並使div可排序,並將表排序爲 – ctwheels 2014-08-29 18:29:24

+1

我沒有時間ATM來編寫任何代碼,但我認爲解決方案更抽象一些而不是字面上的舉動。一些在一行中執行td的計數,並在它上面進行模數以獲得連續的所有tds,即,我們想要將子表移動到els td3/td6/td9。也許有人可以運行。 HTH – briansol 2014-08-29 20:34:54

+1

我不確定這是否有幫助,但對我來說,這看起來像數據表示問題。 AFAIK,一行​​代表*記錄*,而列代表該記錄的*屬性*值。對我而言,這裏的'人'是一個記錄,'年齡','工作'等是人的屬性。 (*是'person1','person2'等'age'或'job'的屬性?好吧,不適合我。*)基於此,[**你的結構應該是這樣**](http ://jsfiddle.net/tt3ceLbf/21/)。對,不是*問題的回答,因此評論:) – 2014-08-31 09:02:15

回答

2

什麼像this

它是你問一個解決辦法,但它基本上是同樣的事情,剛修好的造型,空間等,只要你願意

HTML

<div class="sortableContainer sort connect"> 
    <div> 
     <table> 
      <thead> 
       <tr> 
        <td height="20px"></td> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td>Age</td> 
       </tr> 
       <tr> 
        <td>Job</td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
    <div> 
     <table> 
      <thead> 
       <tr> 
        <td>Person 1</td> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td>18</td> 
       </tr> 
       <tr> 
        <td>Clerk</td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
    <div> 
     <table> 
      <thead> 
       <tr> 
        <td>Person 2</td> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td>23</td> 
       </tr> 
       <tr> 
        <td>Policeman</td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
</div> 
<div class="sortableContainer sort connect"> 
    <div> 
     <table> 
      <thead> 
       <tr> 
        <td height="20px"></td> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td>Age</td> 
       </tr> 
       <tr> 
        <td>Job</td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
    <div> 
     <table> 
      <thead> 
       <tr> 
        <td>Person 3</td> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td>17</td> 
       </tr> 
       <tr> 
        <td>Student</td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
    <div> 
     <table> 
      <thead> 
       <tr> 
        <td>Person 4</td> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td>46</td> 
       </tr> 
       <tr> 
        <td>Firefighter</td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
</div> 

CSS

td, th { 
    border:1px solid #222 
} 
.red { 
    background:red 
} 
.ui-state-disabled { 
    opacity:1 
} 
.sortableContainer>div { 
    display:inline-block; 
} 
table { 
    border-spacing:0px; 
    border-collapse:collapse; 
} 

JS

$(function() { 
    $(".sort").sortable({ 
     connectWith: ".connect" 
    }).disableSelection(); 
}); 
+1

我沒有想過這件事。這似乎是一個很好的解決方案。我需要注意的是第二個和第三個表格中的內容長度,以確保它們的高度和寬度與第一個表格匹配。否則整個結構將被打破。 – RedGiant 2014-08-29 19:22:08

+0

@RedGiant我不太清楚你的意思,你能強調一下嗎? – ctwheels 2014-08-29 19:43:32

+1

@RedGiant我想我知道你的意思了,無論如何,這是你的意思嗎? http://jsfiddle.net/tt3ceLbf/22/ – ctwheels 2014-08-31 19:49:02