SAPUI5:如何获取行的索引?

时间:2015-03-17 16:26:06

标签: javascript indexing row sapui5

我遇到了问题,实际上我无法检索表格中行的索引。我尝试了很多东西,但我还没有找到解决办法。这是我的代码:

<script id='TableSuser'>
var oTableSuser = new sap.ui.table.Table({editable:false, width: "400px", visibleRowCount:2});
var oControl = new sap.ui.commons.Button({text : "{suser}",  press : function() {openDialog(oTableSuser.getSelectedIndex());}});
oTableSuser.addColumn(new sap.ui.table.Column({label: new sap.ui.commons.Label({text: "Suser"}), template: oControl, sortProperty: "Button", filterProperty: "Button", name: "password_colonne"}));
oControl = new sap.ui.commons.TextView({text:"{suser_mdp}"});
oTableSuser.addColumn(new sap.ui.table.Column({label: new sap.ui.commons.Label({text: "suser_mdp"}), template: oControl, sortProperty: "suser_mdp", filterProperty: "suser_mdp", visible: false}));

var oModel = new sap.ui.model.json.JSONModel();
oModel.loadData("suser.php");
oTableSuser.setModel(oModel);
oTableSuser.bindRows("/");

function openDialog(index) {
var currentRowContext = oTableSuser.getContextByIndex(index);
var valeur_suser_mdp = oModel.getProperty("suser_mdp",currentRowContext); 

alert(index);
alert(currentRowContext);
alert(valeur_suser_mdp);
oTableSuser.getColumns()[1].setVisible(true);
};
oTableSuser.placeAt("suser");
</script>

当我调用函数 function(){openDialog( oTableSuser.getSelectedIndex());}

时,我想检索行的索引

我尝试了不同的东西而且我没有找到如何检索行的索引,我总是有&#34; -1&#34; 如果有人有想法,我有兴趣吗? :)

2 个答案:

答案 0 :(得分:1)

您正在将变量传递给模板函数,该函数不起作用(在渲染时,未选择任何内容,因此为-1)

最好让你的代码像这样:

var oControl = new sap.ui.commons.Button({
    text  : "{suser}",  
    press : oController.openDialog
});

并在控制器中,按如下方式定义openDialog方法:

openDialog: function(oEvent) {
    var tbl   = sap.ui.getCore().byId("id_of_your_table"),
        index = tbl.getSelectedIndex();
}

答案 1 :(得分:-1)

可能有点太晚了,但这可能有助于某人为此寻找解决方案。

如果您拥有表的id或者您拥有表对象,那么您可以执行以下操作来获取所选索引。

`var table = sap.ui.getCore().byId("you table id");

var index = parseInt(table._aSelectedPaths [0] .replace(&#39; /&#39;,&#39;&#39;),10)`

OR 如果你想获得行按事件的索引,那么你可以在回调函数中执行以下操作。

onRowPress:function(evt) { var index = parseInt(evt.getSource()._aSelectedPaths[0].replace('/',''),10); }