How to Remove Row from Table

2019-02-28 18:22发布

This question is a follow up to this: Button to add new row in SAPUI5 table

In my new scenario, I have added a "remove" button in the first column of the table. Again, the JSON file looks like this:

{
  "Invoices": [
    {
      "ProductName": "Pineapple",
      "Quantity": 21,
      "ExtendedPrice": 87.2000,
      "ShipperName": "Fun Inc.",
      "ShippedDate": "2015-04-01T00:00:00",
      "Status": "A"
    },
    ...
  ]
}

My view now looks like this:

<mvc:View controllerName="stepbystep.demo.wt.controller.App" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m" xmlns:core="sap.ui.core"
xmlns:html="http://www.w3.org/1999/xhtml" displayBlock="true">
<Table id="ins" items="{ path : 'invoice>/Invoices', sorter : { path : 'ProductName' } }">
    <headerToolbar>
        <Toolbar>
            <Button icon="sap-icon://add" text="Row" press="addRow"/>
            <Button icon="sap-icon://display" text="Row" press="fetchRecords"/>
        </Toolbar>
    </headerToolbar>
    <columns>
        <Column width="50px"/>
        <Column hAlign="Right" minScreenWidth="Small" demandPopin="true" width="4em">
            <Text text="{i18n>columnQuantity}"/>
        </Column>
        <Column>
            <Text text="{i18n>columnName}"/>
        </Column>
        <Column minScreenWidth="Small" demandPopin="true">
            <Text text="{i18n>columnStatus}"/>
        </Column>
        <Column minScreenWidth="Tablet" demandPopin="false">
            <Text text="{i18n>columnSupplier}"/>
        </Column>
        <Column hAlign="Right">
            <Text text="{i18n>columnPrice}"/>
        </Column>
    </columns>
    <items>
        <ColumnListItem type="Navigation" press="onPress">
            <cells>
                <Button icon="sap-icon://delete" press="deleteRow" type="Reject"/>
                <ObjectNumber number="{invoice>Quantity}" emphasized="false"/>
                <ObjectIdentifier title="{invoice>ProductName}"/>
                <Text text="{ path: 'invoice>Status', formatter: '.formatter.statusText' }"/>
                <Text text="{invoice>ShipperName}"/>
                <ObjectNumber
                    number="{ parts: [{path: 'invoice>ExtendedPrice'}, {path: 'view>/currency'}], type: 'sap.ui.model.type.Currency', formatOptions: { showMeasure: false } }"
                    unit="{view>/currency}" state="{= ${invoice>ExtendedPrice} > 50 ? 'Error' : 'Success' }"/>
            </cells>
        </ColumnListItem>
    </items>
</Table>

And I have a function for adding a row (this one works fine):

addRow: function() {
    this.getView().getModel("invoice").create("/Invoices", {
        ProductName: "",
        Quantity: "",
        ShippedDate: "",
        Status: ""
    });
}

And I am trying to build one for deleting. This is what I have so far:

deleteRow: function(oArg) {
    // var array = oArg.oSource.sId.split("-");
    // var index = array[array.length - 1];

    var path = oArg.oSource.oPropagatedProperties.oBindingContexts.invoice.sPath;

    delete this.getView().getModel("invoice").oData[path.substr(1)];
    this.getView().getModel("invoice").refresh(true);
}

But the row gets emptied and then returns again (like getting fetched from the mock server again). I am trying to completely remove the row (not just its contents), and the data to be removed.

I have seen plenty of examples online, but none cover my use case.

标签: sapui5
1条回答
地球回转人心会变
2楼-- · 2019-02-28 18:54

If you already use create for create new row then I think the best is to be consistent and use remove in order to remove it. so in your case I think your code should look something like this:

this.getView().getModel("invoice").remove(path);

This line will do both:

  1. Execute DELETE request to delete the object on the server (if you are using oDataModel)
  2. Will delete the row from the table and refresh it because the table is bounded to this model

If you can please always use binding in your code. Using binding is much more efficient and easy to maintain because you don't need to deal with any DOM objects. The only thing that you need to do in code is creating/deleting/updating your model objects and UI5 runtime will do the rest for you.

查看更多
登录 后发表回答