当选择多于一行时,SlickGrid onSelectedRowsChange 会触发两次吗?

发布于 2024-10-13 03:40:46 字数 1061 浏览 1 评论 0原文

(请参阅这个问题 和这个问题作为背景...)

给出:

grid = new Slick.Grid("#myGrid", data, columns, options);
grid.setSelectionModel(new Slick.RowSelectionModel());
grid.onSelectedRowsChanged.subscribe(function() { 
   row_ids = grid.getSelectedRows();
   console.log(row_ids);
});

...当我选择一行(比如说,第 5 行),我得到的输出是

[4]

...这是我所期望的。但是,除了该行之外,CMD+Click 或 SHIFT+Click -ing 另一行(例如,第 3 行)会给出

[2]
[4, 2]

...的输出,这不是我所期望的(我期望的只是 [4, 2])。只要所选行数 > ,就会发生这种情况。 1..因此,如果我要继续选择另一行(例如第 17 行),我会

[16]
[4, 2, 16]

console.log 语句上添加一个断点并验证 onSelectedRowsChanged 被触发两次:一次针对新单击的行,一次针对所有选定的行。

这是为什么呢?我只希望它触发一次,为我提供所选行的完整数组。我将如何实现这个目标?或者我错过了什么?

(See this question and this question for background...)

Given:

grid = new Slick.Grid("#myGrid", data, columns, options);
grid.setSelectionModel(new Slick.RowSelectionModel());
grid.onSelectedRowsChanged.subscribe(function() { 
   row_ids = grid.getSelectedRows();
   console.log(row_ids);
});

... when I select one row (say, row 5), I get an output of

[4]

... which is what I would expect. However, CMD+Click or SHIFT+Click -ing another row (say, row 3) in addition to this row gives me an output of

[2]
[4, 2]

... which is NOT what I would expect (I would expect just [4, 2]). This seems to happen so long as the number of rows selected is > 1. So, if I were to continue to select another row (say, row 17), I would get this

[16]
[4, 2, 16]

I added a breakpoint on the console.log statement and verified that the onSelectedRowsChanged is being fired twice: once for the newly clicked row, and once for all the selected rows.

Why is this? I only want it fired once, giving me the complete array of the selected rows. How would I accomplish this? Or am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2024-10-20 03:40:46

问题(我后来发现)在于行选择模型Slick.RowSelectionModel()

具体来说,当选择该行时,将调用 handleActiveCellChange()handleClick(),每个调用 setSelectedRanges(),而前者仅将其设置为当前单击的行,后者将其设置为所有选定的行。

我通过在 init() (在 slick.rowSelectionModel.js 内部)_grid.onActiveCellChanged 处理程序中注销并将调用移至 内部来修复此问题handleClick()

function init(grid) {
   _options = $.extend(true, {}, _defaults, options);
   _grid = grid;
   // _grid.onActiveCellChanged.subscribe(handleActiveCellChange);
   _grid.onKeyDown.subscribe(handleKeyDown);
   _grid.onClick.subscribe(handleClick);
}

...

function handleClick(e, data) {

   ...

   if (!e.ctrlKey && !e.shiftKey && !e.metaKey) {
      handleActiveCellChange(e, data);
      return false;
   }

   ...

}

我不知道作者是否已在“v2 master”中修复了这个问题,正如 @fbuchinger 所说,而且我知道这个修复是快速且肮脏的(这会破坏键盘导航和行之间的选择) ),但它有效并给了我问题中描述的预期行为。由于我更关心点击是否正常工作而不是键盘导航,所以我现在坚持这一点。

有人知道更好的方法吗?

The problem (I've since discovered) lies in the row selection model Slick.RowSelectionModel().

Specifically, when the row was selected, handleActiveCellChange() AND handleClick() were being called, each which calls setSelectedRanges(), while the former only sets it to the currently clicked row, and the latter sets it to all the selected rows.

I fixed this by unregistering in init() (inside slick.rowSelectionModel.js) _grid.onActiveCellChanged handler and moved the call inside handleClick():

function init(grid) {
   _options = $.extend(true, {}, _defaults, options);
   _grid = grid;
   // _grid.onActiveCellChanged.subscribe(handleActiveCellChange);
   _grid.onKeyDown.subscribe(handleKeyDown);
   _grid.onClick.subscribe(handleClick);
}

...

function handleClick(e, data) {

   ...

   if (!e.ctrlKey && !e.shiftKey && !e.metaKey) {
      handleActiveCellChange(e, data);
      return false;
   }

   ...

}

I don't know if this has been fixed by the author in "v2 master", as @fbuchinger said, and I know this fix is quick and dirty (this breaks keyboard navigation and selection between rows), but it works and gives me the expected behavior described in my question. Since I care more about the clicks working properly than the keyboard navigation, I'm sticking with this for now.

Anyone know of a better way?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文