【发布时间】:2014-08-27 08:39:15
【问题描述】:
我有两个示例表格,第一个单元格中有主题标题。
<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>
我已经使th 和td 的第一个孩子无法排序,因为它们是标题。有没有办法使用 jQueryUI 可排序将其他列一次移动一个(td:nth-child,th:nth-child)到另一个表?
如何在change 或start 事件中使整列可排序?
这是我的预期输出:
<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();
});
【问题讨论】:
-
您的 HTML 结构需要保持不变还是可以编辑? IE。为每一列创建一个表,然后将它们放在一个 div 中,并使 div 可排序,可排序的元素是表本身
-
我没有时间 ATM 来编写任何代码,但我认为解决方案比字面移动更抽象一些。连续计算 td 的数量,并对其进行模数以获取连续的所有 td,即,我们要移动带有子元素 els td3/td6/td9 的表。也许有人可以运行。 HTH
-
我不确定这是否有帮助,但对我来说,这看起来像是一个数据表示问题。 AFAIK,一行代表一个 record,列代表该记录的一个 attribute 值。对我来说,这里
person是一条记录,age、job等是人的属性。 (person1、person2等是age或job的属性吗?好吧,不适合我。)基于此,you're structure should be like this。对,不是问题的答案,因此是评论:)
标签: javascript jquery html jquery-ui jquery-ui-sortable