Skip to content
Open
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
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ pix_sortable_behavior:
AppBundle/Entity/Baz: rang
sortable_groups:
entities:
AppBundle/Entity/Baz: [ group ]
AppBundle/Entity/Baz: [ group ] #Sortable groups goes here or in your doctrine mapping if you use Gedmo

```

#### Use a draggable list instead of up/down buttons
In order to use a draggable list instead of up/down buttons, change the template in the ```move``` action to ```AppBundle:Admin:_sort_drag_drop.html.twig```.
In order to use a draggable list instead of up/down buttons, change the template in the ```move``` action to ```PixSortableBehaviorBundle:Default:_sort_drag_drop.html.twig```.

```php
<?php
Expand All @@ -43,8 +43,9 @@ In order to use a draggable list instead of up/down buttons, change the template
->add('_action', null, array(
'actions' => array(
'move' => array(
'template' => 'AppBundle:Admin:_sort_drag_drop.html.twig',
'template' => 'PixSortableBehaviorBundle:Default:_sort_drag_drop.html.twig',
'enable_top_bottom_buttons' => true, //optional
'groups' => array('group') //only if you've defined a sortable group for your entity in the config
),
),
))
Expand Down Expand Up @@ -78,8 +79,9 @@ pixSortableBehaviorBundle.error
->add('_action', null, array(
'actions' => array(
'move' => array(
'template' => 'AppBundle:Admin:_sort.html.twig',
'enable_top_bottom_buttons' => true,
'template' => 'PixSortableBehaviorBundle:Default:_sort.html.twig',
'enable_top_bottom_buttons' => false, //Disabled buttons
'groups' => array('group') //only if you've defined a sortable group for your entity in the config
),
),
))
Expand Down
13 changes: 11 additions & 2 deletions Resources/public/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ DraggableTable.prototype.init = function (node, settings) {
var moved = $(ui.item).find('.js-sortable-move');
var newPosition = ui.item.index();

groups = moved.data('group');
if (groups) {
var list = $(ui.item).parent().children()
group = list.filter(function() {
return $(this).find('.js-sortable-move').data("group") == groups;
});
newPosition = group.index(ui.item);
}

$.ajax({
'type': 'GET',
'url': moved.data('url').replace('NEW_POSITION', newPosition),
Expand All @@ -45,10 +54,10 @@ DraggableTable.prototype.init = function (node, settings) {
$(document).trigger("pixSortableBehaviorBundle.success", [data]);
},
'error': function(data) {
$(document).trigger("pixSortableBehaviorBundle.error",[data]);
$(document).trigger("pixSortableBehaviorBundle.error", [data]);
}
});

}
}).disableSelection();
};
};
8 changes: 6 additions & 2 deletions Resources/views/Default/_sort_drag_drop.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
{% set current_position = currentObjectPosition(object) %}
{% set last_position = lastPosition(object) %}
{% set enable_top_bottom_buttons = field_description.options.actions.move.enable_top_bottom_buttons ?? true %}
{% if field_description.options.actions.move.groups is defined %}
{% set groups = field_description.options.actions.move.groups %}
{% endif %}


<span class="btn btn-sm btn-default js-sortable-move" data-url="{{ admin.generateObjectUrl('move', object, {'position': 'NEW_POSITION'}) }}">
<span class="btn btn-sm btn-default js-sortable-move"
data-url="{{ admin.generateObjectUrl('move', object, {'position': 'NEW_POSITION'}) }}"
{% if groups is defined and groups is iterable %}data-group="{% for group in groups %}{{ group~attribute(object, group).id }}{% endfor %}"{% endif %}>
<i class="fa fa-arrows"></i>
</span>

Expand Down
10 changes: 2 additions & 8 deletions Services/PositionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,19 @@ public function getPosition($object, $movePosition, $lastPosition)

switch ($movePosition) {
case 'up' :
if ($currentPosition > 0) {
$newPosition = $currentPosition - 1;
}
break;

case 'down':
if ($currentPosition < $lastPosition) {
$newPosition = $currentPosition + 1;
}
break;

case 'top':
if ($currentPosition > 0) {
$newPosition = 0;
}
break;

case 'bottom':
if ($currentPosition < $lastPosition) {
$newPosition = $lastPosition;
}
break;

default:
Expand All @@ -143,6 +135,8 @@ public function getPosition($object, $movePosition, $lastPosition)
}

}
//Asserts the position is in the range
$newPosition = max(0, min($newPosition, $lastPosition));

return $newPosition;
}
Expand Down