SAPUI5 - Drag & Drop Row on List (Re-order Rows)
<List
id="idAttachList"
headerText="{i18n>piList}"
items="{
path: 'sendFileData>/Attach'
}"
mode="MultiSelect"
updateFinished="onAttachListUpdateFinished"
selectionChange="onAttachListSelectionChange">
<dragDropConfig>
<dnd:DragDropInfo sourceAggregation="items"
targetAggregation="items"
dropPosition="Between"
drop="onAttachRowMove" /> <items>
</dragDropConfig>
<CustomListItem>
</CustomListItem>
</items>
</List>
//------ controller.js
onAttachRowMove: function (oEvent) {
var sDropPosition = oEvent.getParameter("dragSession").getDropPosition(),
oDragControl = oEvent.getParameter("dragSession").getDragControl(),
oDropControl = oEvent.getParameter("dragSession").getDropControl(),
sDragControlPath = oDragControl.getBindingContextPath("sendFileData"),
iDragIndex = parseInt(oDragControl.getBindingContextPath("sendFileData").split("/")[2]),
iDropIndex = parseInt(oDropControl.getBindingContextPath("sendFileData").split("/")[2]),
iNewPos = iDropIndex + (sDropPosition == "Before" ? 0 : 1) + (iDragIndex < iDropIndex ? -1 : 0),
oDragItem = oDragControl.getBindingContext("sendFileData").getProperty(sDragControlPath),
oListId = oDragControl.getParent().getId(),
sBindingPath = oDragControl.getParent().getBinding("items").getPath(),
oData = this.getView().getModel("sendFileData").getProperty(sBindingPath);
// remove the item
oData.splice(iDragIndex, 1);
// insert the control in target aggregation
oData.splice(iNewPos, 0, oDragItem);
/* if (sDropPosition === "Before") {
oData.splice(iDropIndex, 0, oDragItem);
} else {
oData.splice(iDropIndex + 1, 0, oDragItem);
} */
// console.log("dragFromIndex:", iDragIndex);
// console.log("dragToIndex:", iDropIndex);
this.getView().getModel("sendFileData").setProperty(sBindingPath, oData);
//console.log(oData);
this._updateAttachListSelection(oListId);
},
_updateAttachListSelection: function (sListId) {
var oList = this._oDialogSendToVerifier.getContent().find(x => x.sId.includes(sListId)),
oItems = oList?.getItems();
if (oItems) {
oList.removeSelections(true);
oItems.forEach((oItem) => {
if (oItem.getBindingContext("sendFileData").getObject("Select")) {
oList.setSelectedItem(oItem);
}
});
}
}