2014-02-25 94 views
2

即使在SAP HCM發展很有經驗,我剛開始我的追求學習UI5(使用eclipse),所以我的道歉提前如果我的問題是有點基本...SAP UI5:錯誤結合

我試圖創建一個數據綁定(基於UI5 rockstar DJ Adams的例子),但由於某種原因,沒有結果。

  1. 在我的控制器

    我已經進入下面的代碼(在OnInit函數)來創建數據,並將其提供:

    onInit: function() { 
    var cities = [ { id: "A1", name: "Kobe" }, 
          { id: "A2", name: "Hiroshoma" } 
    ]; 
         var oModel = new sap.ui.model.json.JSONModel(); 
         oModel.setData(cities); 
    
         sap.ui.getCore().setModel(oModel); 
    
    
    
    }, 
    
  2. 在我看來

    ,我嘗試使用綁定的數據下面的代碼:

         </IconTabFilter> 
             <IconTabFilter 
              binding="{/cities/0}" 
              text="{name}" 
              icon="sap-icon://group" 
              design="Horizontal"> 
    
             </IconTabFilter> 
             <IconTabFilter 
              binding="{/cities/1}" 
              text="{name} ({id})" 
              icon="sap-icon://group" 
              design="Horizontal"> 
    
             </IconTabFilter> 
    
            </items> 
           </IconTabBar> 
    </content> 
    </Page> 
    </core:View> 
    
  3. 在我的輸出中,所有元素都能正確顯示,但是我沒有得到我在模型中初始化的值。不過,我沒有得到任何錯誤,要麼

我的問題:1。 你能提供一些幫助/指導看到我犯了一個錯誤? 2.什麼是數據綁定(調試器,其他提示)時檢測問題的最簡單方法?

非常感謝您的指導,

湯姆

+0

只是從綁定中刪除'/城市'。它應該能夠獲取數據。 – Nareshkumar

回答

1

錯誤確實是在你的綁定。 儘管你有一個變量'cities',你的json上下文以'id'開始。 你可以更新您的JSON模式有根元素「城市」,或者將其設置爲你的modeldeclaration:

.setModel({城市:oModel})

至於調試,我喜歡這個標準谷歌瀏覽器瀏覽器工具。它允許手錶,斷點和(小)實時代碼更改

+3

非常感謝您的回覆。事實上,正如你所提到的,我將一個變量的名稱與根元素的名稱混合在一起。問題解決了,我可以繼續探索。 –

0

請嘗試使用模板,而不是綁定單個元素,您可以使用模板將數組綁定到單個元素。 實施例:

<List 
items="{/ProductCollection}" 
headerText="Products"> 
<items> 
    <ObjectListItem 
    title="{Name}" 
    type="Active" 
    press="onListItemPress" 
    number="{Price}" 
    numberUnit="{CurrencyCode}"> 
    <firstStatus> 
     <ObjectStatus 
     text="Overweight" 
     state="Error" /> 
    </firstStatus> 
    <secondStatus> 
     <ObjectStatus 
     text="In Stock" 
     state="Success" /> 
    </secondStatus> 
    <attributes> 
     <ObjectAttribute text="{WeightMeasure} {WeightUnit}" /> 
     <ObjectAttribute text="{Width} x {Depth} x {Height} {DimUnit}" /> 
    </attributes> 
    </ObjectListItem> 
</items> 

[實時例如:https://sapui5.hana.ondemand.com/sdk/explored.html#/sample/sap.m.sample.ObjectListItem/code]

如果要對數據更精細的控制,那麼您可以設定鍵值對多模型。

例子:

sap.ui.getCore().setModel("key",oModel); 

,並獲得價值:

sap.ui.getCore().getModel("key"); 
0

你創建模型的做法是不正確的安靜。可變城市不是對象而是數組。要麼你可以設置數據模型:

onInit: function() { 
var cities ={ "cities": [{ "id": "A1", "name": "Kobe" }, 
        { "id": "A2", "name": "Hiroshoma" } 
        ]} 
    var oModel = new sap.ui.model.json.JSONModel(); 

    oModel.setData(cities); 

    sap.ui.getCore().setModel(oModel); 

}, 

,或者你可以設置屬性:

var cities = [ { id: "A1", name: "Kobe" }, 
     { id: "A2", name: "Hiroshoma" } 
]; 
    var oModel = new sap.ui.model.json.JSONModel(); 

    oModel.setProperty("/cities", cities); 

    sap.ui.getCore().setModel(oModel); 

,你也有你的XML正確綁定。你可以按照從SapUi5探索列表項目的模板或開發指南