2017-05-17 32 views
-1

我正在嘗試使用List控件並將數據從json模型綁定到它。 我從我的JSON模型得到的初始屬性在我的列表,顯示如下:爲什麼我的列表項中沒有數據?

<List headerText = "{/question}" ></List>

但後來我的JSON模式也有它由answerText財產的答案的集合。你可以在我的控制器中看到這個模型,如下面的App.controller.js。

問題:每當我嘗試加載頁面時,headerText從存儲在json模型中的文本中顯示出來,但不是在列表中顯示集合,而是顯示沒有數據。

請檢查我的代碼並給我一個合適的解決方案。

App.view.xml

<mvc:View xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m" xmlns:l="sap.ui.layout" xmlns:f="sap.ui.layout.form" xmlns:core="sap.ui.core" 
displayBlock="true" controllerName="opensap.examples.controller.App" height="100%"> 
<Page title="List Page"> 
    <content> 
     <List headerText="{/question}" id="myList" items="{ path: '/answers' }"> 
      <items> 
       <InputListItem label="{answerText}"> 
        <CheckBox></CheckBox> 
       </InputListItem> 
      </items> 
     </List> 
     <Button press="load" text="Click Me"></Button> 
    </content> 
</Page></mvc:View> 

App.controller.js

sap.ui.define([ 
"sap/ui/core/mvc/Controller", 
"sap/ui/model/json/JSONModel"], function(Controller, JSONModel) { 

var oData = { 
    "question": "Which pet do you like from the following?", 
    "answers": [{ 
     "answerText": "Cats" 
    }, { 
     "answerText": "Rabbits" 
    }, { 
     "answerText": "Dogs" 
    }, { 
     "answerText": "Hamsters" 
    }] 
}; 

var oModel; 

Controller.extend("opensap.examples.controller.App", { 

    onInit: function() { 
     oModel = new JSONModel(); 
     oModel.setData(oData); 
     this.getView().setModel(oModel); 
}, 
    load: function() { 
     console.log(oModel); 
    } 

}); 
}); 

輸出:output

回答

0

請嘗試以下的變化,它可以幫助你

在控制器,

var oModel = new sap.ui.model.json.JSONModel(); 
oModel.setData(oData); 
this.getView().setModel(oModel, "myModel"); 

在XML視圖中,

<List headerText="{myModel>/question}" id="myList" items="{ path: 'myModel>/answers' }"> 
    <items> 
     <InputListItem label="{myModel>answerText}"> 
     <CheckBox></CheckBox> 
     </InputListItem> 
    </items> 
</List> 
+0

謝謝您的輸入。我嘗試了這個,但仍然得到了與之前「無數據」相同的響應。 – loki

+0

我得到它的工作。我必須刪除路徑:從項目,需要重寫爲項目=「{myModel>答案/}」。 謝謝你的幫助。 – loki

相關問題