I am using AngularJS and ui-sortable to reorder images to be uploaded via HTML using the following input:
<input name="userfile[]" type="file" id="userfile" ng-if="charSelected" onchange="angular.element(this).scope().filesLoaded(this.files)" multiple>
I display the images grabbed from file upload as follows:
<table>
<tr><span ng-if="charSelected && type == 'Image'">Preview (Drag to Reorder)</span></tr>
<tr ng-model="images" ui-sortable="reorderFiles">
<td ng-repeat="image in images track by $index" ng-if="charSelected && type == 'Image'"><img ng-src="{{image}}" /></td>
</tr>
</table>
I can drag and drop to reorder the images just fine. However, the problem is, I don't know how to change the order of the files when a user reorders them. I tried the following:
// Sortable change event
$scope.reorderFiles = {
update: function(e, ui) {
var originalPos = ui.item.index();
var newPos = ui.item.sortable.dropindex;
// Swap the files
var t1 = document.getElementById('userfile').files[originalPos];
console.log("orig: " + document.getElementById('userfile').files[originalPos].name);
document.getElementById('userfile').files[originalPos] = document.getElementById('userfile').files[newPos];
document.getElementById('userfile').files[newPos] = t1;
console.log("new: " + document.getElementById('userfile').files[originalPos].name);
}
}
But it seems the object returned by document.getElementById is immutable, because console.log just returns the same name even after I swap files[originalPos] with files[newPos].
How can I change the order of the files the user wants to upload?