简体   繁体   English

如何获取sapui5中的行索引?

[英]How do you get the index of a row in sapui5?

I have a table that gets rows added to it dynamically. 我有一个表,可以动态添加行。 Right now I have 140 rows. 现在我有140行。 The visibleRowCount is set to 20 like so: 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");

When I click on a row I want to find out the index. 当我单击一行时,我想找出索引。 This is how I do it: 这是我的方法:

$("#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);
});

Which gets the correct index for the first 20 rows but once you start scrolling the table the index of the top row becomes 1 since the sapui5 table loads the information into the table on scroll. 它会为前20行获取正确的索引,但是一旦您开始滚动表,由于sapui5表将信息加载到滚动表中,因此第一行的索引将变为1。 I think I am going to have to go about this a different way. 我认为我将不得不以不同的方式去做。 Any ideas? 有任何想法吗?

I will set up a jsbin tomorrow if needed. 如果需要,我明天将设置一个jsbin。

When a row of a table is selected/deselected, a rowSelectionChange event is fired. 当选择/取消选择表的一行时,将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");

Above in the code, we can get the selected or deselected row; 在代码上方,我们可以获取选中或取消选中的行; then we use the isIndexSelected function to check if it is selected or deselected. 然后我们使用isIndexSelected函数来检查它是否被选中。 And by getting the context and path, we are able to get the binding object itself. 通过获取上下文和路径,我们可以获取绑定对象本身。

Please note that if row 1 is already selected and now user select row 2, this event will not fire for that deselection of row 1, an event will be fired for selection of row 2. 请注意,如果已经选择了第1行,而现在用户选择了第2行,则该事件不会因取消选择第1行而触发,将触发选择第2行的事件。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM