当选择多于一行时,SlickGrid onSelectedRowsChange 会触发两次吗?
给出:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题(我后来发现)在于行选择模型
Slick.RowSelectionModel()
。具体来说,当选择该行时,将调用
handleActiveCellChange()
和handleClick()
,每个调用setSelectedRanges()
,而前者仅将其设置为当前单击的行,后者将其设置为所有选定的行。我通过在
init()
(在slick.rowSelectionModel.js
内部)_grid.onActiveCellChanged
处理程序中注销并将调用移至内部来修复此问题handleClick()
:我不知道作者是否已在“v2 master”中修复了这个问题,正如 @fbuchinger 所说,而且我知道这个修复是快速且肮脏的(这会破坏键盘导航和行之间的选择) ),但它有效并给了我问题中描述的预期行为。由于我更关心点击是否正常工作而不是键盘导航,所以我现在坚持这一点。
有人知道更好的方法吗?
The problem (I've since discovered) lies in the row selection model
Slick.RowSelectionModel()
.Specifically, when the row was selected,
handleActiveCellChange()
ANDhandleClick()
were being called, each which callssetSelectedRanges()
, 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()
(insideslick.rowSelectionModel.js
)_grid.onActiveCellChanged
handler and moved the call insidehandleClick()
: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?