【问题标题】:How to navigate from master page to detail page view using routing mechanism?如何使用路由机制从母版页导航到详细页视图?
【发布时间】:2026-01-13 03:55:01
【问题描述】:
sap.ui.define([
    "sap/ui/core/UIComponent",
    "sap/ui/Device",
    "RoutNav/model/models"
], function (UIComponent, Device, models) {
    "use strict";

    return UIComponent.extend("RoutNav.Component", {
        metadata: {
            manifest: "json",
            rootView: "RoutNav.view.View1",

            routes: [
                {
                    pattern: "",
                    name: "Master",
                    view: "Master",
                    targetAggregation: "masterPages",
                    targetControl: "idAppControl",
                    subroutes: [
                        {
                            pattern: "tab:",
                            name: "Detail",
                            view: "Detail"
                        }
                    ]
                }
            ]
        },

        /**
         * The component is initialized by UI5 automatically during the startup of the app and calls the init method once.
         * @public
         * @override
         */
        init: function () {
            // call the base component's init function
            UIComponent.prototype.init.apply(this, arguments);

            // set the device model
            this.setModel(models.createDeviceModel(), "device");
        }
    });
});

我已经尝试过了,在master.view.xml 页面中我刚刚给出了一个标题。它不工作。

【问题讨论】:

  • 完全不清楚你在问什么。您添加的代码不包含任何导航。您能否添加一个像 JSFiddle 或 Plunker 这样的工作示例来显示您的代码?

标签: sapui5


【解决方案1】:

你缺少配置代码

 routing : {
      config : {
        routerClass : Application_Name.MyRouter,
        viewType : "XML",
        viewPath : "Application_Name.view",
        targetAggregation : "detailPages",
        transition: "slide",
        clearTarget : false
      },
      routes : [
        {
            pattern : "",
            name : "Master",
            view : "Master",
            targetAggregation : "masterPages",
            targetControl : "idAppControl",
            subroutes : [
                {
                    pattern : "tab:",
                    name : "Detail",
                    view : "Detail"
                }
            ]
        }
    ]
 }

【讨论】:

  • 嗨hirse,实际上我的要求是我应该在母版页xml视图中创建列表或按钮,当我点击按钮时,它应该使用sapui5中的路由机制导航到详细信息页面,你能不能展示一些代码
【解决方案2】:

从主导航到细节的步骤..使用 JSON

  1. Master.view.xml

<List id="list" growing="true" mode="SingleSelectMaster"
    growingThreshold="100" growingScrollToLoad="true" select="onSelect"
    noDataText="No Data">
</List>
  1. Master.controller.js

    navBack: function(){
		var oRouter = new sap.ui.core.UIComponent.getRouterFor(this);
		oRouter.myNavBack("S1");
	},
	onInit: function(){
		this.data = new sap.ui.model.json.JSONModel("Model/data.json");
		sap.ui.getCore().setModel(this.data);
		
		var list = this.getView().byId("list");
		list.bindItems("/employees", new sap.m.StandardListItem({
			title: "{fname}"
		}));
		list.setModel(this.data);
	},
	onSelect: function(evt){
		var index = evt.getParameter("listItem").getBindingContext().getPath();
		index.charAt(index.length-1)-2;
		
		var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
		oRouter.navTo("Detail", {
			key : evt.getParameter("listItem").getTitle()			
		});
	}
  1. Detail.view.xml

<Table onSelect="onpress" id="table" class="sapUiSmallMarginTop">
   <headerToolbar>
		<Toolbar>
			<Title level="H2" text="Personal Details" />
			<ToolbarSpacer />
			<Button press="onTableSettings" icon="sap-icon://drop-down-list" />
		</Toolbar>
	</headerToolbar>
		<columns>
			<Column>
				<Text text="Last_Name" key="name"/>
			</Column>
			<Column>
				<Text text="Mobile" />
			</Column>
			<Column>
		    	<Text text="Age" key="Age" />
			</Column>
		</columns>
	</Table>
  1. Detail.controller.js

