ui5详细教程Stone

SAPUI5(12) - 对象显示组件

2017-01-09  本文已影响256人  Stone0823

上一篇介绍了使用sap.m.Panelsap.m.Text控件来显示一个供应商的信息。但SAPUI5提供了几个专门用于显示数据的组件,可以让编码更加简单,显示更加漂亮:

ObjectIdentifier

定义:The ObjectIdentifier is a display control that enables the user to easily identify a specific object. The ObjectIdentifier title is the key identifier of the object and additional text and icons can be used to further distinguish it from other objects.

对象主要通过title属性进行标识,使用texticon进行补充。

举个例子:

/**
 * ObjectIdentifier
 * Stone Wang Demo
 */

function onTitlePress(oEvent){
    jQuery.sap.require("sap.m.MessageBox");
    var sName = oEvent.getSource().getTitle();
    sap.m.MessageBox.information(sName);
}

var oAppData = {            
    "SupplierId": "1",
    "SupplierName": "ABC新能源汽车有限公司",
    "Profile": "生产和销售新能源汽车及备件"
};

var oModel = sap.ui.model.json.JSONModel();
oModel.setData(oAppData);

sap.ui.getCore().setModel(oModel);

var oObjIdentifier = new sap.m.ObjectIdentifier({
    title: "{/SupplierName}",
    text: "{/Profile}",
    titleActive: true,
    titlePress: onTitlePress
});

oObjIdentifier.placeAt("content");

ObjectNumber

定义:The ObjectNumber control displays number and number unit properties for an object. The number can be displayed using semantic colors to provide additional meaning about the object to the user.
ObjectNumber

用于显示数字,并且能根据不同的状态提供颜色区分。例子:

/**
 * ObjectNumber
 * Stone Wang Demo
 */

var oAppData = {            
    "ProductId": "1",
    "ProductName": "Sony DPT-S1",
    "Price": "5890",
    "Currency": "CNY"
};
            
var oModel = sap.ui.model.json.JSONModel();
oModel.setData(oAppData);

sap.ui.getCore().setModel(oModel);

var oProduct = new sap.m.ObjectIdentifier({
    title: "{/ProductName}"
});

var oNumber1 = new sap.m.ObjectNumber({
    number: "{/Price}",
    numberUnit: "{/Currency}"
});
var oNumber2 = new sap.m.ObjectNumber({
    number: "{/Price}",
    numberUnit: "{/Currency}",
    state: "Error"
});         
var oNumber3 = new sap.m.ObjectNumber({
    number: "{/Price}",
    numberUnit: "{/Currency}",
    state: "Warning"
});
var oNumber4 = new sap.m.ObjectNumber({
    number: "{/Price}",
    numberUnit: "{/Currency}",
    state: "Success"
});

new sap.ui.layout.VerticalLayout({
    content: [oProduct, oNumber1, oNumber2, oNumber3, oNumber4]
}).placeAt("content");

故意使用4个不同的状态,显示索尼DPT-S1的价格。

ObjectAttribute

定义: The ObjectAttribute control displays a text field that can be normal or active. The ObjectAttribute fires a press event when the user selects active text.

示例:

/**
 * ObjectAttribute
 * Stone Wang Demo
 */

function onAttributePressed(oEvent){
    jQuery.sap.require("sap.m.MessageBox");
    var sMessage = oEvent.getSource().getTitle() 
        + ":" + oModel.getProperty("/Price");
    sap.m.MessageBox.information(sMessage);
}

var oAppData = {            
    "ProductId": "1",
    "ProductName": "Sony PST-1",
    "Price": "5890",
    "Currency": "CNY"
};

var oModel = sap.ui.model.json.JSONModel();
oModel.setData(oAppData);

sap.ui.getCore().setModel(oModel);              

var oObjAttribute = new sap.m.ObjectAttribute({
    title: "{/ProductName}",
    text: "{ProductId}",
    active: true,
    press: onAttributePressed
});

oObjAttribute.placeAt("content");

