你怎么得到sapui5中的一行索引?

时间:2015-07-14 21:18:21

标签: javascript jquery sapui5

我有一个表,可以动态地添加行。现在我有140行。 visibleRowCount设置为20,如下所示:

var oTable = new sap.ui.table.Table({
    id: "sapTable",
    title: "Table Example",
    visibleRowCount: 20,
    selectionMode: sap.ui.table.SelectionMode.Single
}).addStyleClass("alternate-color");

当我点击一行时,我想找出索引。我就是这样做的:

$("#myTable").on("tap", "tr", function (e) {
    // Works until you scroll the table - Top element becomes index of 1
    var index = this.rowIndex;
    console.log(index);
});

哪个获取前20行的正确索引但是一旦开始滚动表,则顶行的索引变为1,因为sapui5表在滚动时将信息加载到表中。我想我将不得不采取不同的方式。有什么想法吗?

如果需要,我明天会设置一个jsbin。

1 个答案:

答案 0 :(得分:2)

当选择/取消选择某一行时,会触发rowSelectionChange个事件。

  var oTable = new sap.ui.table.Table({
    id: "sapTable",
    title: "Table Example",
    visibleRowCount: 20,
    selectionMode : sap.ui.table.SelectionMode.Single,
    rowSelectionChange: function(e) {
      var oIndex = e.getParameter('rowIndex');
      if (oTable.isIndexSelected(oIndex )) {
        var oContext= oTable.getContextByIndex(oIndex );
        var path = oContext.sPath;
        var object = oTable.getModel().getProperty(path);
        console.log(object);        
      }
    }
  }).addStyleClass("alternate-color");

在代码的上方,我们可以得到选中或取消选择的行;然后我们使用isIndexSelected函数来检查它是被选中还是取消选中。通过获取上下文和路径,我们可以获得绑定对象本身。

请注意,如果已选择第1行,现在用户选择第2行,则不会触发第1行取消选择此事件,将触发选择第2行的事件。

希望这有帮助!

相关问题