在做这个功能的时候在网上找了大量资料,发现都不适用,要不然就是代码太冗余,所以另起炉灶,自己封装了这个函数

下面是完整的代码:

 <!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="easyui/themes/default/easyui.css"/>
<link rel="stylesheet" href="easyui/themes/icon.css"/>
<script src="easyui/jquery.min.js"></script>
<script src="easyui/jquery.easyui.min.js"></script>
<script src="easyui/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
<div id="tab1">
<table id="removeCell1">
<thead>
<tr>
<th data-options="field:'X',width:50,align:'center',sortable:true">X</th>
<th data-options="field:'Y',width:50,align:'center',sortable:true">Y</th>
<th data-options="field:'Z',width:50,align:'center',sortable:true">Z</th>
<th data-options="field:'IMSI',width:50,align:'center',sortable:true">A</th>
<th data-options="field:'Status',width:50,align:'center',sortable:true">B</th>
<th data-options="field:'Online',width:50,align:'center',sortable:true">C</th>
</tr>
</thead>
</table>
</div>
<div id="tab2">
<table id="removeCell2">
<thead>
<tr>
<th data-options="field:'X',width:50,align:'center',sortable:true">X</th>
<th data-options="field:'Y',width:50,align:'center',sortable:true">Y</th>
<th data-options="field:'Z',width:50,align:'center',sortable:true">Z</th>
<th data-options="field:'IMSI',width:50,align:'center',sortable:true">A</th>
<th data-options="field:'Status',width:50,align:'center',sortable:true">B</th>
<th data-options="field:'Online',width:50,align:'center',sortable:true">C</th>
</thead>
</table>
</div>
<script>
$("#removeCell1").datagrid({
rownumbers:true,
width:380,
height:300,
singleSelect:true,
multiSort:true,
remoteSort:true,
url:"query.json",
onLoadSuccess:function() {
remove("tab1");
}
})
$("#removeCell2").datagrid({
rownumbers:true,
width:380,
height:300,
singleSelect:true,
multiSort:true,
remoteSort:true,
url:"query.json",
onLoadSuccess:function() {
remove("tab2");
}
}) function remove(limit) {
var dom='.datagrid-header-inner .datagrid-cell';
var tbodyTr = $(".datagrid-view2 .datagrid-body .datagrid-btable tbody tr");
if(limit){
dom='#'+limit+' .datagrid-header-inner .datagrid-cell';
tbodyTr = $("#"+limit+" .datagrid-view2 .datagrid-body .datagrid-btable tbody tr");
}
$(dom).draggable({
revert: true,
proxy: 'clone'
}).droppable({
accept: dom,
onDrop: function (e, source) {
var destination=$(e.currentTarget).parent().index();
var start=$(source).parent().index();
if(destination<start){
$(source).parent().insertBefore($(e.currentTarget).parent());
}else{
$(source).parent().insertAfter($(e.currentTarget).parent());
}
for(var i=0;i<tbodyTr.length;i++){
var cell1=$(tbodyTr[i]).find("td")[destination];
var cell2=$(tbodyTr[i]).find("td")[start];
if(destination<start){
$(cell2).insertBefore($(cell1));
}else{
$(cell2).insertAfter($(cell1));
}
}
}
});
}
</script>
</body>
</html>

我定义了两个table,table外部必须包裹有唯一id的div,这是要保证两个table之间的操作互不影响.

主要实现功能的就是remove()这个函数,里面传入的参数是table外面div的ID,这样可以区分两个table的操作。

如果你的页面只有一个table的话不传参数也是可以的,也可以把limit参数相关的代码删除.

remove()函数需要在datagrid数据加载完成后调用,否则拖动时只有列头改变.

缺点:

这个函数虽然解决了列表拖动的问题,同时也产生了新的问题--------单击排序

当鼠标放到列头时会变成可拖动的标识,这时排序就没用了吗?当然不是,虽然单击时有影响,但是我发现双击还是有用的哦!

如果你对这个缺点不能容忍的话,欢迎提出新的解决方案.

请尊重别人的劳动成果,转载务必标明出处!

05-07 13:32
查看更多