Skip to content

Instantly share code, notes, and snippets.

@kevinruscoe
Last active August 29, 2015 14:17
Show Gist options
  • Save kevinruscoe/310ed7b409caa986348c to your computer and use it in GitHub Desktop.
Save kevinruscoe/310ed7b409caa986348c to your computer and use it in GitHub Desktop.
// jquery sortable for team / gallery priority ordering
$(function() {
var container = $('.sortable-container');
//* user resorts list
container.sortable({
update: function( event, ui ) {
update_via_ajax();
}
});
//* remove item from list
$(document).on('click', '.image .glyphicon-remove', function(e) {
$(this).parent().remove();
update_via_ajax();
});
function update_via_ajax() {
//* get post IDs in new list order
var sortList = $('.ui-sortable-handle').map(function () {
return $(this).data('post-id');
}) // reduce to low-level array
.get();
//* ajax request to update ACF repeater field
$.ajax({
type : "post",
datatype : 'json',
url : hipgive_ajax.ajaxurl,
data : ({ action: 'hipgive_ajax', ajax_method : 'sort_images', value: sortList }),
success: function(response) {
// update badge index - no baring on actual list order in back end
$('.badge').each(function(index) {
$(this).html(index+1);
});
container.fadeTo( "fast", 1, function() {
// Animation complete
});
},
error: function(x, t, m) {
console.log( x, t, m);
}
}); // ajax request
// fade out container while ajax processes
container.fadeTo( "slow" , 0.3, function() {
// Animation complete.
});
} // end udpate_via_ajax()
}); // sortable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment