SAPUI5技术问题:如何根据指定产品获取对应供应商列表?
Hey there! Let's sort out why you're seeing every supplier instead of just the ones tied to your target products (like Supplier1 and Supplier2).
What's Wrong with Your Current Code?
Right now, your Table is bound to all Products (products>/Products) and expanding the Supplier navigation property for each one. That means you're looping through every product in the Northwind dataset and displaying its supplier—hence the full list of suppliers, including duplicates if multiple products share the same supplier.
Solutions to Get Targeted Suppliers
Depending on whether you need suppliers for a single product or multiple specific products, here are two approaches:
1. For a Single Specific Product (e.g., You're on a Product Detail Page)
If your view is already bound to a single Product (like in an ObjectPage showing one product's details), you can directly bind to that product's Supplier property. Since each Northwind Product links to exactly one Supplier, you might not even need a Table—though if you prefer using one, here's how:
<ObjectPageSection title="Suppliers"> <subSections> <ObjectPageSubSection> <blocks> <!-- Bind directly to the current product's Supplier --> <m:Table id="suppliersTable" items="{path : 'products>/Supplier'}"> <m:columns> <m:Column/> </m:columns> <m:items> <m:ColumnListItem> <m:cells> <m:ObjectIdentifier text="{products>Name}"/> </m:cells> </m:ColumnListItem> </m:items> </m:Table> </blocks> </ObjectPageSubSection> </subSections> </ObjectPageSection>
If you don't need a Table, you could simplify this to just an ObjectIdentifier or Text control bound directly to products>/Supplier/Name.
2. For Multiple Specific Products (e.g., Product IDs 1 and 2)
To pull suppliers for a set of products and avoid duplicate entries (since one supplier might provide multiple products), you have two options:
Option A: Filter in XML Binding with Grouping
Add a $filter to your OData binding to target only your desired products, then use grouping to avoid duplicate suppliers:
<ObjectPageSection title="Suppliers"> <subSections> <ObjectPageSubSection> <blocks> <m:Table id="suppliersTable" items="{ path: 'products>/Products', parameters: { expand: 'Supplier', filter: 'ProductID eq 1 or ProductID eq 2' <!-- Add your product IDs here --> }, sorter: { path: 'Supplier/SupplierID', group: true <!-- Groups duplicates together --> } }"> <m:columns> <m:Column/> </m:columns> <m:items> <m:ColumnListItem> <m:cells> <m:ObjectIdentifier text="{products>Supplier/Name}"/> </m:cells> </m:ColumnListItem> </m:items> </m:Table> </blocks> </ObjectPageSubSection> </subSections> </ObjectPageSection>
Option B: Fetch and Deduplicate in the Controller
For cleaner control over deduplication, fetch the filtered products in your controller, extract unique suppliers, then bind them to your Table using a local JSON model:
First, update your XML to use a new local model:
<ObjectPageSection title="Suppliers"> <subSections> <ObjectPageSubSection> <blocks> <m:Table id="suppliersTable" items="{path : 'suppliers>/'}"> <m:columns> <m:Column/> </m:columns> <m:items> <m:ColumnListItem> <m:cells> <m:ObjectIdentifier text="{suppliers>Name}"/> </m:cells> </m:ColumnListItem> </m:items> </m:Table> </blocks> </ObjectPageSubSection> </subSections> </ObjectPageSection>
Then add this logic to your controller's onInit method:
onInit: function() { const oODataModel = this.getView().getModel("products"); const targetProductIDs = [1, 2]; // Replace with your product IDs const filterString = targetProductIDs.map(id => `ProductID eq ${id}`).join(" or "); // Fetch filtered products with their suppliers oODataModel.read("/Products", { urlParameters: { $expand: "Supplier", $filter: filterString, $select: "Supplier/SupplierID,Supplier/Name" // Only fetch needed fields }, success: (oData) => { // Deduplicate suppliers using a map const supplierMap = {}; const uniqueSuppliers = []; oData.results.forEach(product => { const supplier = product.Supplier; if (!supplierMap[supplier.SupplierID]) { supplierMap[supplier.SupplierID] = supplier; uniqueSuppliers.push(supplier); } }); // Bind the unique suppliers to the Table const oSuppliersModel = new sap.ui.model.json.JSONModel(uniqueSuppliers); this.getView().setModel(oSuppliersModel, "suppliers"); }, error: (oError) => { console.error("Failed to fetch suppliers:", oError); } }); }
Final Notes
- Adjust the
ProductIDvalues in the filter to match your target products. - If you're using a different Northwind entity structure (unlikely, but possible), double-check the navigation property name (
Supplier) is correct.
内容的提问来源于stack exchange,提问作者ravi

