ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

Filtering doesn't work when fields are object properties

Open esheffield-ivantage opened this issue 13 years ago • 25 comments

Filtering doesn't seem to work when the data in the grid consists of nested objects. For example, suppose you have data that looks like this:

$scope.myData = [{fields: {name: "Moroni", age: 50}},
                 {fields: {name: "Tiancum", age: 43}},
                 ...];

You can configure the column definitions so that the data displays properly in the grid:

columnDefs:[
  {
    field: "fields['name']",
    displayName: "Name"
  },
  ...
]

... but any attempts at filtering with the search box will result in no items showing in the grid. Presumably this is because ng-grid filters strings, not objects. Is there a way to get filtering to work in such a case? Here's a Plunker demonstrating this example: http://plnkr.co/edit/l2J1S9

esheffield-ivantage avatar Jul 02 '13 21:07 esheffield-ivantage

This is fixed in latest 2.0.7.

jonricaurte avatar Jul 06 '13 20:07 jonricaurte

What you want to do is instead of using fields['name'] you want to use fields.name like so:

http://plnkr.co/edit/SFz7jf?p=preview

jonricaurte avatar Jul 06 '13 20:07 jonricaurte

@jonricaurte Thanks for the quick fix! Unfortunately, this still doesn't work in my case since my attribute names haves dots in them and therefore require square bracket notation to access. Apologies, I should have reflected this in my original plunker. Here's an updated one: http://plnkr.co/edit/wClSI4

esheffield-ivantage avatar Jul 08 '13 13:07 esheffield-ivantage

I'll look into it.

jonricaurte avatar Jul 09 '13 14:07 jonricaurte

If you have MULTIPLE nested objects displayed in the grid then filtering ignores all of them and only applies to non-nested columns. http://plnkr.co/edit/o6odBn?p=preview Note: link shows it is broken in 2.0.7. Removing either nested reference makes the other work.

Additionally when you format data for display (converting date formats for example) then filtering appears to be broken for that column. See http://plnkr.co/edit/MCtPEk?p=preview This is a problem since we need to localize date formatting. So the value on the wire is different from the value in the grid.

zstlaw avatar Jul 16 '13 16:07 zstlaw

Just wondering if there's been any progress on this one? I've got pretty much the same problem.

sonicparke avatar Sep 13 '13 17:09 sonicparke

The filtering does work for "fields.name" but not if capitalised "Item.name" like my data is being returned:

http://plnkr.co/edit/j18SQC?p=preview

scottmcdonnell avatar Oct 30 '13 11:10 scottmcdonnell

@scottmcdonnell You've given me hope but I changed my structure to all lowercase and it's still not working. Can you see what I'm doing wrong? http://plnkr.co/edit/AANkzL?p=preview

sonicparke avatar Oct 30 '13 17:10 sonicparke

The square brackets don't seem to work. Funnily enough the array can be accessed using dots. Instead of:

field: 'level2array[0].value'

try:

field: 'level2array.0.value'

http://plnkr.co/edit/r6Ezse?p=preview

filtering is now working for just the first column 'level2array.0' - hmmn not much better. I think you will need to change your json structure to really make this work...

scottmcdonnell avatar Oct 30 '13 18:10 scottmcdonnell

Thanks for the .0. thought. But yeah, I wonder why it only picks up the first column and not the rest of the items in that 2nd array.

sonicparke avatar Oct 30 '13 18:10 sonicparke

Any progress on this issue?

chandraCM avatar Dec 17 '13 16:12 chandraCM

Filter doesn't work for nested objects; when i make as single object it works. Is there any way to make it work with nested objects.

chandraCM avatar Dec 17 '13 16:12 chandraCM

Still can't filter using filter options of ng-grid when using nested objects

pamcdm avatar Mar 12 '14 17:03 pamcdm

Not much luck with this any update ?

acostaf avatar Mar 24 '14 15:03 acostaf

This still doesn't seem to work in 2.0.11.

sanchopancho13 avatar May 04 '14 04:05 sanchopancho13

any update on this issue? Using custom cell templates also break search feature. see the plunkr http://plnkr.co/edit/QA690p?p=preview

mizanRahman avatar May 09 '14 08:05 mizanRahman

No updates. Anyone is free to take initiative and submit a PR though.

c0bra avatar May 09 '14 14:05 c0bra

@mizanRahman - The search works by only searching for columnDef.field so keep that the same and refer to your link by accessing the row. See this plunkr - http://plnkr.co/edit/gOpMM8esXvyAkN2MvKXp?p=preview.

Nested search seems to be working fine for me now. Anyone have any current version plunkr test case of a search not working?

scottmcdonnell avatar May 09 '14 14:05 scottmcdonnell

I tried the 2.0.11 version and filtering with complex field names(item.name) is still not working for me.

skamalak avatar May 15 '14 23:05 skamalak

I've created a plunkr to demonstrate the issue. The filtering does work when searchEntireRow() is used (as demonstrated by the plunkr created by scottmcdonnell), but it doesn't work when searchColumn() is used: http://plnkr.co/edit/QjdHIjrUQiFyBrT9NfYb?p=preview (It only works when the displayName is used in the search conditions)

The problem is that searchColumn searches the column like this:


var col = self.fieldMap[condition.columnDisplay];

"condition.columnDisplay" contains the fieldname of the column in lowercase (in my case: "user.name"), but this key is not present in "self.fieldMap"! "self.fieldMap" contains an object with key "user" (this object does provide the name as a nested property), and it also contains the key "profile", which is the displayName in lowercase and without spaces. but the key "user.name" (with the dot) is not present in "self.fieldMap", which is the reason why "col" is undefined and no search results are returned.

The function buildSearchConditions() provides condition.columnDisplay, but it uses the columnName instead of the displayName (not sure if this is meant this way):


searchConditions.push({
    column: columnName,
    columnDisplay: columnName.replace(/\s+/g, '').toLowerCase(),
    regex: getRegExp(columnValue, 'i')
});

If I change r2753 (where "fieldMap" is constructed) in ng-grid.debug.js from:


if (col.displayName) {
    self.fieldMap[col.displayName.toLowerCase().replace(/\s+/g, '')] = col;
}

to:


if (col.displayName) {
    self.fieldMap[col.displayName.toLowerCase().replace(/\s+/g, '')] = col;
    self.fieldMap[col.field.toLowerCase().replace(/\s+/g, '')] = col;
}

Then the column filtering does work for nested properties (without having to use the displayName).

Please reopen this issue and fix this problem. I'm sorry I didn't create a PR but I don't have much experience with GIT, so I'd rather explain the problem here.

cs-StephanieDM avatar May 21 '14 13:05 cs-StephanieDM