How to delete a row from a Table in SAPUI5 Application when I used Model as XMLModel


I have created SAPUI5 application, in that I have loaded data from external .xml file into a table, it was fine. Now, I am trying to delete a specific row from that table.
For this purpose, I use this code:

var oModel = new sap.ui.model.xml.XMLModel();
oModel.loadData("Deployments.xml", "", false);
oTable.bindRows("/service");   // here "service"  is the root element of xml file
    var oTable = new sap.ui.commons.Button({
    text: "Delete Service",
    press: function() {
        var idx = oTable.getSelectedIndex();
        if (idx !== -1) {
            var m = oTable.getModel();
            var data = m.getData();
            var removed = data.splice(idx, 1); // error showing at this line
  [0]) + 'is removed');

        } else {
  'Please select a row');

But, I am getting error at the line: var removed = data.splice(idx, 1);. However, the same code is good for when model is JSON. How can I delete a specific row from a table when model XMLModel?

Best Solution

It is a lot easier an more reliable to use a Bindings BindingPath to manipulate data belonging to a particular binding. Here is your adapted sample for a XMLModel:

press: function() {
    var iIdx = oTable.getSelectedIndex();
    var sPath = oTable.getContextByIndex(iIdx).getPath();
    var oObj = oTable.getModel().getObject(sPath);

This way you save the hazzle of dealing with the XML structure and furthermore this will scale with any change in the binding path you might introduce in the future.

BR Chris

Related Question