通过XMLView

时间:2016-04-22 12:47:48

标签: sapui5

使用sap.m.Select,我的代码如下:

<m:Select
    selectedKey='{state}'
    items="{
        path: 'states>/content',
        sorter: {
            path: 'name'
        }
    }">
    <core:Item key="{states>id}" text="{states>name}" />
</m:Select>

由于希望能够在其他输入中选择状态时按国家/地区过滤状态,因此,我尝试使用filters,该文档在以下文档中定义:

问题在于我无法找到如何正确使用它的任何地方(文档,谷歌,SO,源代码,示例,测试)。我尝试了这两种方法但没有成功:

<m:Select
    selectedKey='{state}'
    items="{
        path: 'states>/content',
        sorter: {
            path: 'name'
        },
        filters: [{
            path: 'countryId',
            operator: 'EQ',
            value1: '10' // just example
        ]}
    }">
    <core:Item key="{states>id}" text="{states>name}" />
</m:Select>

# View
<m:Select
    selectedKey='{state}'
    items="{
        path: 'states>/content',
        sorter: {
            path: 'name'
        },
        filters: ['.filterByCountry'}
    }">
    <core:Item key="{states>id}" text="{states>name}" />
</m:Select>

# Controller
...
filterByCountry: new sap.ui.model.Filter({
    path: 'countryId',
    operator: 'EQ',
    value1: '10'
}),
...

有人知道使用它的正确方法吗?

1 个答案:

答案 0 :(得分:21)

以下是过滤器在XML视图中的工作原理 - 请参阅下面我为您编写的2个示例(如果他们不在stackoverflow上运行,请使用jsbin链接)。他们都使用Northwind OData服务。你会发现它非常直接:

<Select
    items="{
        path : '/Orders',
        sorter: {
            path: 'OrderDate',
            descending: true
        },
        filters : [
            { path : 'ShipCountry', operator : 'EQ', value1 : 'Brazil'}
        ]
    }">

当然,您也可以添加多个过滤器(请参阅下面的第二个示例)。

但是,请记住,过滤器是在XMLView中声明的。不幸的是,UI5当前不是那么动态,只允许使用XMLView中的绑定语法动态更改XMLView中定义的此类过滤器。相反,你需要一些JavaScript代码。在您的情况下,您可以侦听其他字段的更改事件。在事件处理程序中,您只需创建一个新的Filter并应用它:

var oSelect, oBinding, aFilters, sShipCountry;

sFilterValue = ...; // I assume you can get the filter value from somewhere...
oSelect = this.getView().byId(...); //get the reference to your Select control
oBinding = oSelect.getBinding("items");
aFilters = [];

if (sFilterValue){
    aFilters.push( new Filter("ShipCountry", FilterOperator.Contains, sFilterValue) );
}
oBinding.filter(aFilters, FilterType.Application);  //apply the filter

这应该就是你需要做的一切。下面的示例不使用任何JavaScript代码进行过滤,但我想你明白了。

<强> 1。示例 - 选择框:

运行代码:https://jsbin.com/wamugexeda/edit?html,output

&#13;
&#13;
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>SAPUI5 single file template | nabisoft</title>
        <script src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js"
            id="sap-ui-bootstrap"
            data-sap-ui-theme="sap_bluecrystal"
            data-sap-ui-libs="sap.m"
            data-sap-ui-bindingSyntax="complex"
            data-sap-ui-compatVersion="edge"
            data-sap-ui-preload="async"></script>
            <!-- use "sync" or change the code below if you have issues -->
 
        <!-- XMLView -->
        <script id="myXmlView" type="ui5/xmlview">
            <mvc:View
                controllerName="MyController"
                xmlns="sap.m"
                xmlns:core="sap.ui.core"
                xmlns:mvc="sap.ui.core.mvc">
 
                <Select
                    items="{
                        path : '/Orders',
                        sorter: {
                            path: 'OrderDate',
                            descending: true
                        },
                        filters : [
                            { path : 'ShipCountry', operator : 'EQ', value1 : 'Brazil'}
                        ]					
                    }">
                    <core:Item key="{OrderID}" text="{OrderID} - {ShipName}" />
                </Select>
 
            </mvc:View>
        </script>
 
        <script>
            sap.ui.getCore().attachInit(function () {
                "use strict";
 
                //### Controller ###
                sap.ui.define([
                    "sap/ui/core/mvc/Controller",
                    "sap/ui/model/odata/v2/ODataModel"
                ], function (Controller, ODataModel) {
                    "use strict";
 
                    return Controller.extend("MyController", {
                        onInit : function () {
                            this.getView().setModel(
                                new ODataModel("https://cors-anywhere.herokuapp.com/services.odata.org/V2/Northwind/Northwind.svc/", {
                                    json : true,
                                    useBatch : false
                                })
                            );
                        }
                    });
                });
 
                //### THE APP: place the XMLView somewhere into DOM ###
                sap.ui.xmlview({
                    viewContent : jQuery("#myXmlView").html()
                }).placeAt("content");
 
            });
        </script>
 
    </head>
 
    <body class="sapUiBody">
        <div id="content"></div>
    </body>
