How to delete the row of sap.m.Table on click of an icon inside ColumnListItem? [duplicate]

不想你离开。 提交于 2019-12-13 06:15:10

问题


I have a JS view in which I am creating a sap.m.Table. It's "columns" are bound to a JSON model. It's "items" are bound to odata service. I have two issues I have been struggling with for a while now.

Issue 1 - How to delete the row a table on click of an icon inside columnlistitem?
Issue 2 - I have created another question for this - How to access row and column data on click of an icon in ColumnListItem

My view looks like this.

createContent : function(oController) {
    var oTable = new sap.m.Table("table1", {
            width: "auto",
            noDataText: "Please add rows to be displayed!"
        }).addStyleClass("sapUiResponsiveMargin");

    oTable.bindAggregation("columns", "Periods>/periods", function(sId, oContext) {
            var sColumnId = oContext.getObject().period;
            return new sap.m.Column({
                hAlign: "Center",
                vAlign: "Middle",
                header: new sap.m.Text({
                    text: sColumnId
                })
            });
       });

    oTable.bindItems("zStatus>/StatusSet", function(sId, oContext) { 

var row = new sap.m.ColumnListItem(sId, {
                type : "Inactive",
                cells: [
                new sap.ui.core.Icon({
                                            src: "sap-icon://delete",
                                            hoverColor: "red",
                                            activeColor: "red",
                                            press: [oController.onDeleteIconPress, oController]
                }),
                             new sap.m.Text({
                                        text: "{zStatus>Description}"
                             }),                                      
                     new sap.ui.core.Icon(sId, {
                            src: {
                                path: "zStatus>Status1",
                                formatter: function(status) {
                                    switch(status) {
                                    case "R":
                                        return "sap-icon://sys-cancel";
                                    case "G":
                                        return "sap-icon://sys-enter";
                                    case "Y":
                                        return "sap-icon://notification";
                                    default:
                                        return "sap-icon://sys-help";
                                    }
                                }
                            },
                            size: "1.5em",
                            press: [oController.onStatusIconPress, oController]
                        }) ]
    });


   return oTable;
}

In my controller I create an array, then a JSON model "Periods" from it and set it to this view. Odata model "zStatus" is defined in manifest file.

My controller code -

onInit : function() {
    // array aPeriods is populated first then
    var oPeriodsModel = new sap.ui.model.json.JSONModel();
    oPeriodsModel.setData({
        periods : aPeriods
    });
    this.getView().setModel(oPeriodsModel, "Periods");
}

onDeleteIconPress : function(oEvent) {
    // I manage to get the selected row 
    var oSelectedRow = oEvent.getSource().getParent().getBindingContext("zStatus").getObject();

    var oOriginalRows = oTable.getBinding("items").getModel().getProperty("/");
    var found = false, i;
    for (var key in oOriginalRows) {
        var oOriginalRow = oOriginalRows[key];
        if(oOriginalRow.Name === oSelectedRow.Name) {
            delete oOriginalRows[key];
            found = true;
            break;
        }
    }
    if(found) {
    // Problem 1 : Even though I delete row from the model it is still shown on the UI
    oTable.getBinding("items").getModel().setProperty("/", oOriginalRows);
        oTable.getBinding("items").getModel().refresh();
    }
}

I tried several other ways but no luck. Looks like I do not understand binding completely yet.


回答1:


Please try using

oTable.getBinding("items").getModel().refresh(true);

Here is how I would achieve the same task

 onDeleteIconPress : function(oEvent) {
 //get the index of the selected row
   var array = oEvent.oSource.sId.split('-')
   var index = array[array.length - 1];
 //get the model 
   this.getModel("Periods").YourListItemsOBJECTorArray.splice(index, 2);
 //refresh the model to let the UI know that there's change in the data.
   sap.ui.getCore().getModel("Periods").refresh(true);
}

Thanks.



来源:https://stackoverflow.com/questions/41650574/how-to-delete-the-row-of-sap-m-table-on-click-of-an-icon-inside-columnlistitem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!