2016-06-24 42 views
0

至於我的表結合我需要這個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

'obj.data [0]'應該工作。 – Tushar

+1

1'Array [1]'是長度。而不是指數。 – BenG

+0

謝謝你的工作,但我得到了我的JsonModel的問題,因爲我得到這個錯誤:sap-ui-core.js:71 Uncaught TypeError:無法讀取undefined的屬性'setModel' – Nali

回答

0

你SH烏爾德這樣來做:

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

,使其正確的陣列

+0

是迄今爲止工作,但我仍然錯誤與我的setModel:sap-ui-core.js:71 Uncaught TypeError:無法讀取未定義的屬性'setModel' – Nali

+0

'this.oView'解析? – Qualiture

+0

nope我已經this.oView.setModel(oJSonModel); – Nali