简体   繁体   English

在sapui5中从XML视图手动转换为Javascript视图

[英]Manually convert from XML view to Javascript view in sapui5

The documentation at https://sapui5.hana.ondemand.com/#/controls provides many SAPUI5 samples. https://sapui5.hana.ondemand.com/#/controls上的文档提供了许多SAPUI5示例。 But all the views are written in XML. 但是所有视图都是用XML编写的。 I can find examples written in Javascript elsewhere but I'm asking for a general rule to apply on XML code. 我可以在其他地方找到用Java脚本编写的示例,但是我要求一个适用于XML代码的通用规则。 Here is an example List.view.xml wich I need to manually convert to List.view.js 这是List.view.xml的示例,我需要手动将其转换为List.view.js

<mvc:View
height="100%"
controllerName="sap.m.sample.ListSelectionSearch.List"
xmlns:l="sap.ui.layout"
xmlns:core="sap.ui.core"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m">
<Page
    showHeader="false" >
    <subHeader>
        <Toolbar>
            <SearchField
                liveChange="onSearch"
                width="100%" />
        </Toolbar>
    </subHeader>
    <content>
        <List
            id="idList"
            items="{/ProductCollection}"
            selectionChange="onSelectionChange"
            mode="MultiSelect"
            includeItemInSelection="true" >
            <infoToolbar>
                <Toolbar
                    visible="false"
                    id="idInfoToolbar" >
                    <Label id="idFilterLabel" />
                </Toolbar>
            </infoToolbar>
            <items>
                <StandardListItem
                    title="{Name}"
                    description="{ProductId}"
                    icon="{ProductPicUrl}"
                    iconDensityAware="false"
                    iconInset="false" />
            </items>
        </List>
    </content>
</Page>

Any help will be appreciated. 任何帮助将不胜感激。

Here is the same in SAPUI5 's JSView with list aggregation done in the controller: 这与SAPUI5JSView的相同,在控制器中完成了列表聚合:

Alternatively, have a look at this full application Example SAPUI5 JSView Application 另外,看看这个完整的应用程序示例SAPUI5 JSView应用程序

OR 要么

Make use of the Diagnostic tool by pressing CTRL+SHIFT+ALT+S and API References 通过按CTRL + SHIFT + ALT + S和API参考来使用诊断工具

 sap.ui.define(["sap/m/Page", "sap/m/List", "sap/m/Toolbar", "sap/m/SearchField", "sap/m/Label", "sap/m/Text"], function(Page, List, Toolbar, SearchField, Label, Text) { "use strict"; return sap.ui.jsview("sap.m.sample.ListSelectionSearch.View", { getControllerName: function() { return "sap.m.sample.ListSelectionSearch.List"; }, createContent: function(oController) { var oToolbar = new Toolbar({ visible: true, content: [ new SearchField({ liveChange: function() { oController.onSearch(); }, width: "100%" }) ] }); var oInfoToolbar = new Toolbar({ content: new Toolbar(this.createId("idInfoToolbar"), { visible: true, content: new Text({ text: "Label Text" }) }) }); var oList = new List(this.createId("idList"), { mode: "MultiSelect", includeItemInSelection: true, infoToolbar: oInfoToolbar }); var oPage = new Page(this.createId("oPageId"), { height: "100%", title: "Page Title", showHeader: true, showSubHeader: true, headerContent: oToolbar, content: [oList] }); var app = new sap.m.App(); app.addPage(oPage); app.placeAt("content"); return app; } }); }); //in Controller sap.ui.define(["sap/m/StandardListItem", "sap/ui/model/json/JSONModel"], function(StandardListItem, JSONModel) { "use strict"; var oData = { "ProductCollection": [{ "titleId": 0, "Name": "Olayinka Otuniyi", "ProductId": "001", "ProductPicUrl": "sap-icon://competitor" }, { "titleId": 1, "Name": "Maria Anders", "ProductId": "002", "ProductPicUrl": "sap-icon://badge" }, { "titleId": 2, "Name": "Ana Trujillo", "ProductId": "003", "ProductPicUrl": "sap-icon://broken-link" }, { "titleId": 3, "Name": "Thomas Hardy", "ProductId": "004", "ProductPicUrl": "sap-icon://create" }, { "titleId": 4, "Name": "Christina Berglund", "ProductId": "005", "ProductPicUrl": "sap-icon://pending" }, { "titleId": 5, "Name": "Hanna Moos", "ProductId": "006", "ProductPicUrl": "sap-icon://decision" }, { "titleId": 6, "Name": "Martín Sommer", "ProductId": "007", "ProductPicUrl": "sap-icon://process" }, { "titleId": 7, "Name": "Laurence Lebihans", "ProductId": "008", "ProductPicUrl": "sap-icon://accept" }, { "titleId": 8, "Name": "Elizabeth Lincoln", "ProductId": "009", "ProductPicUrl": "sap-icon://alert" }] }; return sap.ui.controller("sap.m.sample.ListSelectionSearch.List", { // onInit: function() { // }, onAfterRendering: function() { var oModel = new JSONModel(oData); this.getView().setModel(oModel, "products"); var oTemplate = new StandardListItem({ title: "{products>Name}", description: "{products>ProductId}", icon: "{products>ProductPicUrl}", iconDensityAware: false, iconInset: false, type: "Active" }); oTemplate.attachPress(this.onSelectionChange, this); this.getView().byId("idList").bindItems({ path: "/ProductCollection", template: oTemplate, model: "products" }); }, onSearch: function() { console.log("Searching"); }, onSelectionChange: function() { console.log("changing Selection"); } }); }); 

