简体   繁体   English

如何在ng-grid中显示和过滤Breeze实体?

[英]How to display and filter Breeze entities in ng-grid?

I have an ng-grid with the following configuration: 我有一个具有以下配置的ng-grid:

vm.gridOptions = {
    data: 'vm.users',
    showFilter: true,
    columnDefs: [
        { field: 'firstName' },
        { field: 'lastName' },
        { field: 'email' }
    ]
};

The vm.users property is populated with the results from a Breeze query: vm.users属性由Breeze查询的结果填充:

var query = breeze.EntityQuery.from('Users')
    .where('firstName', 'startsWith', 'K');

return manager
    .executeQuery(query)
    .then(querySucceeded, _queryFailed);

This basically works. 这基本上是可行的。 It shows up in the grid in the way I would expect, but when I try to filter, no matter what I type, everything gets filtered out. 它以我期望的方式显示在网格中,但是当我尝试过滤时,无论键入什么内容,所有内容都会被过滤掉。 I was able to track this down to this section of code in ng-grid: 我能够在ng-grid的这部分代码中进行跟踪:

var searchEntireRow = function(condition, item, fieldMap){
    var result;
    for (var prop in item) {
        if (item.hasOwnProperty(prop)) { // <-- Works if I get rid of this condition
            var c = fieldMap[prop.toLowerCase()];
            if (!c) {
                continue;
            }

            ...

The problem is that when ng-grid is searching an entity ( item ), the necessary fields exist in item , but they fail the hasOwnProperty check. 问题在于,当ng-grid搜索实体( item )时, item存在必需的字段,但是它们没有hasOwnProperty检查。 It works if I get rid of that check, and it might even be safe, because it still checks to make sure the prop exists in the fieldMap ... but I hesitate to make such a change. 如果我取消该检查,它就可以工作,甚至可能是安全的,因为它仍在检查以确保该prop存在于fieldMap ……但我fieldMap进行此类更改。

It works fine if I do a projection, rather than an entity query, but I don't want to lose the caching that I get with entity queries (I guess I could do an entity query followed by a projection query against the cache...). 如果我执行投影而不是实体查询,则可以正常工作,但是我不想丢失实体查询获得的缓存(我想我可以先进行实体查询,然后再对缓存进行投影查询。 )。 Is there a better way to get breeze entities filterable in an ng-grid, or is this a bug? 有没有更好的方法可以使微风实体在ng-grid中可过滤,还是这是一个错误?

I'm using ng-grid 2.0.11 and angular 1.2.15. 我正在使用ng-grid 2.0.11和angular 1.2.15。

If you're model library is "backingStore" (as it is for an Angular app), none of your entity properties are "own" properties. 如果您的模型库是“ backingStore”(对于Angular应用程序而言),那么您的实体属性都不是“自有”属性。 They are all implemented as ES5 properties on the prototype of the entity type's constructor. 它们都是在实体类型的构造函数原型上作为ES5属性实现的。

If you really wanted to restrict your filter seeing the immediate data properties (not inherited data properties) of the object, you could write this: 如果您确实想限制过滤器查看对象的即时数据属性(而不是继承的数据属性),则可以这样编写:

var proto = Object.getPrototypeOf(obj);
for (var key in obj) {if (obj.hasOwnProperty(key)) /* your logic here */;}

Understand that this picks up a few Breeze properties as well such as entityAspect and won't detect properties of a "base class" if you have any entity inheritance in your model. 可以理解,这会拾取一些Breeze属性以及诸如entityAspect并且如果您的模型中有任何实体继承,也不会检测到“基类”的属性。

Now that you know why, you can decide how to proceed. 现在您知道为什么了,您可以决定如何进行。

Be aware that every Breeze entity type has circularities. 请注意,每种Breeze实体类型都有圆度。 At a minimum, the x.entityAspect.entity points back to x . 至少, x.entityAspect.entity指向x That's a problem for some 3rd party controls; 对于某些第三方控制来说,这是一个问题。 I do not know if it is a problem for the ng-Grid. 我不知道ng-Grid是否有问题。

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

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