当点击title的时候,对话框显示名称 + 价格:

使用oEvent.getSource().getTitle()获得ObjectAttribute的title属性,使用oModel.getProperty("/Price")获得model的price。

ObjectMarker

ObjectMarker以图标的方式显示预定义的几种标记。可以绑定press事件。

/**
 * Object Marker
 * Demo written by Stone Wang
 */

function onMarkerPressed(oEvent){
    sap.m.MessageToast.show(oEvent.getParameter("type") + " was pressed.");
}

var oPanel = new sap.m.Panel({
    headerText: "图例",
    content: [
        new sap.m.ObjectMarker({
            type: "Flagged"
        }),     
        
        new sap.m.ObjectMarker().setType(sap.m.ObjectMarkerType.Favorite),
        
        new sap.m.ObjectMarker({
            type: "Locked"
        }),
        
        new sap.m.ObjectMarker({
            type: "Draft",
            press: function(oEvent){
                sap.m.MessageToast.show(oEvent.getParameter("type"));
            }
        })
    ]
}).placeAt("content");
    

页面显示效果:

ObjectStatus

以文本的方式显示对象,并根据对象的状态用不同的颜色进行区分。

/**
 * Object Status
 * Demo written by Stone Wang
 */

new sap.ui.layout.VerticalLayout({
    content: [
        new sap.m.ObjectStatus({
            text: "Normal message",
            state: "None"
        }),
        
        new sap.m.ObjectStatus({
            text: "Normal message with icon",
            state: "None",
            icon: "sap-icon://accept"
        }),
        
        new sap.m.ObjectStatus({
            text: "Error message",
            state: "Error"
        }),
        
        new sap.m.ObjectStatus({
            text: "Error message with icon",
            state: "Error",
            icon: "sap-icon://alert"
        }),
        
        new sap.m.ObjectStatus({    
            text: "Purchase Order was created successfully.",
            state: "Success",
            icon: "sap-icon://save"
        }),
    ]
}).placeAt("content");  

界面效果:

ObjectHeader

ObjectHeader可以显示对象很多信息,包括关键的title和附件的text, icon等,还可以包含其它控件,从而表达比较丰富的信息。比如,接下来我们通过ObjectHeader来显示一个产品的相关信息:

/**
 * Object Header
 * Demo written by Stone Wang
 */

// 定义Application data
var oAppData = {            
    "Product": {
        "Name": "Power Projector 4713",
        "Price": "856.49",
        "Currency": "CNY",
        "Width": "51",
        "Depth": "42",
        "Height": "18",
        "DimUnit": "cm",
        "Description": "A very powerful projector with special features for Internet usability, USB"
    }
};

// 绑定Model与Data
var oModel = sap.ui.model.json.JSONModel();
oModel.setData(oAppData);

// 设置Model到Core对象,从而使model在Application中可见
sap.ui.getCore().setModel(oModel);

// 使用ObjectHeader显示产品信息
new sap.m.ObjectHeader({
    title: "{/Product/Name}",
    number: "{/Product/Price}",
    numberUnit: "{/Product/Currency}",
    
    statuses: [
        new sap.m.ObjectStatus({text:"Some damaged", state: "Error"}),
        new sap.m.ObjectStatus({text:"In stock", state: "Success"})
    ],
    
    attributes: [
        new sap.m.ObjectAttribute({
            text: "{/Product/Width} x {/Product/Depth} x {/Product/Height} {/Product/DimUnit}"
        }),
        new sap.m.ObjectAttribute({
            text: "{/Product/Description}"
        }),
        new sap.m.ObjectAttribute({
            text: "www.jd.com",
            active: true,
            press: function(oEvent){
                sap.m.URLHelper.redirect("http://www.jd.com", true);
            }
        })
    ]
}).placeAt("content");

页面显示效果:

比较美观的显示了一个产品的相关信息,点击www.jd.com可以实现跳转。

上一篇 下一篇

猜你喜欢

热点阅读