Skip to Content

Pass data from detail view to Master view SAPUI5

I need to pass data from detail view to master view after selecting a checkbox as shown below.

on the detail controller I already have the checkbox function:

    estSelect: function(oEvent) {

var isSelected = oEvent.getParameter("selected");
var cxt = oEvent.getSource().getBindingContext();
var wordName = cxt.getObject("Word");

if (isSelected) {
cxt.getModel().setProperty("percent", "100%", cxt);
               this.updateSummary(wordName);
} else {
cxt.getModel().setProperty("percent", null, cxt);
}
}, 

I was trying to create a tree table with the data to the master view content:

    updateSummary: function(mName) {
var summaryLocation = sap.ui.getCore().byId("<masterContentLocation>");

var oModel = new sap.ui.model.json.JSONModel([{
title: mName,
children: [{
title: mName
}]
}]);

sap.ui.require([], function() {
var oTree = new sap.m.Tree({});
oTree.setModel(oModel);
var oStandardTreeItem = new sap.m.StandardTreeItem({
title: "{title}"
});
oTree.bindItems("/", oStandardTreeItem);
oTree.placeAt(summaryLocation);
});
wanted.png (32.6 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Jan 25 at 09:19 AM

    Here you have two options:

    • Update a value on a local JSON Model and listen to that edit in the master (overkilling maybe)
    • Use event bus to send/receive a message (I suggest this one)

    Send a message (from detail to master):

    sap.ui.getCore().getEventBus().publish(channel, event, data);

    Subscribe (master)

    sap.ui.getCore().getEventBus().subscribe(channel, event, handler, listener);

    Unsubscribe (master)

    sap.ui.getCore().getEventBus().unsubscribe(channel, event, handler, listener);
    Add comment
    10|10000 characters needed characters exceeded

  • Jan 24 at 08:55 PM

    put them in model, which is shared to all view. no need to pass between each other....

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 25 at 08:24 AM

    You can use EventBus for passing data between different controllers. For example:

    Master controller (onInit):

    var oEventBus = sap.ui.getCore().getEventBus();
    
    function handleWordSelected(oData) {
        var oModel = //get your master model here
        var aItems = oModel.getProperty('/');
        aItems.push({title: oData.word, children:[]});
        oModel.updateBindings();
    }
    
    oEventBus.subscribe('wordSelected', handleWordSelected);

    Detail controller (your checkbox select handler):

    estSelect: function(oEvent) {
        var isSelected = oEvent.getParameter("selected");
        var cxt = oEvent.getSource().getBindingContext();
        var wordName = cxt.getObject("Word");
    
        if (isSelected) {
            cxt.getModel().setProperty("percent", "100%", cxt);
                   this.updateSummary(wordName);
        } else {
            cxt.getModel().setProperty("percent", null, cxt);
        }    
    
        var oEventBus = sap.ui.getCore().getEventBus();
        oEventBus.publish('wordSelected', {word: wordName});
    }
    Add comment
    10|10000 characters needed characters exceeded