Should not be that difficult: 应该没有那么困难:

new sap.m.Page({
    showHeader: false,
    subHeader: new sap.m.Toolbar({
        content: [ // (**)
            new sap.m.SearchField({
                liveChange: onSearch, // event handler
                width: "100%"
            })
        ]
    }),
    content: [
        new sap.m.List({
            //...
        })
    ]
});

OR you can keep writing XML and then create JS instances from it using API: 或者,您可以继续编写XML,然后使用API​​从中创建JS实例:

(**) here is probably the trickiest part. (**)这可能是最棘手的部分。 How could you know that this should be wrapped inside "content" property? 您怎么知道应该将其包装在“ content”属性中? Very easy! 很容易! If you see one control inside another directly (without any tags around it), it means inner control is in default aggregation of parent control. 如果直接在另一个控件内部看到一个控件(周围没有任何标签),则表示内部控件默认是父控件的集合。 So, all you need to do is check what is the name of the default aggregation of the parent control. 因此,您要做的就是检查父控件的默认聚合的名称是什么。 In case of sap.m.Toolbar it's a content . 如果是sap.m.Toolbar则为 content

UPDATE: however, it might be difficult to understand which aggregation is default, because in our docs we do not show this information. 更新:但是,可能很难理解默认的聚合,因为在我们的文档中我们没有显示此信息。 I will contact responsible team on this matter. 我将就此事与负责的小组联系。 As a work around it's possible to get this information from the source code, eg sap.m.Page - see defaultAggregation definition in metadata description. 作为一种变通方法,可以从源代码中获取此信息,例如sap.m.Page-请参见元数据描述中的defaultAggregation定义。

Please see the example below 请看下面的例子

View: 视图:

sap.ui.jsview("ResourceRootName.view.ViewName", {

    getControllerName: function() {
        return "ResourceRootName.view.ViewName";
    },

    createContent : function(oController) {

        this.oList = new sap.m.List({
            showUnread: true,
            mode: sap.ui.Device.system.phone ? sap.m.ListMode.None : sap.m.ListMode.SingleSelectMaster,
            itemPress: [oController.onListSelect, oController]
        });

        this.page = new sap.m.Page({
            title: "{i18n>pageTitle}",
            navButtonText: "Home",
            showNavButton: true,
            navButtonPress: function() {
                oController.handleNavBack();
            },
            subHeader: new sap.m.Bar({
                contentMiddle: [
                    new sap.m.SearchField(oController.createId("searchFieldTasks"), {
                        width: "100%"
                    })
                ]
            }),
            content: [this.oList]
        }); 

        return this.page; //Note: if you need to return more than two controls you can do so by using array
    }
});

In controller you can bind your list as below 在控制器中,您可以如下绑定列表

Controller: 控制器:

this.getView().oList.bindAggregation("items", {
      path: "/EntityCollectionSet",
      template: new sap.m.StandardListItem({
                title: "{Title}",
                description: "{Description}"
            }),
      filters: []//If you want to pass any filters
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM