如何从其他页面控制器将 ColumnListItem 添加到 MVC 中页面内的表 [英] How to add ColumnListItem to a table inside a page in MVC from other page controller

查看:15
本文介绍了如何从其他页面控制器将 ColumnListItem 添加到 MVC 中页面内的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 MVC 编写的 SAPUI5 应用程序

I have a SAPUI5 application written in MVC

我有一个名为 oPage4 的视图:

I have a view called oPage4:

var landscapePage = new sap.m.Page({
        title : "Landscape Name",
        showNavButton : true,
        navButtonPress : [oController.back,oController],
        footer : new sap.m.Bar({
            id : 'landscapePage_footer',
            contentMiddle : [ 
             new sap.m.Button({

            }),
             new sap.m.Button({

            })
            ]
        }),
});

oLandscapePageTable = new sap.m.Table("landscape", {
        inset : true,
        visible : true,
        getIncludeItemInSelection : true,
        showNoData : false,
        columns : [ new sap.m.Column({
            styleClass : "name",
            hAlign : "Left",
            header : new sap.m.Label({
            })
        }) ]
});

landscapePage.addContent(oLandscapePageTable);
return landscapePage;

然后在 page1 控制器中,我想在第 4 页的表格中添加一个 columnlistitem.

then inside page1 controller I want to add a columnlistitem to the table of page 4.

var oPage4 = sap.ui.getCore().byId("p4");
var landscapePageRow = new sap.m.ColumnListItem({
    type : "Active",
    visible : true,
    selected : true,
    cells : [ new sap.m.Label({
        text : something
    }) ]
});
oPage4.getContent().addItem(landscapePageRow);

它不起作用.请告诉我怎么做?

it doesn't work. please show me how to do so?

推荐答案

好的,我想我现在明白你的问题了.一般来说,我会避免从另一个视图调用页面并对其进行操作.但是,绝对有可能:

Ok, I think I understood your problem now. In general I would avoid calling the page and doing manipulations on it from another view. However, it is absolutely possible:

您可以使用更多可以从外部调用的函数来扩展您的 page4,如下所示:

You can extend your page4 with some more functions that can be called from outside like this:

sap.ui.jsview("my.page4", {

    createContent : function() {
        this.table = ...    
        ...
    },

    addColumnListItem : function(columnListItem) {
        // add it to the table calling this.table ...
    }

}

从另一个角度来看,您现在可以像这样调用这个函数:

From another view you´re now able to call this function like this:

var page4 = sap.ui.jsview("my.page4");
page4.addColumnListItem(page4, columnListItem);

注意: page4 对象本身并不指向您要返回的控件,而是指向视图实例本身.如果您将 page4 对象记录到控制台,您会注意到这一点.这就是您必须添加上述功能的原因.

Attention: The page4 object itself doesn´t point to the control you´re returning but to the the view instance itself. You will notice this, if you log the page4 object to the console. This is why you have to add functions like described.

其他一些方法是使用 EventBus,如此处 发布和订阅事件.既然你已经要求了,让我告诉你如何做到这一点:

Some other approaches would be to use the EventBus like described here to publish and subscribe to events. Since you´ve asked for it let me show you how you could do it:

主要目的是,一个人可以订阅一个特定的事件,其他人可以发布这样的事件到事件总线.我给你举个例子:

The main intention is, that one can subscribe to a particular event and others can publish such events to the eventbus. Let me give you an example:

订阅 EventBus:

var eventBus = sap.ui.getCore().getEventBus();
eventBus.subscribe("channel1", "event1", this.handleEvent1, this);

当然,您可以随意命名您的频道和活动.第三个参数表示在发布事件时将调用的函数.最后一个参数是this"在给定函数中将指向的范围.

Of course you can name your channel and events as you wish. The third parameter indicates the function, that will be called in case of published events. The last paramter is the scope 'this' will point to in the given function.

您的 handleEvent1 函数可能如下所示:

Your handleEvent1 function could look like this:

handleEvent1 : function(channel, event, data) {
    var listItem = data.listItem
}

将事件发布到 EventBus:

var columnListItem = ...
var eventBus = sap.ui.getCore().getEventBus();

eventBus.publish("channel1", "event1", 
    {
        listItem : columnListItem
    }
);

您还有一个选择是根据模型制作 columnListItems.就像每次都取决于您的实际架构和数据.

One more option you have is to make the columnListItems depending on a model. Like everytime it depends on your actual architecture and data.

告诉我这是否解决了您的问题,或者您是否需要更多信息.

Let me know if this solved your problem or if you need some more information.

这篇关于如何从其他页面控制器将 ColumnListItem 添加到 MVC 中页面内的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