angular-ui / ui-grid

UI Grid: an Angular Data Grid
http://ui-grid.info
MIT License
5.39k stars 2.47k forks source link

How do I get access to entire row.entity object inside colDef.sortingAlgorithm? #3219

Closed cleon26 closed 9 years ago

cleon26 commented 9 years ago

I've been looking into sorting a little bit and have come up with a few questions. We have a grid that uses a directive inside the cell template to get display data... For example, my data looks like this: var data = [ { resourceId: 123, roleId: 332 }, { resourceId: 998, roleId: 465 }, { resourceId: 653, roleId: 882 }, { resourceId: 564, roleId: 332 }, ];

The grid itself uses a directive in the cellTemplate to get the "name" of the resource from a cached list. var resourceCol = { displayName : 'Resource', field : 'resourceId', cellTemplate : '<div><get-name resource-id="row.entity.resourceId"></get-name></div>' };

Two questions:

  1. I assume that there is no simple trick to parse out the name written by the directive and sort using that... or is there?
  2. While googling, I've looked at adding a sortingAlgorithm to the column definition, is there a way to get the entire row.entity object inside the sort function? In this particular case, it may not help, but I can think of others where I will need to sort by another field in the dataset.
c0bra commented 9 years ago

@cleon26

I don't think that you can access the row.entity object within sortingAlgorithm currently, which is really unfortunate. I know at least one other person has asked about it and we need to address it.

Without that ability I think that sort of makes your first question moot, but I might have misunderstood. Feel free to try to clarify your use case.

PaulL1 commented 9 years ago

If I wanted to display one value but sort by another, I'd be tempted to give my column field as the one I wanted to sort (or filter) by, and then modify the cellTemplate (or use a filter or whatever) to display the data I want.

Another option would be to manipulate your data. So if your row definition is: { field1: value1, field2: value2, field3: value3 }

And you want to display field2, but sort by field1, you could arguably modify your data to be: { subObject: { field1: value1, field2: value2 }, field1: value1, field2: value2, field3: value3 }

You could then have a columnDef that was something like: { field: 'subObject', cellTemplate: '<div>{{ row.entity.subObject.field2 }}</div>', sortAlgorithm: function( a, b ) { return a.field1 - b.field1; }}

I didn't test any of that code or do a plunker, so expect many typos and general misnaming of things, but that concept should work in theory.

cleon26 commented 9 years ago

Hello again,

I think I tried to ask too much in this post initially and confused myself about what I was asking.

Yes, sorting by another field or even with the subobject idea would work. As long as the data has all the fields I need.

Here's a plunkr of my exact scenario: http://plnkr.co/edit/AqbL7SHsUc7pDyAwl0M2?p=preview

It is a pretty stripped down version of what I'm trying to accomplish but it gets the point across I think. Our data comes across the wire with only id's. If you could just convince my api/dba coders to give me all the data all the time, that would be great :) We use essentially a dictionary to lookup the names based on the id's. is a little more complex but the end goal is that it just dumps the name into the cell. What do you suggest as far as sorting this data? To be clear, I'd want to sort by the resolved name, not the id.

I suppose I could enrich the data myself by looking up the id's and adding properties, but I'd rather not do that - it would require maintenance in many places instead of just one.

PaulL1 commented 9 years ago

OK, so I would probably use a filter rather than a directive to get you the name - that's what angular filters do. But it probably makes only a little difference - it means you don't have to override the cell template and therefore things would be a bit more maintainable.

Given that you have the names in a dictionary, can you not just use that same lookup function in the sort function?

That would give you code more like: http://plnkr.co/edit/m7Rs7i37dOJDW8fjX1t6?p=preview, which sorts.

cleon26 commented 9 years ago

That looks interesting. I'll implement it here and see how it looks. Thanks for your help.

PaulL1 commented 9 years ago

This is a useful request, but not really in the current roadmap. We have far too many good ideas in the repository already, giving the impression that we have a project riven with defects.

Accordingly, I am leaving the enhancement tag on this, but closing it. If someone would like this functionality, we'd welcome a pull request that adds it.