ng-grid的"beforeSelectionChange"等效项是什么?在ui-grid中? [英] Whats the equivalent for ng-grid's "beforeSelectionChange" in ui-grid?

查看:141
本文介绍了ng-grid的"beforeSelectionChange"等效项是什么?在ui-grid中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ng-grid中,我曾经通过以下方式使用beforeSelectionChange:

In ng-grid, I used to use beforeSelectionChange in the following way:

当用户选择一行时,将执行ajax调用.在进行ajax调用时,我设置了$scope.doingAjaxCall = true,并且为了防止用户更改选择,我在网格定义中添加了此代码:

When the user selects a row, an ajax call is performed. While the ajax call is happenning I set $scope.doingAjaxCall = true, and to prevent the user from changing the selection, I had this in the grid definition:

beforeSelectionChange: function () {
    return !($scope.doingAjaxCall);
},

如果正在进行ajax调用,它将锁定/冻结选择.

which locks/freezes the selection if the ajax call is happenning.

现在,在ui-grid(又名ng-grid 3)中,我不知道afterSelectionChange的等效含义.

Now, in ui-grid (aka ng-grid 3), I don't know whats the equivalent for afterSelectionChange.

在文档的此部分中:
http://ui-grid.info/docs/# /api/ui.grid.selection.api:PublicApi
我看到两个事件:

In this section of the documentation:
http://ui-grid.info/docs/#/api/ui.grid.selection.api:PublicApi
I see two events:

  • rowSelectionChanges
  • rowSelectionChangedBatch.
  • rowSelectionChanges
  • rowSelectionChangedBatch.

这些似乎等同于旧版afterSelectionChange

在文档的此部分中:
http://ui-grid.info/docs/# /api/ui.grid.selection.service:uiGridSelectionService
我看到这两种方法似乎与需求有关:

And in this section of the documentation:
http://ui-grid.info/docs/#/api/ui.grid.selection.service:uiGridSelectionService
I see these two methods that seem to be related to the need:

  • raiseSelectionEvent(grid, changedRows, event)
  • decideRaiseSelectionEvent(grid, row, changedRows, event)
  • raiseSelectionEvent(grid, changedRows, event)
  • decideRaiseSelectionEvent(grid, row, changedRows, event)

但是我不知道如何使用它们

But I don't understand how to use them

重要说明:
我正在使用multiSelect: false(即:只能选择一行)

Important note:
I'm using multiSelect: false (ie: only one row can be selected)

推荐答案

ui-grid提供了执行此操作的工具,但我不确定是否有"ui-grid"方法,但是肯定有这样做的方法兼容.

ui-grid provides the tools to do this, I'm not sure there's a "ui-grid" way, but there are definitely ways to do it that are compatible.

根据您的描述,我假设您只允许单个选择,因为如果您允许多个选择,那么他们可以继续添加选择,并且您将在后台运行ajax.因此,我假设您为 http://ui-grid.info/设置multiSelect: false docs/#/tutorial/210_selection

Based on your description I'm assuming you are only allowing single selection, because if you allowed multiple selection they could keep adding selections and you'd run the ajax in the background. So I assume you're setting multiSelect: false per http://ui-grid.info/docs/#/tutorial/210_selection

我可以看到两种获取结果的方法:

I can see two ways to get the result:

  1. 如果用户在运行ajax调用时尝试选择行,则将选择设置回原来的位置.这意味着您必须聆听selectionChanged和selectionChangedBatch事件,跟踪所需的选择,并跟踪进行ajax调用的时间.

  1. If the user tries to select a row whilst an ajax call is running, set the selection back to where it was. This means you'd have to listen to the selectionChanged and selectionChangedBatch events, keep track of the selection that you want, and keep track of when an ajax call was in progress.

使用isRowSelectable函数,如果ajax调用正在运行,则不允许选择任何行.该函数本身与您使用ng-grid即return !$scope.doingAjaxCall类似.区别在于,只要更改$scope.doingAjaxCall的值,就需要调用notifyDataChange.从内存中,使用户无法选择的行仍将允许在代码中选择该行-因此,不会更改选择该行.

Use an isRowSelectable function that doesn't let any row be selected if an ajax call is running. The function itself is similar to what you used to have with ng-grid i.e. return !$scope.doingAjaxCall. The difference is you need to call notifyDataChange whenever you change the value of $scope.doingAjaxCall. From memory making a row not selectable by the user will still allow it to be selected in code - so this won't change that the row is selected.

我可能会选择第二种选择.

I'd probably go with the second option.

根据要求添加信息:isRowSelectable在上述教程中(有一个工作示例).唯一的窍门是,我觉得它只是针对特定事件进行了重新评估,因此,当您更改doingAjaxCall变量时,它可能不仅会自动从每个项目中删除选择.因此,我建议致电notifyDataChange,这也在本教程中.

Adding information as requested: isRowSelectable is in the tutorial as noted above (there's an example of it working). The only trick is that I have a feeling it's only re-evaluated on specific events, so it may not just automatically remove the select from each item when you change the doingAjaxCall variable. Hence I recommended calling notifyDataChange, which is also in the tutorial.

我建议您用一个塞子去尝试一下,然后,如果您有困难,我可以寻找并提供建议.我现在没有时间从头开始编写它.

I'd suggest you have a go at it in a plunker, and then if you have difficulty I can look and provide advice. I don't have the time right now to write it from scratch.

这篇关于ng-grid的"beforeSelectionChange"等效项是什么?在ui-grid中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