</html>
&#13;
&#13;
&#13;

<强> 2。示例 - 表格:

运行代码:https://jsbin.com/yugefovuyi/edit?html,output

&#13;
&#13;
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>SAPUI5 single file template | nabisoft</title>
        <script src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js"
            id="sap-ui-bootstrap"
            data-sap-ui-theme="sap_bluecrystal"
            data-sap-ui-libs="sap.m"
            data-sap-ui-bindingSyntax="complex"
            data-sap-ui-compatVersion="edge"
            data-sap-ui-preload="async"></script>
            <!-- use "sync" or change the code below if you have issues -->
 
        <!-- XMLView -->
        <script id="myXmlView" type="ui5/xmlview">
            <mvc:View
                controllerName="MyController"
                xmlns="sap.m"
                xmlns:core="sap.ui.core"
                xmlns:mvc="sap.ui.core.mvc">
 
                <Table
                    id="myTable"
                    growing="true"
                    growingThreshold="10"
                    growingScrollToLoad="true"
                    busyIndicatorDelay="0"
                    items="{
                        path : '/Orders',
                        sorter: {
                            path: 'OrderDate',
                            descending: true
                        },
                        filters : [
                            { path : 'ShipCity', operator : 'Contains', value1 : 'rio'},
                            { path : 'ShipName', operator : 'EQ', value1 : 'Hanari Carnes'}
                        ]					
                    }">
                    <headerToolbar>
                        <Toolbar>
                            <Title text="Orders of ALFKI"/>
                            <ToolbarSpacer/>
                        </Toolbar>
                    </headerToolbar>
                    <columns>
                        <Column>
                            <Text text="OrderID"/>
                        </Column>
                        <Column>
                            <Text text="Order Date"/>
                        </Column>
                        <Column>
                            <Text text="To Name"/>
                        </Column>
                        <Column>
                            <Text text="Ship City"/>
                        </Column>
                    </columns>
                    <items>
                        <ColumnListItem type="Active">
                            <cells>
                                <ObjectIdentifier title="{OrderID}"/>

                                <Text
                                    text="{
                                        path:'OrderDate',
                                        type:'sap.ui.model.type.Date',
                                        formatOptions: { style: 'medium', strictParsing: true}
                                    }"/>

                                <Text text="{ShipName}"/>

                                <Text text="{ShipCity}"/>

                            </cells>
                        </ColumnListItem>
                    </items>
                </Table>
 
            </mvc:View>
        </script>
 
        <script>
            sap.ui.getCore().attachInit(function () {
                "use strict";
 
                //### Controller ###
                sap.ui.define([
                    "sap/ui/core/mvc/Controller",
                    "sap/ui/model/odata/v2/ODataModel"
                ], function (Controller, ODataModel) {
                    "use strict";
 
                    return Controller.extend("MyController", {
                        onInit : function () {
                            this.getView().setModel(
                                new ODataModel("https://cors-anywhere.herokuapp.com/services.odata.org/V2/Northwind/Northwind.svc/", {
                                    json : true,
                                    useBatch : false
                                })
                            );
                        }
                    });
                });
 
                //### THE APP: place the XMLView somewhere into DOM ###
                sap.ui.xmlview({
                    viewContent : jQuery("#myXmlView").html()
                }).placeAt("content");
 
            });
        </script>
 
    </head>
 
    <body class="sapUiBody">
        <div id="content"></div>
    </body>
</html>
&#13;
&#13;
&#13;