如何在ColumnListItem中单击图标时访问行和列数据

时间:2017-01-14 17:48:01

标签: json odata sap sapui5

我有一个JS视图,我在其中创建一个sap.m.Table。它"列"绑定到JSONModel。它"项目"绑定到ODataModel。当我单击ColumnListItem中包含的Icon时,我想访问行数据和列名。

查看代码:

createContent : function(oController) {
    var oTable = new sap.m.Table("table1", {
        width: "auto",
        noDataText: "Please add rows to be displayed!"
    }).addStyleClass("sapUiResponsiveMargin");

    oTable.bindAggregation("columns", "Periods>/periods", function(sId, oContext) {
        var sColumnId = oContext.getObject().period;
            return new sap.m.Column({
                hAlign: "Center",
                vAlign: "Middle",
                header: new sap.m.Text({
                    text: sColumnId
                })
            });
       });

    oTable.bindItems("zStatus>/StatusSet", function(sId, oContext) { 

    var row = new sap.m.ColumnListItem(sId, {
        type : "Inactive",
        cells: [
            new sap.ui.core.Icon({
                src: "sap-icon://delete",
                hoverColor: "red",
                activeColor: "red",
                press: [oController.onDeleteIconPress, oController]
            }),
            new sap.m.Text({
                text: "{zStatus>Description}"
            }),                                      
            new sap.ui.core.Icon(sId, {
                src: {
                    path: "zStatus>Status1",
                    formatter: function(status) {
                    switch(status) {
                        case "R":
                            return "sap-icon://sys-cancel";
                        case "G":
                            return "sap-icon://sys-enter";
                        case "Y":
                            return "sap-icon://notification";
                        default:
                            return "sap-icon://sys-help";
                    }
                }
            },
            size: "1.5em",
            press: [oController.onStatusIconPress, oController]
       }) ]
    });


   return oTable;
}

在我的控制器中,我创建了一个数组,然后是一个JSON模型" Periods"从它并将其设置为此视图。 Odata模型" zStatus"在清单文件中定义。

控制器代码:

onInit : function() {
    // array aPeriods is populated first then
    var oPeriodsModel = new sap.ui.model.json.JSONModel();
    oPeriodsModel.setData({
        periods : aPeriods
    });
    this.getView().setModel(oPeriodsModel, "Periods");
},

onStatusIconPress : function(oEvent) {
    // I can get the row data on icon press
    // Problem 2: but how do I get the column name?
    // I wanted to attach the column name to icon as customData but I could       
    // not access model attached to columns inside bindItems method
}

1 个答案:

答案 0 :(得分:1)

我设法自己解决了。

在createContent中创建一个数组。在bindAggregation列中填充列ID,然后在bindItems方法中使用此数组。 然后我可以将customData传递给图标。

这是代码 -

np.random.seed(123)
#1M df
N = 1000000
L2 = ['S','E','U',np.nan]
df = pd.DataFrame({'Group':np.random.randint(100000, size=N), 
                   'Morph': np.random.choice(L2, N)})

#print (df)

In [46]: %timeit (df[~df['Group'].isin(df.loc[df['Morph'].isin(['U',np.nan]), 'Group'].unique())])
1 loop, best of 3: 372 ms per loop

In [47]: %timeit (df.groupby('Group').filter(lambda x: (~x.Morph.isin(['U',np.nan]).any() )))
1 loop, best of 3: 34.7 s per loop