Add callback for handling sorted repeater items

This commit is contained in:
Ben Thomson 2019-04-08 23:40:41 +08:00
parent 98832fbc57
commit f49b5ab4b8
3 changed files with 16 additions and 2 deletions

View File

@ -322,6 +322,11 @@ class Repeater extends FormWidgetBase
return $widget->onRefresh();
}
public function onReorder()
{
// Handle reordering of repeater items
}
//
// Group mode
//

View File

@ -35,6 +35,7 @@
Repeater.DEFAULTS = {
sortableHandle: '.repeater-item-handle',
sortableHandler: null,
sortableContainer: 'ul.field-repeater-items',
titleFrom: null,
minItems: null,
@ -232,13 +233,20 @@
Repeater.prototype.onSortStart = function($item, container, callback, event) {
this.sortingStartIndex = $item.index()
callback($item, container, callback, event);
callback($item, container, callback, event)
}
Repeater.prototype.onSortStop = function($item, container, callback, event) {
var endIndex = $item.index()
callback($item, container, callback, event);
this.$el.request(this.options.sortableHandler, {
data: {
_repeater_index: this.sortingStartIndex,
_repeater_new_index: endIndex
}
})
callback($item, container, callback, event)
}
// FIELD REPEATER PLUGIN DEFINITION

View File

@ -3,6 +3,7 @@
<?= $titleFrom ? 'data-title-from="'.$titleFrom.'"' : '' ?>
<?= $minItems ? 'data-min-items="'.$minItems.'"' : '' ?>
<?= $maxItems ? 'data-max-items="'.$maxItems.'"' : '' ?>
data-sortable-handler="<?= $this->getEventHandler('onReorder') ?>"
data-sortable-container="#<?= $this->getId('items') ?>"
data-sortable-handle=".<?= $this->getId('items') ?>-handle">