【问题标题】:How to get the data from a row that was double-clicked in an ExtJS grid?如何从 ExtJS 网格中双击的行中获取数据?
【发布时间】:2011-05-22 07:46:39
【问题描述】:

在 ExtJS 网格中,我可以像这样获取所选数据项的索引:

grid.getSelectionModel().on('rowselect', function(sm, index, rec){
    changeMenuItemInfoArea(menuItemApplication, 'you are on row with index ' + index);
    var row_number_parts = rec.id.split('-'); // rec.id = e.g. "ext-record-1"
    var selected_index = row_number_parts[2] - 1;
    alert(selected_index);
});

但是如何在双击时获取所选数据项的索引?

当我这样做时:

listeners: {
    'rowdblclick': function(grid, rowindex, e){
        console.log(...);
    }
}

gride 似乎都没有我需要的信息,rowindex 也没有用,因为如果用户使用一列,那么双击的行的索引不一定是加载网格的数据集的索引。

附录

感谢@McStretch,我最终解决了手头的问题,将id 放在项目列表中,隐藏id 列,然后将id 发送到编辑页面,如下所示:

listeners: {
    'rowdblclick': function(grid, index, rec){
        var id = grid.getSelectionModel().getSelected().json[0];
        go_to_page('edit_item', 'id=' + id);
    }
}

【问题讨论】:

    标签: extjs grid


    【解决方案1】:

    根据cellClick的文档,索引实际上是指存储中记录的索引:

    function(grid, rowIndex, columnIndex, e) {
        // Get the Record, this is the point at which rowIndex references a 
        // record's index in the grid's store.
        var record = grid.getStore().getAt(rowIndex);  
    
        // Get field name
        var fieldName = grid.getColumnModel().getDataIndex(columnIndex); 
        var data = record.get(fieldName);
    }
    

    如果是这种情况,那么您不必担心网格重新排序。我认为您应该在'rowdblclick' 侦听器中使用上述方法来获取索引/记录——它更具可读性。测试一下,看看会发生什么。

    【讨论】:

      【解决方案2】:

      我找到了一种方法:

      listeners: {
          'rowdblclick': function(grid, index, rec){
              var row_label = grid.getSelectionModel().getSelected().id;
              var row_label_parts = row_label.split('-');
              var selected_index = row_label_parts[2] - 1;
              alert(selected_index);
          }
      }
      

      【讨论】:

      • 不要这样做。即使在排序之后,索引也是正确的。排序是在存储级别处理的,因此索引将在存储和网格之间匹配。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 2012-01-21
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 2021-09-27
      相关资源
      最近更新 更多