2014-08-27 177 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