简体   繁体   English

在SAPUI5表中添加新行的按钮

[英]Button to add new row in SAPUI5 table

I am trying to add new lines on a SAPUI5 table by a click of a button. 我试图通过单击按钮在SAPUI5表上添加新行。 I have seen plenty of online tutorials but I have not found exactly my use case. 我已经看过很多在线教程,但是我找不到确切的用例。

The JSON is loaded through a mock server for now (for testing purposes and has this structure: 暂时通过模拟服务器加载JSON(出于测试目的,并且具有以下结构:

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

I have a table that is defined like this in the view : 我在view有一个这样定义的表:

<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 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>
                <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 the controller contains this function (I am only pasting the part that works): 并且控制器包含此功能(我仅粘贴有效的部分):

addRow: function() {
    var oList = this.getView().byId("ins");
    var oDt = oList.getBinding("items").getModel().oData;
}

However, I am not sure how to proceed. 但是,我不确定如何进行。 I have tried push() in the model, but I am getting errors. 我曾在模型中尝试过push() ,但出现错误。 What I was thinking of doing now is to add an empty row in oDt and bind it to the table, but I am not sure how to proceed with this, and whether it is the optimal solution. 我现在oDt是在oDt添加一个空行并将其绑定到表,但是我不确定如何进行此操作以及它是否是最佳解决方案。

EDIT: How to Add a New ColumnListItem to a Table was marked as potential duplicate. 编辑: 如何将新的ColumnListItem添加到表中被标记为潜在的重复项。 This question is on the same topic, but the approach of the OP does not seem to match my use case (I am new to SAPUI5, so I might be wrong - in which case, please forgive me). 这个问题是在同一主题上,但是OP的方法似乎与我的用例不符(我是SAPUI5的新手,所以我可能错了-在这种情况下,请原谅我)。

JSONModel : JSONModel

this.getView().getModel('invoice').getProperty('/Invoices').push({/* new invoice data */})

ODataModel : ODataModel

this.getView().getModel('invoice').create('/Invoices', {/* new invoice data */})

or 要么

this.getView().getModel('invoice').createEntry('/Invoices', {/* new invoice data */})
this.getView().getModel('invoice').submitChanges();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM