使用动态数组创建对象

Nali

至于我的表绑定,我需要转换此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>
品质

您应该这样进行:

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

使其成为适当的数组

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章