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

CellTemplate directive disassociated with row data

Open jklaus opened this issue 10 years ago • 24 comments

I'm trying to understand the cause of what seems to be a known issue the use of directives in cellTemplate.

I have logic which examines a nested array, if any of these array objects have a status of 'prohibited' I am rendering 'prohibited' in that grid cell. When I execute this logic within a $filter it works fine but if I put this logic in the link (or controller) of my directive it only works proper for the first ~20 rows. Once you begin scrolling, or sort the grid, the column rendering the directive becomes disassociated with the rest of the data. I have executed a similar test as proposed here and have found that $scope.row.entity is indeed disassociated with the actual row being rendered.

While I can work around this issue via the $filter approach it feels dirty since I'll also be wrapping the 'prohibited' string in dom elements to add styling.

If need be I can create a plunkr to demonstrate what I'm seeing.. but at this point I am certain the issue arises when the filtering logic is executed inside the directive. Even calling the actual $filter inside the directive causes this issue. Yet I can call the $filter via cellFilter or directly in the markup of the cellTemplate (cellTemplate:'

{{MODEL_COL_FIELD | MyFilterHere}}
') without issue.

jklaus avatar Dec 16 '15 16:12 jklaus

Sounds like it's not getting reevaluated on scroll. We reuse cells when scroll, cganging which row/col tgat cell points at.

Sent from my iPhone

On 17 Dec 2015, at 5:16 AM, jKlaus <[email protected]mailto:[email protected]> wrote:

I'm trying to understand the cause of what seems to be a known issue the use of directives in cellTemplate.

I have logic which examines a nested array, if any of these array objects have a status of 'prohibited' I am rendering 'prohibited' in that grid cell. When I execute this logic within a $filter it works fine but if I put this logic in the link (or controller) of my directive it only works proper for the first ~20 rows. Once you begin scrolling, or sort the grid, the column rendering the directive becomes disassociated with the rest of the data. I have executed a similar test as proposed herehttp://stackoverflow.com/questions/30230268/custom-directive-in-cell-template-in-ui-grid-angularjs and have found that $scope.row.entity is indeed disassociated with the actual row being rendered.

While I can work around this issue via the $filter approach it feels dirty since I'll also be wrapping the 'prohibited' string in dom elements to add styling.

If need be I can create a plunkr to demonstrate what I'm seeing.. but at this point I am certain the issue arises when the filtering logic is executed inside the directive. Even calling the actual $filter inside the directive causes this issue. Yet I can call the $filter via cellFilter or directly in the markup of the cellTemplate (cellTemplate:'

{{MODEL_COL_FIELD | MyFilterHere}}
') without issue.

Reply to this email directly or view it on GitHubhttps://github.com/angular-ui/ui-grid/issues/4869.

PaulL1 avatar Dec 16 '15 17:12 PaulL1

@PaulL1 it seems its something to that effect but as I scroll I do see the directive being hit. Just what is rendered doesn't match the correct row. Ie row 25 will be in regards to application 'GMail' but the directive will run on application 'Skype'. Or at least it renders the as if it ran on 'Skype'.

jklaus avatar Dec 16 '15 17:12 jklaus

Its worth noting that the cellTemplate is getting the correct row.entity as I have a column beside this one that renders proper. The entire issue looks to be caused by having the directive rendered via cellTemplate.

jklaus avatar Dec 16 '15 18:12 jklaus

I think you'd need to provide a plunker. It sounds like your code is perhaps holding a reference to the old item.

I wonder whether you could get the effect you're seeking by applying a cellClass instead, which is a standard function, and then using css to render "prohibited" when that cellClass is applied?

PaulL1 avatar Dec 16 '15 18:12 PaulL1

I can come up with many work-arounds but later on this project I will need to be rendering directives in a similar manner.. or at least I'd like to be able to. Need to get a handle on this problem. It may be something stupid I'm not seeing but given the tests I've performed it seems there is something wrong with the cellTemplate -> directive handoff for rows that aren't initially rendered.

@PaulL1 plnkr coming your way soon.

jklaus avatar Dec 16 '15 18:12 jklaus

@PaulL1 Here is a streamlined implementation demonstrating the issue I'm seeing. PLNKR

Note the misalignment that occurs when you modify the sort on Dev UUID or scroll down the list.

jklaus avatar Dec 16 '15 20:12 jklaus

@PaulL1
I ended up posting my issue on stack overflow and someone suggested wrapping all of the logic within a watch. While this does solve the issue I don't quite understand why. The directive is still being fired regardless, it just seems as though its running on stale/disassociated data outside of the watch. Perhaps I have a flawed view of how the link function should be operating?

jklaus avatar Dec 17 '15 15:12 jklaus

