ui5_Walkthrough_Step 31: Routing with Parameters 带参数路由

一、基本概念

二、练习

新建文件夹30,复制练习ui5_Walkthrough_Step 30: Routing and Navigation 路由和导航 文件夹下内容。

2.1 webapp/manifest.json

将上一练习manifest.json文件中配置的路由规则routes中detail路由的"pattern"从

"pattern": "detail", 改为**"pattern": "detail/{invoicePath}",**

javascript 复制代码
{
  "_version": "1.12.0",
  "sap.app": {
    "id": "sap.ui5.walkthrough",
    "type": "application",
    "i18n": "i18n/i18n.properties",
    "title": "{{appTitle}}",
    "description": "{{appDescription}}",
    "applicationVersion": {
      "version": "1.0.0"
    },
    	  "dataSources": {
            "invoiceRemote": {
              "uri": "V2/Northwind/Northwind.svc/",
              "type": "OData",
              "settings": { 
              "odataVersion": "2.0"
                          }
                    }
	}
  },
  "sap.ui": {
    "technology": "UI5",
    "deviceTypes": {
      "desktop": true,
      "tablet": true,
      "phone": true
    }
  },
  "sap.ui5": {
    "rootView": {
      "viewName": "sap.ui5.walkthrough.view.App",
      "type": "XML",
      "async": true,
      "id": "app"
    },
    "resources": {
      "css": [
        {
          "uri": "css/style.css"
        }
      ]
    },
    "dependencies": {
      "minUI5Version": "1.93",
      "libs": {
        "sap.ui.core": {},
        "sap.m": {}
      }
    },
    "models": {
      "i18n": {
        "type": "sap.ui.model.resource.ResourceModel",
        "settings": {
          "bundleName": "sap.ui5.walkthrough.i18n.i18n",
          "supportedLocales": [],
          "fallbackLocale": ""
        }
      },
      
      "invoice": {
			"dataSource": "invoiceRemote"
		}
    },
    "routing": {
	  "config": {
		"routerClass": "sap.m.routing.Router",
		"type": "View",
		"viewType": "XML",
		"path": "sap.ui5.walkthrough.view",
		"controlId": "app",
		"controlAggregation": "pages"
	  },
	  "routes": [
		{
		  "pattern": "",
		  "name": "overview",
		  "target": "overview"
		},
		{
		  "pattern": "detail/{invoicePath}",
		  "name": "detail",
		  "target": "detail"
		}
	  ],
	  "targets": {
		"overview": {
		  "id": "overview",
		  "name": "Overview"
		},
		"detail": {
		  "id": "detail",
		  "name": "Detail"
		}
	  }
	}
  }
}

2.2 webapp/view/Detail.view.xml

Detail视图XML文件新增controllerName属性controllerName="sap.ui5.walkthrough.controller.Detail"

ObjectHeader页签修改属性intro 和title,页面显示invoice模型中的ShipperName和ProductName字段信息

XML 复制代码
<mvc:View
    controllerName="sap.ui5.walkthrough.controller.Detail"
	xmlns="sap.m"
	xmlns:mvc="sap.ui.core.mvc">
	<Page
		title="{i18n>detailPageTitle}">
		<ObjectHeader
		    title="ProductName:{invoice>ProductName}"
		    intro="ShipperName:{invoice>ShipperName}"/>
			
	</Page>
</mvc:View>

2.3 webapp/controller/InvoiceList.controller.js

onPress函数中oRouter.navTo()方法新增传参invoicePath: window.encodeURIComponent(oItem.getBindingContext("invoice").getPath().substr(1)),

