【发布时间】:2019-07-04 01:09:56
【问题描述】:
我正在尝试学习 jQueryUI 可排序功能,以使其在我在网页上使用的响应式表格上工作。
我做了一个jsfiddle 来进行一些测试,但我就是不知道如何拖放列(不仅如此)。
我怀疑我应该使用“connectWith”(或“items”?)选项并连接所有与拖动的 th 共享相同 id 的 td,但我只是没有成功。
我什至不确定“connectWith”或“items”实际上是用来做什么的,但我在文档中找不到任何其他东西似乎可以帮助我实现列拖放。
HTML:
<html>
<body>
<table>
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>data 1</td>
<td>data 2</td>
<td>data 3</td>
<td>data 4</td>
</tr>
<tr>
<td>data 1</td>
<td>data 2</td>
<td>data 3</td>
<td>data 4</td>
</tr>
</tbody>
</table>
</body>
</html>
CSS:
*html, body {
margin:0px;
padding:0px;
width:100%;
height:100%;
}
body:before{
content: "";
display: block;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
table{
table-layout: fixed;
margin: 0 auto;
border-collapse: collapse;
overflow: hidden;
white-space: nowrap;
width:100%;
color:#000;
float:left;
}
tr{
display:table-row;
border: 1px solid black
}
th, td {
border:1px solid #000;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
display: table-cell;
text-align:center;
}
th{
position: relative;
background:#bbb;
}
jQuery:
$( function() {
$('thead tr').sortable({
start: function(e, ui)
{
var ind_th= ui.item.index();
$('tbody td:nth-child('+(ind_th+1)+')').addClass('drg').css('color','red');
},
connectWith:".drg",
stop: function(e, ui)
{
$('tbody td').removeClass('drg')
}
});
});
感谢您的帮助。
【问题讨论】:
标签: jquery-ui drag-and-drop jquery-ui-sortable dynamic-columns