Skip to content

Sortable: Append a tr with td to the placeholder of tbody elements #1380

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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
52 changes: 44 additions & 8 deletions tests/unit/sortable/sortable.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,56 @@
<table id="sortable-table">
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>1.1</td>
<td>1.2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>1.3</td>
<td>1.4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
<td>1.5</td>
<td>1.6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>1.7</td>
<td>1.8</td>
</tr>
</tbody>
<tbody>
<tr>
<td>2.1</td>
<td>2.2</td>
</tr>
<tr>
<td>2.3</td>
<td>2.4</td>
</tr>
<tr>
<td>2.5</td>
<td>2.6</td>
</tr>
<tr>
<td>2.7</td>
<td>2.8</td>
</tr>
</tbody>
<tbody>
<tr>
<td>3.1</td>
<td>3.2</td>
</tr>
<tr>
<td>3.3</td>
<td>3.4</td>
</tr>
<tr>
<td>3.5</td>
<td>3.6</td>
</tr>
<tr>
<td>3.7</td>
<td>3.8</td>
</tr>
</tbody>
</table>
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/sortable/sortable_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,42 @@ test( "{ placholder: String } tr", function() {
});
});

test( "{ placholder: String } tbody", function() {
expect( 6 );

var originalWidths,
element = $( "#sortable-table" ).sortable({
placeholder: "test",
start: function( event, ui ) {
var currentWidths = otherBody.children().map(function() {
return $( this ).width();
}).get();
ok( ui.placeholder.hasClass( "test" ), "placeholder has class" );
deepEqual( currentWidths, originalWidths, "table cells maintain size" );
equal( ui.placeholder.children().length, 1,
"placeholder has one child" );
equal( ui.placeholder.children( "tr" ).length, 1,
"placeholder's child is tr" );
equal( ui.placeholder.find( "> tr" ).children().length,
dragBody.find( "> tr:first" ).children().length,
"placeholder's tr has correct number of cells" );
equal( ui.placeholder.find( "> tr" ).children().html(),
$( "<span>&#160;</span>" ).html(),
"placeholder td has content for forced dimensions" );
}
}),
bodies = element.children( "tbody" ),
dragBody = bodies.eq( 0 ),
otherBody = bodies.eq( 1 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these up so that reading the code from top to bottom has the proper context. This means that calling .sortable() would move out of the var statement.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, feel free to ignore this since there are other tests written like this already. We can address during the interaction rewrites.


originalWidths = otherBody.children().map(function() {
return $( this ).width();
}).get();
dragBody.simulate( "drag", {
dy: 1
});
});

/*
test("{ revert: false }, default", function() {
ok(false, "missing test - untested code is broken code.");
Expand Down
25 changes: 18 additions & 7 deletions ui/sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,8 @@ return $.widget("ui.sortable", $.ui.mouse, {
_createPlaceholder: function(that) {
that = that || this;
var className,
o = that.options;
o = that.options,
tr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flip these two lines. Uninitialized variables come first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'd just remove the variable.


if(!o.placeholder || o.placeholder.constructor === String) {
className = o.placeholder;
Expand All @@ -788,12 +789,12 @@ return $.widget("ui.sortable", $.ui.mouse, {
.addClass(className || that.currentItem[0].className+" ui-sortable-placeholder")
.removeClass("ui-sortable-helper");

if ( nodeName === "tr" ) {
that.currentItem.children().each(function() {
$( "<td>&#160;</td>", that.document[0] )
.attr( "colspan", $( this ).attr( "colspan" ) || 1 )
.appendTo( element );
});
if ( nodeName === "tbody" ) {
tr = $( "<tr></tr>", that.document[0] )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why this is creating a <tr> instead of another <tbody> to act as a placeholder? As is, sorting <tbody>s is kind of weird because the injected <tr> used as a placeholder is not the same height as a <tbody>.

.appendTo( element ) ;
that._createTrPlaceholder( that, that.currentItem.find( "tr:first" ), tr );
} else if ( nodeName === "tr" ) {
that._createTrPlaceholder( that, that.currentItem, element );
} else if ( nodeName === "img" ) {
element.attr( "src", that.currentItem.attr( "src" ) );
}
Expand Down Expand Up @@ -830,6 +831,16 @@ return $.widget("ui.sortable", $.ui.mouse, {

},

_createTrPlaceholder: function(that, sourceTr, targetTr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces around the parenthesis—( sourceTr, targetTr )

that = that || this;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first parameter is unnecessary. You can change this line to var that = this;.


sourceTr.children().each(function() {
$( "<td>&#160;</td>", that.document[0] )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you just copy/pasted this, but since other code needs to be updated, might as well fix the spacing here as well.

.attr( "colspan", $( this ).attr( "colspan" ) || 1 )
.appendTo( targetTr );
});
},

_contactContainers: function(event) {
var i, j, dist, itemWithLeastDistance, posProperty, sizeProperty, cur, nearBottom, floating, axis,
innermostContainer = null,
Expand Down