javascript 复制代码
sap.ui.define(
  [
    "sap/ui/core/mvc/Controller",
    "sap/ui/model/json/JSONModel",
    "../model/formatter",
    "sap/ui/model/Filter",
    "sap/ui/model/FilterOperator",
  ],
  function (Controller, JSONModel, formatter, Filter, FilterOperator) {
    "use strict";

    return Controller.extend("sap.ui5.walkthrough.controller.InvoiceList", {
      formatter: formatter,
      //初始化
      onInit: function () {
        var oViewModel = new JSONModel({
          currency: "EUR",
        });
        this.getView().setModel(oViewModel, "view");
      },
      //选择行
      onSelectionChange:function(oEvent){
			var oSelected = oEvent.getSource().getSelectedItem();
			var oList = this.byId("invoiceList");
			var aItems = oList.getItems();
			alert(aItems.indexOf(oSelected));
		},
      //发票列表搜索
      onFilterInvoices: function (oEvent) {
        // build filter array
        const aFilter = [];
        const sQuery = oEvent.getParameter("query");
        if (sQuery) {
          aFilter.push(
            new Filter("ProductName", FilterOperator.Contains, sQuery)
          );
        }

        // filter binding
        const oList = this.byId("invoiceList");
        const oBinding = oList.getBinding("items");
        oBinding.filter(aFilter);
      },
      //详情页面跳转
      onPress: function (oEvent) {
      //获取路由
			var oRouter = this.getOwnerComponent().getRouter();
      //获取事件
      var oItem = oEvent.getSource();
      //getPath().substr(1)对路径去掉第一个字符(即去除开头的斜杠)并进行URI编码
      //将编码后的路径作为参数invoicePath传递,导航到"detail"路由
			oRouter.navTo("detail",{
				invoicePath: window.encodeURIComponent(oItem.getBindingContext("invoice").getPath().substr(1))
         }

      );
		}
    });
  }
);

2.4 webapp/controller/Detail.controller.js

javascript 复制代码
sap.ui.define([
	"sap/ui/core/mvc/Controller"
], function (Controller) {
	"use strict";
	return Controller.extend("sap.ui5.walkthrough.controller.Detail", {
		onInit: function () {
            // 获取路由
			var oRouter = this.getOwnerComponent().getRouter();
            // 监听路由匹配
			oRouter.getRoute("detail").attachPatternMatched(this._onObjectMatched, this);
		},
     /* oEvent.getParameter获取列表传入参数invoicePath
        window.decodeURIComponent对路径解码
        this.getView().bindElement 绑定到视图*/
		_onObjectMatched: function (oEvent) {
			this.getView().bindElement({
				path: "/" + window.decodeURIComponent(oEvent.getParameter("arguments").invoicePath),
				model: "invoice"
			});
		}
	});
});

三、运行结果

http://localhost:8080/webapp/index.html

双击列表任意行进去页面

相关推荐
倾心琴心5 天前
【agent辅助pcb routing coding学习】实践3 kicad routing tools 从PCB文件获取了哪些信息
算法·agent·pcb·eda·routing
倾心琴心5 天前
【agent辅助pcb routing coding学习】实践4 kicad pcb 核心类层次关系
算法·agent·pcb·eda·routing
倾心琴心5 天前
【agent辅助pcb routing coding学习】实践1 kicad pcb 格式讲解
算法·agent·pcb·eda·routing
倾心琴心5 天前
【agent辅助pcb routing coding学习】实践5 kicad类按类别理解
算法·agent·pcb·eda·routing
Irene19912 个月前
Vue3 规范推荐的 <script setup> 中书写顺序(附:如何响应路由参数变化)
vue.js·路由
七夜zippoe2 个月前
API网关设计模式实战 Spring Cloud Gateway路由过滤限流深度解析
java·设计模式·gateway·路由·api网关
贾修行2 个月前
.NET MAUI 跨平台开发全栈指南:从零构建现代化多端应用
.net·路由·.net maui
Tipriest_2 个月前
排查一个多网卡的机器上不了网的问题(更改默认路由)
网络·网关·路由·多网卡
一只小阿乐2 个月前
vue 改变查询参数的值
前端·javascript·vue.js·路由·router·网文·未花中文网
noravinsc2 个月前
笔记本电脑如何配置路由,可以同时访问外网和内网
电脑·路由·内外网