Skip to content

Sortable: Setting table row placeholder height to be same as sorted row #1578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions tests/unit/sortable/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,54 @@ test("{ dropOnEmpty: true }, default", function() {
test("{ dropOnEmpty: false }", function() {
ok(false, "missing test - untested code is broken code.");
});
*/

test("{ forcePlaceholderSize: false }, default", function() {
ok(false, "missing test - untested code is broken code.");
test("{ forcePlaceholderSize: false } table rows", function() {
expect( 1 );

var element = $( "#sortable-table2 tbody" );

element.sortable({
placeholder: "test",
forcePlaceholderSize: false,
start: function( event, ui ) {
notEqual( ui.placeholder.height(), ui.item.height(),
"placeholder is same height as item" );
}
});

// This row has a non-standard height
$("tr", element).eq( 0 ).simulate( "drag", {
dy: 1
});
});

test("{ forcePlaceholderSize: true }", function() {
ok(false, "missing test - untested code is broken code.");
test("{ forcePlaceholderSize: true } table rows", function() {
expect( 2 );

// Table should have the placeholder's height set the same as the row we're dragging
var element = $( "#sortable-table2 tbody" );

element.sortable({
placeholder: "test",
forcePlaceholderSize: true,
start: function( event, ui ) {
equal( ui.placeholder.height(), ui.item.height(),
"placeholder is same height as item" );
}
});

// First row has a non-standard height
$("tr", element).eq( 0 ).simulate( "drag", {
dy: 1
});
// Second row's height is normal
$("tr", element).eq( 1 ).simulate( "drag", {
dy: 1
});
});

/*
test("{ forceHelperSize: false }, default", function() {
ok(false, "missing test - untested code is broken code.");
});
Expand Down
21 changes: 20 additions & 1 deletion tests/unit/sortable/sortable.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
border-width: 0;
height:19px;
}
#sortable-table {
#sortable-table,
#sortable-table2 {
width: 100%;
}
</style>
Expand Down Expand Up @@ -105,6 +106,24 @@
</tbody>
</table>

<!-- This table has rows of varying height -->
<table id="sortable-table2">
<tbody>
<tr>
<td>1<br>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
</tr>
</tbody>
</table>

<div id="sortable-images">
<img src="../../images/jqueryui_32x32.png" alt="">
<img src="../../images/jqueryui_32x32.png" alt="">
Expand Down
19 changes: 14 additions & 5 deletions ui/sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,15 +785,16 @@ return $.widget("ui.sortable", $.ui.mouse, {
_createPlaceholder: function(that) {
that = that || this;
var className,
nodeName,
o = that.options;

if(!o.placeholder || o.placeholder.constructor === String) {
className = o.placeholder;
nodeName = that.currentItem[0].nodeName.toLowerCase();
o.placeholder = {
element: function() {

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

that._addClass( element, "ui-sortable-placeholder",
className || that.currentItem[ 0 ].className )
Expand Down Expand Up @@ -824,9 +825,17 @@ return $.widget("ui.sortable", $.ui.mouse, {
return;
}

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