2017-05-11 40 views
0

嗨我需要帮助与更新我的标签在一个视图中使用json模型的新值每次单击一个按钮。你如何在SAPUI5中做到这一点?SAPUI5:如何从json模型更新视图中的标签?

您可以看到问题文本和答案文本来自问卷模型我想从数据[0] .question.text更新为数据1 .question.text,它显示在我的工具栏文本中。如何在SAPUI5中实现这一点?

我已经使用工具工具栏标题的问题文本和不同的选项标签?这是对的吗?我能否更新工具栏标题文本?

请找到下面的代码:

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.onlinequestionnaire.controller.App" height="100%"> 
<Page title="Online Questionnaire" class="sapUiContentPadding" id="myPage"> 
    <headerContent> 
     <Button icon="sap-icon://action" tooltip="Share"/> 
    </headerContent> 
    <subHeader/> 
    <content> 
     <VBox class="sapUiSmallMargin"> 
      <f:SimpleForm id="SimpleFormToolbar" layout="ResponsiveGridLayout"> 
       <f:toolbar> 
        <Toolbar id="TB1"> 
         <Title text="Question 1" level="H4" titleStyle="H4"/> 
         <ToolbarSpacer/> 
         <Button icon="sap-icon://drop-down-list"/> 
        </Toolbar> 
       </f:toolbar> 
       <f:content> 
        <Toolbar design="Solid"> 
         <Title text="{questionnaire>/data/0/question/text}" level="H5" titleStyle="H5" textAlign="Center" id="questionText"/> 
        </Toolbar> 
        <VBox id="multipleChoiceHolder"> 
         <HBox id="answerRow1"> 
          <HBox width="700px" backgroundDesign="Solid" alignItems="Center" id="mCHorHolder1"><CheckBox id="checkBox1"/><Label text="Dogs" id="multipleChoice1"/></HBox> 
          <HBox width="700px" backgroundDesign="Solid" alignItems="Center" id="mCHorHolder2"><CheckBox id="checkBox2"/><Label text="Cats" id="multipleChoice2"/></HBox> 
         </HBox> 
         <HBox id="answerRow2"> 
          <HBox width="700px" backgroundDesign="Solid" alignItems="Center" id="mCHorHolder3"><CheckBox id="checkBox3"/><Label text="Rabbits" id="multipleChoice3"/></HBox> 
          <HBox width="700px" backgroundDesign="Solid" alignItems="Center" id="mCHorHolder4"><CheckBox id="checkBox4"/><Label text="Hamsters" id="multipleChoice4"/></HBox> 
         </HBox> 
        </VBox> </f:content> 
      </f:SimpleForm> 
     </VBox> 
    </content> 
    <footer> 
     <Toolbar id="toolBar"> 
      <Button text="Previous" type="Emphasized" width="300px" press="goToPrevious" icon="sap-icon://navigation-left-arrow" id="previousButton"/> 
      <ToolbarSpacer/> 
      <Button text="Next" type="Emphasized" width="300px" press="goToNext" icon="sap-icon://navigation-right-arrow" iconFirst="false" 
       id="nextButton"/> 
     </Toolbar> 
    </footer> 
</Page> 

questionnaire.json

{ data: [{ 
    "question": "Which pet do you like from the following?", 
    "answers": ["Cats", "Rabbits", "Dogs", "Hamsters"], 
    "correct": 1 
}, { 
    "question": "Which pet do you prefer from the following?", 
    "answers": ["Cats", "Dogs"], 
    "correct": 1 
}, { 
    "question": "What food brand does your pet eat?", 
    "answers": ["Pedigree", "Catfood", "Rabbitfood", "HamstersFood"], 

    "correct": 1 
}] 
} 

个App.controller.js

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


Controller.extend("opensap.onlinequestionnaire.controller.App", { 
    goToPrevious:function(){ 
     alert("Previous Question"); 
    }, 
    goToNext:function(){ 
     alert("Next Question"); 
    } 
}); 

}); 

我的前端看起来像这样:My Frontend looks like this:

回答

0

我建议你要么储存当前索引或为您的当前选择的问题,一个单独的模型。例如。

您可以定义初始化的方法和/或获取下一个元素

initOrSetNext: function() { 
    var oCurrentQuestionModel = this.getView().getModel('currentQuestion'); 
    var oQuestionModel = this.getView().getModel('questionnaire'); 
    var iCurrentIndex = oCurrentQuestionModel.getProperty('/index'); 

    // if run first time set to 0 - else increase by one 
    iCurrentIndex = iCurrentIndex ? ++iCurrentIndex : 0; 

    var oCurrent = { 
    index : iCurrentIndex, 
    question: oQuestionModel.getProperty('/data/' + iCurrentIndex); 
    } 
    oCurrentQuestionModel.setProperty('/', oCurrent); 
} 

回答更新(如上图),以显示incease。

缺少什么:加载之前检查下一个索引是否存在

+0

谢谢我喜欢新模型的当前问题的方法。如何将索引增加1并将其分配给问卷模型是一个问题,因为我知道如何在普通JavaScript中执行此操作,只需一个for循环,并将当前问题的div的ID存储在变量中,然后再增加它。但我不知道在SAPUI5中这样做。 也可以通过在模型文件夹中创建.json文件来创建这个单独的模型,还是我需要在控制器的init方法中创建它? – loki

+0

无论你喜欢 - 我会说init方法是这里最好的地方。我会更新答案以展示如何增加它。 –

+0

谢谢。我作为实习生加入SAP,我的任务是开发一个Web应用程序来显示问卷。我之前没有在SAP上工作过,所以很难理解它。 – loki