简体   繁体   English

替换数据时AngularJS ui-grid保持选择状态

[英]AngularJS ui-grid keep selection state when replacing data

I have a ui-grid bound to an object array called data like this: 我有一个绑定到名为data的对象数组的ui网格,如下所示:

$scope.grd = {
        enableRowSelection: true,
        multiSelect: true,
        enableRowHeaderSelection: false,
        columnDefs: [
                { field: 'id', name: 'ID' },
                { field: 'name', name: 'Name' },
                { field: 'tags', name: 'Tags' }
        ],
        data: "data"
};

if I now replace an item in the array like this: 如果我现在这样替换数组中的项目:

$scope.data[i] = replacementData; 

the grid is updated correctly but the selection state is lost. 网格已正确更新,但选择状态已丢失。 I guess the selection module simple doesn't support this though the core module does . 我猜的selection虽然模块简单不支持该core模块

Is there a way to get the selection state of an item before replacing it? 有没有一种方法可以在替换项目之前获得其选择状态?

I found this GridRow class in the docs of the selection module that has an isSelected property but no ideas how to get it... 我在选择模块的文档中找到了这个GridRow类,该类具有isSelected属性,但是不知道如何获取它。

Here is a Plunk that demonstrates the behavior - notice that the selectionCount is also wrong after the row is replaced, so there must be some kind of selected item info somewhere. 这是一个演示行为的Plunk-请注意,替换行后selectionCount也是错误的,因此在某处必须有某种选定的项目信息。

Update : It seems that replacing an item in the bound array doesn't remove the GridRow that ui-grid uses internally (that also is the reason why the selectedCount is wrong). 更新 :似乎替换绑定数组中的项目不会删除ui-grid内部使用的GridRow (这也是selectedCount错误的原因)。 Calling unSelectRow before replacing the item fixes the count but the GridRow still exists... 在替换项目之前调用unSelectRow修复计数,但是GridRow仍然存在...

Ended up copying all the properties from the replacementData over the old data object. 结束了复制从所有属性replacementData了旧data对象。 That way the selection state is kept, and no new GridRow get's created. 这样就保持了选择状态,并且没有创建新的GridRow get。

Updated Plunk 更新的Plunk

angular.extend($scope.data[i], replacementData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM