使用动态数组创建对象

时间:2016-06-24 13:59:29

标签: javascript sapui5

至于我的表绑定,我需要转换这个JSON对象:

{"id":"txn_18Ptu52eZvKYlo2C9c8ejhap","amount":999,"availableOn":1467331200,"created":1466773164,"currency":"usd"}

这样的事情:

{"data":[{"id":"txn_18Ptu52eZvKYlo2C9c8ejhap","amount":999,"availableOn":1467331200,"created":1466773164,"currency":"usd"}]}

我试过这个:

success : function(data1, textStatus, jqXHR) {
                    var resp = [data1];
                    var response = {data: resp};
                    var oJSonModel = new sap.ui.model.json.JSONModel(response);
                    this.getView().setModel(oJSonModel);

data1是我们上面得到的对象,然后我将它存储到一个名为resp的数组中,然后我尝试将其存储到一个新对象中,但是我猜错了,因为它似乎只存储了该数组的第二个条目是因为我的调试器向我显示了这个:

response = Object {data: Array[1]}

然后当然我的绑定没有工作.....

在这里编辑我的完整代码:

sap.ui.define([
    'sap/ui/core/mvc/Controller',
    'sap/m/MessageToast',
    'somepath/balance/model/formatter'
], function(Controller, MessageToast, formatter) {
    "use strict";

    return Controller.extend("somepath.balance.controller.Balance", {

        formatter: formatter,

        onInit: function() {
            this.getView().setModel(new sap.ui.model.json.JSONModel());
        },

        onTransactionList: function() {
            this.getView().getModel().loadData("/balance/retrieveTransacationList?limit=" + this.getView().byId("quantity").getValue());
        },

        onTransactionByTransactionId: function() {
            var that = this;

            jQuery.ajax({
                type: "GET",
                contentType: "application/json",
                url: "/balance/retrieveTransaction?transactionId=" + that.getView().byId("searchById").getValue(),
                processData: true,
                dataType: "text",
                async: false,

                success: function(data1, textStatus, jqXHR) {
                    var resp = [];
                    resp.push(data1);
                    var response = {
                        data: resp[0]
                    };
                    var oJSonModel = new sap.ui.model.json.JSONModel(response);
                    that.getView.setModel(oJSonModel);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    var sErrorText = "" + jqXHR.status + " - " + errorThrown + " - " + jqXHR.responseText;
                    MessageToast.show(sErrorText);
                }
            });
        }
    });
});

我的观点中的表格:

    <t:Table id="transactionUiTable" 
                columnHeaderVisible="true" 
                selectionMode="Single" 
                selectionBehavior="RowSelector" 
                enableColumnReordering="false" 
                enableGrouping="false" 
                showColumnVisibilityMenu="false" 
                enableSelectAll="false" 
                enableCustomFilter="false"
                enableBusyIndicator="false"
                rows="{path: '/data'}" 
                rowSelectionChange="onTableSelectionChange">
                    <t:toolbar>
                        <Toolbar id="toolbar">
                                <Input width="15%" id="searchById" value=""/> 
                                <Button text="{i18n>searchTransaction}" type="Emphasized" icon="sap-icon://search" press="onTransactionByTransactionId"/> 
                                <Button text="{i18n>customerTransactionlist}" type="Emphasized" icon="sap-icon://search" press="onCustomerTransactionList"/>
                                <ToolbarSpacer/>
                                <Input width="5%" id="quantity" value=""/>
                                <Button text="{i18n>transactionList}" type="Unstyled" press="onTransactionList"/>
                                <Button icon="sap-icon://action-settings" type="Default"/>
                        </Toolbar>
                    </t:toolbar>
                 <t:columns>
                        <t:Column id="Id" hAlign="Center">
                            <Label id="labelId" text="{i18n>transactionId}"/>
                            <t:template>
                                <Text text="{id}"/>
                            </t:template>
                        </t:Column>
                        <t:Column id="columnDatetime" hAlign="Center">
                            <Label id="labelDatetime" text="{i18n>datetime}"/>
                            <t:template>
                                <Text text="{parts: [                
                                {path: 'created'}               
                                ],               
                                formatter : '.formatter.date'}"/>
                            </t:template>
                        </t:Column>
                        <t:Column id="columnAmount" hAlign="Center">
                            <Label id="labelAmount" text="{i18n>amount}"/>
                            <t:template>
                                <Text text="{
                                             parts: [
                                             {path: 'amount'}, 
                                             {path: 'currency'}
                                            ],
                                            type: 'sap.ui.model.type.Currency',
                                            formatOptions: {
                                             showMeasure: false}}"/>
                            </t:template>
                        </t:Column>
                        <t:Column id="columnCurrency" hAlign="Center">
                            <Label id="labelCurrency" text="{i18n>currency}"/>
                            <t:template>
                                <Text text="{currency}"/>
                            </t:template>
                        </t:Column>
                        <t:Column id="columnFee" hAlign="Center">
                            <Label id="labelFee" text="{i18n>fee}"/>
                            <t:template>
                                <Text text="{
                                             parts: [
                                             {path: 'fee'}, 
                                             {path: 'currency'}
                                            ],
                                            type: 'sap.ui.model.type.Currency',
                                            formatOptions: {
                                             showMeasure: false}}"/>
                            </t:template>
                        </t:Column>
                        <t:Column id="columnNet" hAlign="Center">
                            <Label id="labelNet" text="{i18n>net}"/>
                            <t:template>
                                <Text text="{
                                             parts: [
                                             {path: 'net'}, 
                                             {path: 'currency'}
                                            ],
                                            type: 'sap.ui.model.type.Currency',
                                            formatOptions: {
                                             showMeasure: false}}"/>
                            </t:template>
                        </t:Column>
                        <t:Column id="columnType" hAlign="Center">
                            <Label id="labelType" text="{i18n>type}"/>
                            <t:template>
                                <Text text="{type}"/>
                            </t:template>
                        </t:Column>
                            <t:Column id="columnStatus" hAlign="Center">
                            <Label id="labelStatus" text="{i18n>status}"/>
                            <t:template>
                                <Text text="{status}"/>
                            </t:template>
                        </t:Column>
                    </t:columns>
                </t:Table>

1 个答案:

答案 0 :(得分:0)

你应该这样做:

var resp = [];
resp.push(data1);

使其成为正确的数组