UI5 OData将多个实体绑定到sap.m.Table

问题描述:

我正在使用Northwind数据库测试UI5应用程序。我试图将两个实体组合在一起(ProductsSuppliers)。我想在表格中向每个产品展示供应商。UI5 OData将多个实体绑定到sap.m.Table

我设法使用parameters:{expand : 'Supplier'}sap.m.Tableitems参数中工作,但是,这将带回每个字段的所有数据。

所以它带回类似下面的内容(其中前两个列从/Products拉到最后一列从/Supplier拉:

ProductName UnitPrice SupplierName 
Bread   10   Big Bread Co 

目前我的路径设置为:

items="{ 
    path: '/Products', 
    sorter: { 
     path: 'ProductName', 
     descending: false 
    } 
}" 

,我曾经试图访问使用供应商公司名称:

<Text text="{Suppliers/CompanyName}"/>

但是,我可以看到为什么这不起作用,但我无法弄清楚如何让它工作。

感谢您的帮助。

请看看我有如下尝试:

<Table 
    id="table" 
    width="auto" 
    class="sapUiResponsiveMargin" 
    items="{ 
     path: '/Products', 
     sorter: { 
      path: 'ProductName', 
      descending: false 
     } 
    }" 
    noDataText="{worklistView>/tableNoDataText}" 
    busyIndicatorDelay="{worklistView>/tableBusyDelay}" 
    growing="true" 
    growingScrollToLoad="true" 
    updateFinished="onUpdateFinished"> 

    <!-- parameters:{expand : 'Category'}, --> 
    <headerToolbar> 
     <Toolbar id="toolbar"> 
      <Title id="tableHeader" text="{worklistView>/worklistTableTitle}" /> 
      <ToolbarSpacer /> 
      <SearchField id="searchField" tooltip="{i18n>worklistSearchTooltip}" search="onSearch" width="auto" /> 
     </Toolbar> 
    </headerToolbar> 
    <columns> 
     <Column id="nameColumn"> 
      <Text text="{i18n>tableNameColumnTitle}" id="nameColumnTitle" /> 
     </Column> 
     <Column id="unitNumberColumn" hAlign="End"> 
      <Text text="{i18n>tableUnitNumberColumnTitle}" id="unitNumberColumnTitle" /> 
     </Column> 
     <Column> 
      <Text text="SupplierName" id="SupplierName" /> 
     </Column> 
    </columns> 
    <items> 
     <ColumnListItem type="Navigation" press="onPress"> 
      <cells> 
       <ObjectIdentifier title="{ProductName}" /> 
       <ObjectNumber number="{ path: 'UnitPrice', formatter: '.formatter.numberUnit' }" /> 
       <Text text="{Suppliers/CompanyName}" /> 
      </cells> 
     </ColumnListItem> 
    </items> 
</Table> 

产品:

<EntityType Name="Product"> 
    <Key> 
     <PropertyRef Name="ProductID" /> 
    </Key> 
    <Property xmlns:p6="http://schemas.microsoft.com/ado/2009/02/edm/annotation" Name="ProductID" Type="Edm.Int32" Nullable="false" p6:StoreGeneratedPattern="Identity" /> 
    <Property Name="ProductName" Type="Edm.String" Nullable="false" MaxLength="40" FixedLength="false" Unicode="true" /> 
    <Property Name="SupplierID" Type="Edm.Int32" /> 
    <Property Name="CategoryID" Type="Edm.Int32" /> 
    <Property Name="QuantityPerUnit" Type="Edm.String" MaxLength="20" FixedLength="false" Unicode="true" /> 
    <Property Name="UnitPrice" Type="Edm.Decimal" Precision="19" Scale="4" /> 
    <Property Name="UnitsInStock" Type="Edm.Int16" /> 
    <Property Name="UnitsOnOrder" Type="Edm.Int16" /> 
    <Property Name="ReorderLevel" Type="Edm.Int16" /> 
    <Property Name="Discontinued" Type="Edm.Boolean" Nullable="false" /> 
    <NavigationProperty Name="Category" Relationship="NorthwindModel.FK_Products_Categories" ToRole="Categories" FromRole="Products" /> 
    <NavigationProperty Name="Order_Details" Relationship="NorthwindModel.FK_Order_Details_Products" ToRole="Order_Details" FromRole="Products" /> 
    <NavigationProperty Name="Supplier" Relationship="NorthwindModel.FK_Products_Suppliers" ToRole="Suppliers" FromRole="Products" /> 
