Skip to content

Commit 962e05d

Browse files
oemmesscottgonzalez
authored andcommitted
Sortable: Append a tr with td to the placeholder of tbody elements
When sorting tbody elements of a table the placeholder needs to have a tr with td elements to be visible. The appended elements are created in the same way as for the placeholder of a tr element; the first row of the sorted tbody is used for that. Fixes #10682 Closes gh-1380
1 parent d95c23a commit 962e05d

File tree

3 files changed

+97
-14
lines changed

3 files changed

+97
-14
lines changed

tests/unit/sortable/sortable.html

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,56 @@
7474
<table id="sortable-table">
7575
<tbody>
7676
<tr>
77-
<td>1</td>
78-
<td>2</td>
77+
<td>1.1</td>
78+
<td>1.2</td>
7979
</tr>
8080
<tr>
81-
<td>3</td>
82-
<td>4</td>
81+
<td>1.3</td>
82+
<td>1.4</td>
8383
</tr>
8484
<tr>
85-
<td>5</td>
86-
<td>6</td>
85+
<td>1.5</td>
86+
<td>1.6</td>
8787
</tr>
8888
<tr>
89-
<td>7</td>
90-
<td>8</td>
89+
<td>1.7</td>
90+
<td>1.8</td>
91+
</tr>
92+
</tbody>
93+
<tbody>
94+
<tr>
95+
<td>2.1</td>
96+
<td>2.2</td>
97+
</tr>
98+
<tr>
99+
<td>2.3</td>
100+
<td>2.4</td>
101+
</tr>
102+
<tr>
103+
<td>2.5</td>
104+
<td>2.6</td>
105+
</tr>
106+
<tr>
107+
<td>2.7</td>
108+
<td>2.8</td>
109+
</tr>
110+
</tbody>
111+
<tbody>
112+
<tr>
113+
<td>3.1</td>
114+
<td>3.2</td>
115+
</tr>
116+
<tr>
117+
<td>3.3</td>
118+
<td>3.4</td>
119+
</tr>
120+
<tr>
121+
<td>3.5</td>
122+
<td>3.6</td>
123+
</tr>
124+
<tr>
125+
<td>3.7</td>
126+
<td>3.8</td>
91127
</tr>
92128
</tbody>
93129
</table>

tests/unit/sortable/sortable_options.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,42 @@ test( "{ placholder: String } tr", function() {
388388
});
389389
});
390390

391+
test( "{ placholder: String } tbody", function() {
392+
expect( 6 );
393+
394+
var originalWidths,
395+
element = $( "#sortable-table" ).sortable({
396+
placeholder: "test",
397+
start: function( event, ui ) {
398+
var currentWidths = otherBody.children().map(function() {
399+
return $( this ).width();
400+
}).get();
401+
ok( ui.placeholder.hasClass( "test" ), "placeholder has class" );
402+
deepEqual( currentWidths, originalWidths, "table cells maintain size" );
403+
equal( ui.placeholder.children().length, 1,
404+
"placeholder has one child" );
405+
equal( ui.placeholder.children( "tr" ).length, 1,
406+
"placeholder's child is tr" );
407+
equal( ui.placeholder.find( "> tr" ).children().length,
408+
dragBody.find( "> tr:first" ).children().length,
409+
"placeholder's tr has correct number of cells" );
410+
equal( ui.placeholder.find( "> tr" ).children().html(),
411+
$( "<span>&#160;</span>" ).html(),
412+
"placeholder td has content for forced dimensions" );
413+
}
414+
}),
415+
bodies = element.children( "tbody" ),
416+
dragBody = bodies.eq( 0 ),
417+
otherBody = bodies.eq( 1 );
418+
419+
originalWidths = otherBody.children().map(function() {
420+
return $( this ).width();
421+
}).get();
422+
dragBody.simulate( "drag", {
423+
dy: 1
424+
});
425+
});
426+
391427
/*
392428
test("{ revert: false }, default", function() {
393429
ok(false, "missing test - untested code is broken code.");

ui/sortable.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -793,12 +793,13 @@ return $.widget("ui.sortable", $.ui.mouse, {
793793
.addClass(className || that.currentItem[0].className+" ui-sortable-placeholder")
794794
.removeClass("ui-sortable-helper");
795795

796-
if ( nodeName === "tr" ) {
797-
that.currentItem.children().each(function() {
798-
$( "<td>&#160;</td>", that.document[0] )
799-
.attr( "colspan", $( this ).attr( "colspan" ) || 1 )
800-
.appendTo( element );
801-
});
796+
if ( nodeName === "tbody" ) {
797+
that._createTrPlaceholder(
798+
that.currentItem.find( "tr" ).eq( 0 ),
799+
$( "<tr>", that.document[ 0 ] ).appendTo( element )
800+
);
801+
} else if ( nodeName === "tr" ) {
802+
that._createTrPlaceholder( that.currentItem, element );
802803
} else if ( nodeName === "img" ) {
803804
element.attr( "src", that.currentItem.attr( "src" ) );
804805
}
@@ -835,6 +836,16 @@ return $.widget("ui.sortable", $.ui.mouse, {
835836

836837
},
837838

839+
_createTrPlaceholder: function( sourceTr, targetTr ) {
840+
var that = this;
841+
842+
sourceTr.children().each(function() {
843+
$( "<td>&#160;</td>", that.document[ 0 ] )
844+
.attr( "colspan", $( this ).attr( "colspan" ) || 1 )
845+
.appendTo( targetTr );
846+
});
847+
},
848+
838849
_contactContainers: function(event) {
839850
var i, j, dist, itemWithLeastDistance, posProperty, sizeProperty, cur, nearBottom, floating, axis,
840851
innermostContainer = null,

0 commit comments

Comments
 (0)