onInit : function() {
		this.getRouter().attachRoutePatternMatched(this.onRouteMatched, this);

	},

	onRouteMatched : function(oEvent) {
		// debugger;
		this.data = new sap.ui.model.json.JSONModel("Model/data.json");
		this.getView().setModel(this.data);

		var dataKey = oEvent.getParameter("arguments").key;
	
		this.getView().getModel().attachRequestCompleted(function() {
			var data2 = this.getView().getModel().getData().employees;
			var otable = this.getView().byId("table");

			for (var i = 0; i < data2.length; i++) {
				if (data2[i].fname === dataKey) {
					otable.bindItems("/employees", new sap.m.ColumnListItem({
						cells : [ new sap.m.Text({
							text : "{lname}",
						}), new sap.m.Text({
							text : "{mobile}"
						}), new sap.m.Text({
							text : "{age}"
						}) ]
					}));
				}
				otable.setModel(this.data);
			}
		}, this);

	},
	getRouter : function() {
		return sap.ui.core.UIComponent.getRouterFor(this);
	},
  1. Component.js

 routing : {
      config : {
        routerClass : Application_Name.MyRouter,
        viewType : "XML",
        viewPath : "Application_Name.view",
        targetAggregation : "detailPages",
        transition: "slide",
        clearTarget : false
      },
      routes : [
        {
          pattern : "",
          name : "main",
          view : "Master",
          targetAggregation : "masterPages",
          targetControl : "idAppControl",
	          subroutes : [
	                       {
	    	            	   pattern:"Detail/{key}",
	    			    	   name: "Detail",
	    			    	   view: "Detail",
	    			    	   targetAggregation: "detailPages",
	    	               }
          ]
        }
      ]
    }
  1. MyRouter.js

只需在 MyRouter.js 文件中找到并替换您的 Application_Name

jQuery.sap.require("sap.m.routing.RouteMatchedHandler");
jQuery.sap.require("sap.ui.core.routing.Router");
jQuery.sap.declare("Application_Name.MyRouter");

sap.ui.core.routing.Router.extend("Application_Name.MyRouter", {

	constructor : function() {
		sap.ui.core.routing.Router.apply(this, arguments);
		this._oRouteMatchedHandler = new sap.m.routing.RouteMatchedHandler(this);
	},

	myNavBack : function(sRoute, mData) {
		var oHistory = sap.ui.core.routing.History.getInstance();
		var sPreviousHash = oHistory.getPreviousHash();

		//The history contains a previous entry
		if (sPreviousHash !== undefined) {
			window.history.go(-1);
		} else {
			var bReplace = true; // otherwise we go backwards with a forward history
			this.navTo(sRoute, mData, bReplace);
		}
	},
	myNavToWithoutHash : function (oOptions) {
		var oSplitApp = this._findSplitApp(oOptions.currentView);

		// Load view, add it to the page aggregation, and navigate to it
		var oView = this.getView(oOptions.targetViewName, oOptions.targetViewType);
		oSplitApp.addPage(oView, oOptions.isMaster);
		oSplitApp.to(oView.getId(), oOptions.transition || "show", oOptions.data);
	},

	backWithoutHash : function (oCurrentView, bIsMaster) {
		var sBackMethod = bIsMaster ? "backMaster" : "backDetail";
		this._findSplitApp(oCurrentView)[sBackMethod]();
	},

	destroy : function() {
		sap.ui.core.routing.Router.prototype.destroy.apply(this, arguments);
		this._oRouteMatchedHandler.destroy();
	},

	_findSplitApp : function(oControl) {
		var sAncestorControlName = "idAppControl";

		if (oControl instanceof sap.ui.core.mvc.View && oControl.byId(sAncestorControlName)) {
			return oControl.byId(sAncestorControlName);
		}

		return oControl.getParent() ? this._findSplitApp(oControl.getParent(), sAncestorControlName) : null;
	}

});

别忘了添加 MyRouter.js 文件

【讨论】:

  • 你能告诉我们应该在 MyRouter.js 中添加什么代码吗??
  • OK..只需找到并替换您的 Application_Name
  • 嗨,请给我一个简单的例子,通过创建简单的按钮使用路由从母版页 xml 视图导航到详细页 xml 视图,作为 sap 的新手
  • 你好,我只需要母版页中的两个按钮,点击数据应该详细显示,简单的例子,我已经尝试了上面的一个,我无法得到输出
【解决方案3】:
"routing": {
        "config": {
            "routerClass": "sap.m.routing.Router",
            "viewPath": "mc7prod.view",
            "viewType": "XML",
            "controlAggregation": "pages",
            "clearTarget": "false"
     },
     "routes": [
     {
        "pattern": "",
        "name": "Index",
        "view": "Index",
        "targetControl": "appId"
     },
     {
        "pattern": "split",
        "name": "AppMasterDetail",
        "view": "AppMasterDetail",
        "targetControl": "appId",
        "subroutes": [
            {
                "pattern": "master/{entity}",
                "name": "master",
                "view": "TS_Master",
                "targetAggregation": "masterPages",
                "preservePageInSpplitContainer": true,
                "targetControl": "idAppMaDeControl",
                "subroutes": [
                    {
                        "pattern": "detail/{entity}",
                        "name": "detail",
                        "view": "TS_Detail",
                        "targetAggregation": "detailPages"
                    }
                ]}]
        },

【讨论】:

  • this.getRouter().navTo("master",{entity : "Detail"});
  • 虽然这段代码可以回答这个问题,但最好包含一些上下文,解释它是如何工作的以及何时使用它。从长远来看,纯代码的答案没有用处。
最近更新 更多