简体   繁体   English

如何使用可排序的jquery-ui功能同时对两个表中的行进行拖放排序?

[英]How use sortable jquery-ui feature to sort by drag and drop rows from two tables in the same time?

I have two tables which are next to each other. 我有两个彼此相邻的桌子。 Here is a short example of template: 这是模板的简短示例:

<table id="table1">
    <tbody>
        <tr>
            <td>a1</td>
            <td>a1</td>
        </tr>
        <tr>
            <td>b1</td>
            <td>b1</td>
        </tr>
    </tbody>
</table>
<table id="table2">
    <tbody>
        <tr>
            <td>a2</td>
            <td>a2</td>
        </tr>
        <tr>
            <td>b2</td>
            <td>b2/td>
        </tr>
    </tbody>
</table>

So it looks like that: 所以看起来像这样:

|a1|a1|a2|a2|
|b1|b1|b2|b2|

My goal is to use sortable jquery-ui feature to sort by drag and drop rows of both tables in the same time. 我的目标是使用可排序的jquery-ui功能同时按两个表的拖放行进行排序。 In other words - when user clicks for example cell from tabel1 then the whole row 换句话说-例如,当用户单击tabel1中的单元格时,则整行

|a1|a1|a2|a2|

should be draged and then droped while mouse up. 应在鼠标向上拖动时将其拖动然后放下。 I don't need to drag and drop rows between two tables - I need rows from two tables to be treated as one row while dragging - if this rows have the same position Y in its table. 我不需要两个表之间拖放行 -我需要将两个表中的行拖放时视为一行-如果该行在表中的位置Y相同。

Actually, the use case is that I have grid where I need to implement drag and drop rows. 实际上,用例是我有网格,需要在其中实现拖放行。 User can freeze some columns of the grid - then there are 2 tables- one for freezed columns and one for unfreezed - so there is actually one row but divided into 2 table. 用户可以冻结网格的某些列-然后有2个表-一个用于冻结的列,一个用于未冻结的表-因此实际上存在一行,但分为2个表。 Here is an example of grid: https://www.igniteui.com/grid/column-fixing 这是网格的示例: https : //www.igniteui.com/grid/column-fixing

There is not a good way to accomplish this. 没有一个好方法来完成此任务。 This also gets more difficult if you have a larger number of items. 如果您有更多的物品,这也会变得更加困难。 Basically, you perform sort on one table, and then when the sort is complete, move the corresponding item to the proper location in the second table. 基本上,您在一个表上执行排序,然后在排序完成后,将相应的项目移到第二个表中的适当位置。

 $(function() { $("#table1 tbody").sortable({ handle: ".row-handle", items: "> tr", helper: function(e, item) { var index = item.index(); item.data("right", $("#table2 tbody tr:eq(" + index + ")").clone()); item.data("orig-index", index); $("#table2 tbody tr:eq(" + index + ")").fadeOut("fast"); return item; }, update: function(e, ui) { var row2 = ui.item.data("right"); var index = ui.item.index(); $("#table2 tbody tr").eq(ui.item.data("orig-index")).remove(); switch (true) { case index == 0: console.log("First", index); $("#table2 tbody tr:eq(0)").before(row2); break; case index == $("#table2 tr").length: console.log("Last", index); $("#table2 tbody").append(row2); break; case index > 0 && index < $("#table2 tr").length: console.log("Mid", index); $("#table2 tbody tr").eq(index).before(row2); } } }); }); 
 .left { float: left; width: 50%; } .right { float: right; width: 50%; } 
 <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <table id="table1" class="left ui-widget"> <thead class="ui-widget-header"> <tr> <td style="width: 20px;">&nbsp;</td> <th>Company</th> <th>Name</th> </tr> </thead> <tbody class="ui-widget-content"> <tr> <td style="width: 20px;"><span class="ui-icon ui-icon-grip-dotted-vertical row-handle"></span></td> <td>Company 1</td> <td>Name 1</td> </tr> <tr> <td><span class="ui-icon ui-icon-grip-dotted-vertical row-handle"></span></td> <td>Company 2</td> <td>Name 2</td> </tr> <tr> <td><span class="ui-icon ui-icon-grip-dotted-vertical row-handle"></span></td> <td>Company 3</td> <td>Name 3</td> </tr> </tbody> </table> <table id="table2" class="right ui-widget"> <thead class="ui-widget-header"> <tr> <th>Title</th> <th>Address</th> </tr> </thead> <tbody> <tr> <td>Title 1</td> <td>Address 1</td> </tr> <tr> <td>Title 2</td> <td>Address 2</td> </tr> <tr> <td>Title 3</td> <td>Address 3</td> </tr> </tbody> </table> 


Update 更新

Adjusted the update logic to work better. 调整了update逻辑以更好地工作。 Moving an item multiple times now fixed. 现在可以多次移动项目。


This works pretty well on the first go, yet starts to encounter issues with other sorts. 初次使用时效果不错,但开始遇到其他种类的问题。 I do not see a benefit to aligning two tables like this, for use with Sortable. 对于将两个表对齐以与Sortable一起使用,我看不出任何好处。 You may want to consider if this is a necessary feature versus the sort function already built into your grid plugin. 您可能要考虑这是否是必需功能,而不是网格插件中已内置的排序功能。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM