SlickGrid:如何在分页网格中保留选定的行?
我将 SlickGrid 的分页版本与 CheckboxSelection-Plugin 结合使用。不幸的是,当用户离开当前网格页面时,行选择会丢失,并且在再次访问该页面时不会恢复。此行为也可以通过 SlickGrid 示例 #4 重现。 (单击网格页脚中的小灯泡图标以打开分页)
我已将此情况告知 SlickGrid 的作者,但他并不认为此行为是一个错误,因为 SlickGrid 更像是一个网格框架,而不仅仅是 -添加数据解决方案。
有谁知道如何在 SlickGrid 中实现可靠的选择持久性,该方法适用于分页和非分页网格?
I'm using a paginated version of SlickGrid in conjunction with the CheckboxSelection-Plugin. Unfortunately, the row selection gets lost when one leaves the current grid page and isn't restored when that page is accessed again. This behaviour can also be reproduced with SlickGrid example #4. (click the little light-bulb-icon in the grid footer to turn on pagination)
I've informed SlickGrid's author about this, but he doesn't see this behaviour as a bug, as SlickGrid is more a grid framework than a just-add-data-solution.
Does anyone have an idea on how to implement reliable selection persistence in SlickGrid, that works for both paginated and non-paginated grids?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最后,我通过调整示例 4 中的 grid.onSelectedRowsChanged 回调取得了一些成功。主要区别在于,每次选择更改时我不会重置全局
selectedRowIds
数组,因为grid.getSelectedRows ()
只提供当前页面上选定的行索引。相反,我首先将所有新选择的行添加到selectedRowIds
数组中,然后对其进行迭代以检查其当前页面条目之一是否已被取消选择。Finally I had some success by adapting the grid.onSelectedRowsChanged callback in Example 4. The key difference is that I don't reset the global
selectedRowIds
array each time the selection changes, sincegrid.getSelectedRows()
only gives me the selected row indices on the current page. Instead I first add all newly selected rows to theselectedRowIds
array and then iterate over it to check if one of its current-page-entries has been deselected.我知道这是一个老问题,但我必须实现这个问题,并且建议的解决方案并未涵盖所有用例(例如,如果用户转到最后一页而不进行任何选择,@fbuchinger 提出的解决方案会删除一些选择,并且返回第一页)。
为了解决这个问题,我必须做一个更复杂的实现,我什至必须添加一个新的 DataView 事件
onBeforePagingInfoChanged
并修改 SlickGrid 事件onSelectedRowsChanged
以返回上一行选择(在当前选择之上)。这些更改是在 6pac/SlickGrid 分支中进行的,并在版本2.4.18 下发布
我在使用分页时使用的步骤如下,我们不能只使用
getSelectedRows()
因为这只会返回当前页面中的选择,这是不够的,我们需要保持具有数据对象 ID 的全局数组(不是行索引,因为它会在页面中不断变化),为了同时保留网格索引和数据 ID,我们将在dataView.mapIdsToRows()
之间交替和dataView.mapRowsToIds()。下面显示的代码步骤如下:
因此,在此代码中,变量
this._selectedRowDataContextIds
具有完整的选定数据对象 ID 集。这是一种复杂的方法,但这就是我迄今为止发现的使用网格分页的方法。请注意,代码中仅使用 1 个
lodash
函数isequal
来比较 2 个数组,其他所有内容都是用 ES6 和 TypeScript 编写的(尽管我删除了更多类型)可读)I know this is an old question but I had to implement this and the proposed solution didn't cover all use case (for example, the proposed solution by @fbuchinger removes some selection if user goes to last page, without doing any selections, and comes back to first page).
To deal with this issue, I had to do a much more complex implementation and I even had to add a new DataView event
onBeforePagingInfoChanged
and also modified the SlickGrid eventonSelectedRowsChanged
to also return previous row selections (on top of current selections). These changes were made in the 6pac/SlickGrid fork and were released under version2.4.18
The steps I used are the following when using Pagination, we can't just use the
getSelectedRows()
since this will only return the selection in current page which is not enough, we need to keep a global array with data object IDs (not the row indexes since that will keep changing in a page), to keep both the grid indexes and the data IDs, we'll alternate betweendataView.mapIdsToRows()
anddataView.mapRowsToIds()
.The steps of the code shown below are the following
So in this code, the variable
this._selectedRowDataContextIds
has the full set of selected data object IDs. It's a complex approach but that is what I found so far to be working with a grid Pagination.Note that there's only 1
lodash
functionisequal
that is used in the code to compare 2 arrays, everything else is written in ES6 and TypeScript (though I removed the types to be more readable)建议:
在网格定义之前添加行数组。
在分页事件中,如果行尚未在数组中,则在所有获取和存储所选行之前:
在分页事件中设置所选行:
防止取消选择。如果取消选择某行,则获取数组中的行索引,并从数组中删除该行:
Proposal:
Add an array of rows before grid definition.
In the pagging event, before all obtain and store selected rows if the row aren't in array yet:
In paggin event set selected rows:
Prevent deselect. If a row is deselect, get the row index in array, and remove the row from array:
效果很好。刚刚修改了上面的。
it works fine. Just modified the above one.