mirror of
https://github.com/UndefinedOffset/SortableGridField.git
synced 2024-10-22 17:05:38 +02:00
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
This commit is contained in:
parent
ade87d5ff1
commit
b0fcb318ad
@ -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
|
@ -278,7 +278,6 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
||||
user_error('Target item not found', E_USER_ERROR);
|
||||
}
|
||||
|
||||
$sortPosition = $targetItem->$sortColumn;
|
||||
$currentPage = 1;
|
||||
|
||||
|
||||
@ -293,53 +292,84 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
} else {
|
||||
$swapItem->$sortColumn = $targetItem->$sortColumn;
|
||||
$swapItem->write();
|
||||
}
|
||||
|
||||
|
||||
//Update target item position
|
||||
if ($many_many) {
|
||||
DB::query('UPDATE "' . $table
|
||||
. '" SET "' . $sortColumn.'" = ' . $sortPosition
|
||||
. '" SET "' . $sortColumn.'" = ' . $sortPositions[0]
|
||||
. ' WHERE "' . $componentField . '" = ' . $targetItem->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
|
||||
} else {
|
||||
$targetItem->$sortColumn = $sortPosition;
|
||||
$targetItem->$sortColumn = $sortPositions[0];
|
||||
$targetItem->write();
|
||||
}
|
||||
|
||||
|
||||
//End transaction if supported
|
||||
if(DB::getConn()->supportsTransactions()) {
|
||||
DB::getConn()->transactionEnd();
|
||||
$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.'" = ' . $sortPositions[count($sortPositions) - 1]
|
||||
. ' WHERE "' . $componentField . '" = ' . $targetItem->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
|
||||
} else {
|
||||
$targetItem->$sortColumn = $sortPositions[count($sortPositions) - 1];
|
||||
$targetItem->write();
|
||||
}
|
||||
|
||||
|
||||
$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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
@ -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';
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user