Similar issue here! 1. don't put instance data on the cell scope cause as PauL1 mentions... those cells get reused/re-pointed on scroll. 2. cellTemplate directives don't get re-linked on scroll :(. Still looking for a solution to #2 since I currently have some necessary logic in my custom directive's link function...

mermonkey avatar Dec 22 '15 17:12 mermonkey

@mermonkey There is a solution provided here but it isn't beautiful.

That said I'm having a hard time understanding why things aren't working as they 'should'. If I move my logic to a filter and call the filter from the cellTemplate it works fine. Is it just that the link/controller of any directives contained within a cellTemplate aren't being fired new for each row as you scroll? If that is the case.. why would that be desired?

jklaus avatar Dec 22 '15 18:12 jklaus

Thanks! the $watch also fixes me... and I also wonder if it should really be necessary... I'm using a watch on the row at the moment, but wondering how efficient that is... link: function( scope ) { // link doesn't fire on scroll... how inefficient is this w-a? scope.$watch( "row", function( row ) {

cheers!

mermonkey avatar Dec 22 '15 18:12 mermonkey

That's just it though, for me at least, the link function does fire as I scroll. It just seems its execution is mistimed and the rows don't remain synced.

jklaus avatar Dec 22 '15 18:12 jklaus

@PaulL1, hey, there's another unintended side-effect of ui-grid's behavior of reusing cells as you describe above: If you don't use the $watch solution as prescribed above, resorting other columns will cause the column with the directive in the cell template to not change.

Here's a plunker showing the issue. Try sorting the name column. You'll notice the company column doesn't move. If you uncomment the $watch in the directive, the problem goes away.

I'm sure the cell reuse is done for performance reasons, but I'm thinking that at the very least, cells with a custom template should be refreshed. That said, I don't think angular provides a way to do this generically and anything more than just a cell/column refresh can have massive performance ramifications.

I admit, I don't know what the "right" solution is here. I do feel that the onus should not be on the user to have to place $watches in their code to work around this. Regardless of whether you disagree, I'm thinking this behavior should at least be documented in both the cellTemplate and sorting docs.

Thoughts?

icfantv avatar Dec 30 '15 07:12 icfantv

still actual, but $watch helps.

alex-nesterov avatar Jan 20 '16 13:01 alex-nesterov

I have a very complex directive in my cellTemplate and am running into this problem. Scrolling starts getting very ugly performance-wise when the $watch from the workaround runs (I do quite a bit of calculation and complex dom contruction within in).

So here's another option to alleviate the problem a bit and potentially make it appear less frequently:

my_grid_config.columnVirtualizationThreshold = 200

This avoids cell reuse entirely if the total number of rows is below the threshold.

I still had to implement the watch to handle the case of more rows, of course.

As a drawback I want to mention that when deleting rows (using gridOptions.data.splice(...)), more rows will have to be moved and this results in a large (for me) performance penalty on row delete operations.

molecular avatar May 23 '17 08:05 molecular

Is there any way to fix this? my_grid_config.columnVirtualizationThreshold = 200 didn't work either.

normandanielmq avatar Aug 14 '17 02:08 normandanielmq

@normandanielmq: "my_grid_config.columnVirtualizationThreshold = 200" is not a solution to the problem, at least not if you have more than 200 rows. It's just a way to lessen performance problems resulting from using the workaround described further up (the stackoverflow link by @jklaus, I think)

molecular avatar Aug 14 '17 13:08 molecular

Any chance this gets resolved in the incoming weeks...? Months...? It looks kind of like a major issue.

inad9300 avatar Jun 26 '18 05:06 inad9300

If you have any idea how to fix it, a pull request will certainly be accepted and appreciated. Unfortunately, the ui-grid team is significantly smaller than it used to be, mainly just me, and with almost 800 bugs in the back log, I don’t have a good way to prioritize. At the moment, I am concentrating on clean ups with bug fixes for every release, I will try to get to this in the coming months if time allows.

mportuga avatar Jun 26 '18 12:06 mportuga

A warning message similar to that of https://github.com/angular-ui/bootstrap would be appreciated. Just so people don't jump into this library without knowing that it is barely maintained.

inad9300 avatar Jun 26 '18 12:06 inad9300

I will try to add something to the website on the next release then. I am sorry to disappoint.

On Tue, Jun 26, 2018 at 8:33 AM Daniel M. [email protected] wrote:

I warning message similar to that of https://github.com/angular-ui/bootstrap would be appreciated. Just so people don't jump into this library without knowing that it is barely maintained.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/angular-ui/ui-grid/issues/4869#issuecomment-400290923, or mute the thread https://github.com/notifications/unsubscribe-auth/AGa920JdOyEFIkoC8XiQvhTGTrD0tu28ks5uAiopgaJpZM4G2o6j .

mportuga avatar Jun 26 '18 13:06 mportuga