Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added visual cue when moving cross pages

Implemented a better sorting algorithm for sorting cross pages

Removed sorting to first page and last page as this would mean re-indexing allot of items potentially
  • Loading branch information...
commit b0fcb318ad6d0b82edd95955e5ce7b1539ae2e54 1 parent ade87d5
@UndefinedOffset authored
View
2  README.md
@@ -16,5 +16,3 @@ To move an item to another page drag the row over the respective page button and
## @TODO
* Optimize re-ordering of a has_many relationship when sorting on a single page
-* Add visual cue that a row can be dropped over the page icons
-* Implement shifting when sorting cross pages
View
84 code/forms/GridFieldSortableRows.php
@@ -278,7 +278,6 @@ protected function sortToPage(GridField $gridField, $data) {
user_error('Target item not found', E_USER_ERROR);
}
- $sortPosition = $targetItem->$sortColumn;
$currentPage = 1;
@@ -293,54 +292,85 @@ protected function sortToPage(GridField $gridField, $data) {
}
- if ($data['Target'] == 'firstpage') {
- $sortPosition = $paginator->getItemsPerPage();
- } else if ($data['Target'] == 'previouspage') {
- $sortPosition = $paginator->getItemsPerPage() * ($currentPage - 1);
+ if ($data['Target'] == 'previouspage') {
+ $sortPositions = $items->limit($paginator->getItemsPerPage() + 1, ($paginator->getItemsPerPage() * ($currentPage - 1)) - 1)->column($sortColumn);
} else if ($data['Target'] == 'nextpage') {
- $sortPosition = ($paginator->getItemsPerPage() * $currentPage) + 1;
- } else if ($data['Target'] == 'lastpage') {
- $sortPosition = ($paginator->getItemsPerPage() * (ceil($items->count() / $paginator->getItemsPerPage()) - 1)) + 1;
+ $sortPositions = $items->limit($paginator->getItemsPerPage() + 1, $paginator->getItemsPerPage() * ($currentPage - 1))->column($sortColumn);
} else {
user_error('Not implemented: '.$data['Target'], E_USER_ERROR);
}
- if($targetItem->$sortColumn != $sortPosition) {
- //Start transaction if supported
- if(DB::getConn()->supportsTransactions()) {
- DB::getConn()->transactionStart();
- }
-
-
- //Swap with the item around the target position
- $swapItem = $items->where('"'.$sortColumn.'" >= '.$sortPosition)->First();
+ //Start transaction if supported
+ if(DB::getConn()->supportsTransactions()) {
+ DB::getConn()->transactionStart();
+ }
+
+ if($data['Target']=='previouspage') {
if ($many_many) {
DB::query('UPDATE "' . $table
- . '" SET "' . $sortColumn.'" = ' . $targetItem->$sortColumn
- . ' WHERE "' . $componentField . '" = ' . $swapItem->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
+ . '" SET "' . $sortColumn.'" = ' . $sortPositions[0]
+ . ' WHERE "' . $componentField . '" = ' . $targetItem->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
} else {
- $swapItem->$sortColumn = $targetItem->$sortColumn;
- $swapItem->write();
+ $targetItem->$sortColumn = $sortPositions[0];
+ $targetItem->write();
}
- //Update target item position
+ $i = 1;
+ foreach ($items as $obj) {
+ if ($obj->ID == $targetItem->ID) {
+ continue;
+ }
+
+
+ if ($many_many) {
+ DB::query('UPDATE "' . $table
+ . '" SET "' . $sortColumn.'" = ' . $sortPositions[$i]
+ . ' WHERE "' . $componentField . '" = ' . $obj->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
+ } else {
+ $obj->$sortColumn = $sortPositions[$i];
+ $obj->write();
+ }
+
+ $i++;
+ }
+ } else {
if ($many_many) {
DB::query('UPDATE "' . $table
- . '" SET "' . $sortColumn.'" = ' . $sortPosition
+ . '" SET "' . $sortColumn.'" = ' . $sortPositions[count($sortPositions) - 1]
. ' WHERE "' . $componentField . '" = ' . $targetItem->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
} else {
- $targetItem->$sortColumn = $sortPosition;
+ $targetItem->$sortColumn = $sortPositions[count($sortPositions) - 1];
$targetItem->write();
}
- //End transaction if supported
- if(DB::getConn()->supportsTransactions()) {
- DB::getConn()->transactionEnd();
+ $i = 0;
+ foreach ($items as $obj) {
+ if ($obj->ID == $targetItem->ID) {
+ continue;
+ }
+
+
+ if ($many_many) {
+ DB::query('UPDATE "' . $table
+ . '" SET "' . $sortColumn.'" = ' . $sortPositions[$i]
+ . ' WHERE "' . $componentField . '" = ' . $obj->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
+ } else {
+ $obj->$sortColumn = $sortPositions[$i];
+ $obj->write();
+ }
+
+ $i++;
}
}
+
+
+ //End transaction if supported
+ if(DB::getConn()->supportsTransactions()) {
+ DB::getConn()->transactionEnd();
+ }
}
}
?>
View
4 css/GridFieldSortableRows.css
@@ -8,4 +8,8 @@
.cms table.ss-gridfield-table.dragSorting tbody tr td {
cursor: move;
+}
+
+.cms table.ss-gridfield-table .datagrid-pagination button.sortablerows-droptarget {
+ background-image: url(./../images/pagination-arrows-drop.gif) !important;
}
View
9 javascript/GridFieldSortableRows.js
@@ -47,10 +47,11 @@
}
}).disableSelection();
- gridField.find('.datagrid-pagination button').each(function() {
+ gridField.find('.datagrid-pagination .ss-gridfield-previouspage, .datagrid-pagination .ss-gridfield-nextpage').each(function() {
$(this).droppable({
disabled: $(this).is(':disabled'),
accept: 'tr.ss-gridfield-item',
+ activeClass: 'sortablerows-droptarget',
tolerance: 'pointer',
drop: function(event, ui) {
gridField.find('tbody').sortable('cancel');
@@ -60,14 +61,10 @@
var target='';
- if($(this).hasClass('ss-gridfield-firstpage')) {
- target='firstpage';
- }else if($(this).hasClass('ss-gridfield-previouspage')) {
+ if($(this).hasClass('ss-gridfield-previouspage')) {
target='previouspage';
}else if($(this).hasClass('ss-gridfield-nextpage')) {
target='nextpage';
- }else if($(this).hasClass('ss-gridfield-lastpage')) {
- target='lastpage';
}
Please sign in to comment.
Something went wrong with that request. Please try again.