Skip to content

Commit 87eab46

Browse files
authored andcommitted
Sortable: Setting table row placeholder height to be same as sorted row
Fixes #13662 Closes gh-1578
1 parent b9d687d commit 87eab46

File tree

Image for: File tree

3 files changed

Image for: 3 files changed
+81
-16
lines changed

3 files changed

Image for: 3 files changed
+81
-16
lines changed

‎tests/unit/sortable/options.js

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,55 @@ test("{ dropOnEmpty: true }, default", function() {
255255
test("{ dropOnEmpty: false }", function() {
256256
ok(false, "missing test - untested code is broken code.");
257257
});
258+
*/
258259

259-
test("{ forcePlaceholderSize: false }, default", function() {
260-
ok(false, "missing test - untested code is broken code.");
261-
});
260+
QUnit.test( "{ forcePlaceholderSize: false } table rows", function( assert ) {
261+
assert.expect( 1 );
262262

263-
test("{ forcePlaceholderSize: true }", function() {
264-
ok(false, "missing test - untested code is broken code.");
265-
});
263+
var element = $( "#sortable-table2 tbody" );
264+
265+
element.sortable( {
266+
placeholder: "test",
267+
forcePlaceholderSize: false,
268+
start: function( event, ui ) {
269+
assert.notEqual( ui.placeholder.height(), ui.item.height(),
270+
"placeholder is same height as item" );
271+
}
272+
} );
273+
274+
// This row has a non-standard height
275+
$( "tr", element ).eq( 0 ).simulate( "drag", {
276+
dy: 1
277+
} );
278+
} );
266279

280+
QUnit.test( "{ forcePlaceholderSize: true } table rows", function( assert ) {
281+
assert.expect( 2 );
282+
283+
// Table should have the placeholder's height set the same as the row we're dragging
284+
var element = $( "#sortable-table2 tbody" );
285+
286+
element.sortable( {
287+
placeholder: "test",
288+
forcePlaceholderSize: true,
289+
start: function( event, ui ) {
290+
assert.equal( ui.placeholder.height(), ui.item.height(),
291+
"placeholder is same height as item" );
292+
}
293+
} );
294+
295+
// First row has a non-standard height
296+
$( "tr", element ).eq( 0 ).simulate( "drag", {
297+
dy: 1
298+
} );
299+
300+
// Second row's height is normal
301+
$( "tr", element ).eq( 1 ).simulate( "drag", {
302+
dy: 1
303+
} );
304+
} );
305+
306+
/*
267307
test("{ forceHelperSize: false }, default", function() {
268308
ok(false, "missing test - untested code is broken code.");
269309
});

‎tests/unit/sortable/sortable.html

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
border-width: 0;
2323
height:19px;
2424
}
25-
#sortable-table {
25+
#sortable-table,
26+
#sortable-table2 {
2627
width: 100%;
2728
}
2829
</style>
@@ -105,6 +106,24 @@
105106
</tbody>
106107
</table>
107108

109+
<!-- This table has rows of varying height -->
110+
<table id="sortable-table2">
111+
<tbody>
112+
<tr>
113+
<td>1<br>1</td>
114+
<td>1</td>
115+
</tr>
116+
<tr>
117+
<td>2</td>
118+
<td>2</td>
119+
</tr>
120+
<tr>
121+
<td>3</td>
122+
<td>3</td>
123+
</tr>
124+
</tbody>
125+
</table>
126+
108127
<div id="sortable-images">
109128
<img src="../../images/jqueryui_32x32.png" alt="">
110129
<img src="../../images/jqueryui_32x32.png" alt="">

‎ui/widgets/sortable.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -875,20 +875,20 @@ return $.widget( "ui.sortable", $.ui.mouse, {
875875

876876
_createPlaceholder: function( that ) {
877877
that = that || this;
878-
var className,
878+
var className, nodeName,
879879
o = that.options;
880880

881881
if ( !o.placeholder || o.placeholder.constructor === String ) {
882882
className = o.placeholder;
883+
nodeName = that.currentItem[ 0 ].nodeName.toLowerCase();
883884
o.placeholder = {
884885
element: function() {
885886

886-
var nodeName = that.currentItem[ 0 ].nodeName.toLowerCase(),
887-
element = $( "<" + nodeName + ">", that.document[ 0 ] );
887+
var element = $( "<" + nodeName + ">", that.document[ 0 ] );
888888

889-
that._addClass( element, "ui-sortable-placeholder",
890-
className || that.currentItem[ 0 ].className )
891-
._removeClass( element, "ui-sortable-helper" );
889+
that._addClass( element, "ui-sortable-placeholder",
890+
className || that.currentItem[ 0 ].className )
891+
._removeClass( element, "ui-sortable-helper" );
892892

893893
if ( nodeName === "tbody" ) {
894894
that._createTrPlaceholder(
@@ -917,9 +917,15 @@ return $.widget( "ui.sortable", $.ui.mouse, {
917917
return;
918918
}
919919

920-
//If the element doesn't have a actual height by itself (without styles coming
921-
// from a stylesheet), it receives the inline height from the dragged item
922-
if ( !p.height() ) {
920+
// If the element doesn't have a actual height or width by itself (without
921+
// styles coming from a stylesheet), it receives the inline height and width
922+
// from the dragged item. Or, if it's a tbody or tr, it's going to have a height
923+
// anyway since we're populating them with <td>s above, but they're unlikely to
924+
// be the correct height on their own if the row heights are dynamic, so we'll
925+
// always assign the height of the dragged item given forcePlaceholderSize
926+
// is true.
927+
if ( !p.height() || ( o.forcePlaceholderSize &&
928+
( nodeName === "tbody" || nodeName === "tr" ) ) ) {
923929
p.height(
924930
that.currentItem.innerHeight() -
925931
parseInt( that.currentItem.css( "paddingTop" ) || 0, 10 ) -

0 commit comments

Image for: 0 commit comments
Comments
 (0)