</EntityType> 

供应商:

<EntityType Name="Supplier"> 
    <Key> 
     <PropertyRef Name="SupplierID" /> 
    </Key> 
    <Property xmlns:p6="http://schemas.microsoft.com/ado/2009/02/edm/annotation" Name="SupplierID" Type="Edm.Int32" Nullable="false" p6:StoreGeneratedPattern="Identity" /> 
    <Property Name="CompanyName" Type="Edm.String" Nullable="false" MaxLength="40" FixedLength="false" Unicode="true" /> 
    <Property Name="ContactName" Type="Edm.String" MaxLength="30" FixedLength="false" Unicode="true" /> 
    <Property Name="ContactTitle" Type="Edm.String" MaxLength="30" FixedLength="false" Unicode="true" /> 
    <Property Name="Address" Type="Edm.String" MaxLength="60" FixedLength="false" Unicode="true" /> 
    <Property Name="City" Type="Edm.String" MaxLength="15" FixedLength="false" Unicode="true" /> 
    <Property Name="Region" Type="Edm.String" MaxLength="15" FixedLength="false" Unicode="true" /> 
    <Property Name="PostalCode" Type="Edm.String" MaxLength="10" FixedLength="false" Unicode="true" /> 
    <Property Name="Country" Type="Edm.String" MaxLength="15" FixedLength="false" Unicode="true" /> 
    <Property Name="Phone" Type="Edm.String" MaxLength="24" FixedLength="false" Unicode="true" /> 
    <Property Name="Fax" Type="Edm.String" MaxLength="24" FixedLength="false" Unicode="true" /> 
    <Property Name="HomePage" Type="Edm.String" MaxLength="Max" FixedLength="false" Unicode="true" /> 
    <NavigationProperty Name="Products" Relationship="NorthwindModel.FK_Products_Suppliers" ToRole="Products" FromRole="Suppliers" /> 
</EntityType> 

首先,有什么不对的扩大完全Supplier实体。它不应该对响应的大小/加载时间产生巨大影响。

事实上,如果你打开Chrome浏览器开发工具并跟踪您当前的请求,并低于我的解决方案的时候,两次都将是非常相似(它甚至更快没有选择)

enter image description here

enter image description here

当您比较响应的大小时,它肯定会有所不同。

  • 没有$选择,你会得到Content-Length:2547
  • 随着$选择,你会得到Content-Length:836(所以只有三分之一)

注意,单位是字节!所以这两个请求仍然非常小。


但是,为了回答你的问题:从理论上讲,$select运营商结合嵌套的URL应该可以帮助您。

但是,请记住在OData v4中添加的嵌套URL。

这是给你你想要的结果

http://services.odata.org/V4/Northwind/Northwind.svc/Products?$select=ProductName,UnitPrice&$expand=Supplier($select=CompanyName)

你可以看到,我的主要实体(产品)应用了$select的URL。如果你想从Products所有属性,但Suppliers的只有CompanyName,只需将父级$select改变$select=*

http://services.odata.org/V4/Northwind/Northwind.svc/Products?$select=*&$expand=Supplier($select=CompanyName)

+0

感谢您的回复。如何将嵌套的URL包含到我的XML代码中(即插入到我的表格行中)。 – neeko

+0

这是一个URL参数,所以我会尝试使用'items'聚合的参数'parameter'。 – Marc

你需要做两件事情:

1集参数:

items="{ 
    path: '/Products', 
    parameters:{expand : 'Supplier'} 
    sorter: { 
      path: 'ProductName', 
      descending: false 
      } 
}" 

2.绑定导航属性来获取性能

<Text text="{Supplier/CompanyName}"/> 

请注意,您有供应商这是不对的,因为Supplier是 导航性能

PS:这是一个从Marc

非常详细的&有益的启示