4

Is it possible to set the background color for a row in slickgrid (based on data values) AND use pagination? For angular-slickgrid package.

I used getItemMetadata as suggested multiple other (old) posts - example SlickGrid: How to loop through each row and set color based on the condition?.

This code:

metadata(old_metadata_provider) {  
    return function(row) {  
        var item = this.getItem(row);  
        var ret  = (old_metadata_provider(row) || {});  

        if (item) {  
            ret.cssClasses = (ret.cssClasses || '');  
            if ("attribute" in item) {  
                return { cssClasses: 'redRow' }; //redRow is in styles.css  
            }  
        }  

        return ret;  
    }  
}  

and the call is:

this.dataViewObj.getItemMetadata = this.metadata(this.dataViewObj.getItemMetadata);

It works correctly. However, when I turn pagination on, the color does not work as expected. I read that SlickGrid re-uses the same row elements when scrolling or paginating and will overwrite the styles associated with them. Is there another way to do it? Thanks for any help on this.

I tried adding the following code, after reading suggestion from ghiscoding, but the colors are still not working when pagination is enabled.

angularGridReady(angularGrid: AngularGridInstance) {
    this.angularGrid = angularGrid;
    this.dataViewObj = angularGrid.dataView;
    this.gridObj = angularGrid.slickGrid;
    this.checkRowBackgroundColor(); //where I call the metadata function from my previous post, if the dataView object is defined.

    //I added this code:
    var self = this;
    this.dataViewObj.onPagingInfoChanged.subscribe(function (e, dataView, grid) {
            self.gridObj.invalidate();
            self.gridObj.render();
    });   
}
user2026318
  • 173
  • 1
  • 13
  • You should really provide more info in your question, typically, what have you tried? Show some code... I can only assume that you're using SlickGrid Metata (that is only an assumption since you didn't mention it), maybe the Metadata is lost or needs to invalidate/render after a page change, subscribe to page change event and re-attach metadata and/or re-invalidate/render, worth a try – ghiscoding May 21 '20 at 02:02
  • But you probably haven't tried anything of what I suggested? You should... – ghiscoding May 21 '20 at 15:43
  • ghiscoding, I tried re-invalidate/render, by adding some code to my grid ready function (called by onAngularGridCreated) but the colors are still not working correctly with pagination. See edit above. Can you clarify what you meant by "re-attach metadata" in your comment? Thanks. – user2026318 May 22 '20 at 13:41
  • the metadata is probably assigned just on your page load but lost when you change page, you might need to put back the metadata every time you change page. – ghiscoding May 22 '20 at 14:05
  • ghiscoding - I'm still not sure what you mean by "put back" the metadata? I tried to create a variable myMetadata and set it to getItemMetadata() on the initial load. And then in the onPagingInfoChanged handler I set the dataView.getItemMetadata = that variable and then ran my metadata function on it to set the color and rerender. But it didn't work. I am new to slickgrid, but I'm trying to understand how to the metadata works. Thanks. – user2026318 May 30 '20 at 16:11

1 Answers1

0

Try this approach:

angularGridReady(angularGrid: AngularGridInstance) {
    this.angularGrid = angularGrid;
    this.dataViewObj = angularGrid.dataView;
    this.gridObj = angularGrid.slickGrid;

    // check color change logic for the first time page load
    this.checkRowBackgroundColor();

    // use arrow function so that 'this' works properly
    this.dataViewObj.onPagingInfoChanged.subscribe((e, dataView, grid) => {
        // check your color change logic every time Paging Info Changed
        this.checkRowBackgroundColor();
    });   
}

Inside your checkRowBackgroundColor:

checkRowBackgroundColor() {
    // ... any of your logic

    // get metadata
    this.dataViewObj.getItemMetadata = this.metadata(this.dataViewObj.getItemMetadata);

    // rerender the grid after getting new metadata
    this.gridObj.invalidate();
    this.gridObj.render();
}

Your problem should be solved now. As I have not tested on my local machine, I can not give guarantee. You can checkout original documentation of angular-slickgrid about this specific topic here: Dynamically Add CSS Classes to Item Rows

Mazedul Islam
  • 1,543
  • 1
  • 11
  • 15
  • Thank you for your suggestion. Unfortunately it didn't solve my issue. What happens is that if I press the button to jump to the first or last page, the color is correct. But if I go one page at a time, the wrong rows have color. It's the same issue I had before. – user2026318 Jun 09 '20 at 05:16
  • the fact that the color is selected on the wrong rows means that he needs to use the DataView flag `syncGridSelection` and he's probably not, this flag will make the row selection follow correctly even after a page change, without this flag it will keep row selection index without knowing or caring that the user changed page. Just search for that flag, there's another SO answer talking about it and I'm not sure how to use with metadata – ghiscoding Jun 09 '20 at 12:09
  • I tried adding this.dataViewObj.syncGridSelection(this.gridObj, true); before re-rendering, but it didn't work. But I'm not actually selecting the rows with a mouse click...I'm trying to make the row red if the value in a certain column is null. – user2026318 Jun 09 '20 at 21:40
  • I have tested my solution on my local machine. I have noticed that, on page change, it colors according to the previous page (not current page). For example at page 1 I have [null, null, x], it colors [red, red, white], on page 2 I have [x, null, null] and color [red, red, white], then on page 3 values [null, x, null] and color [white, red, red]. Now if I go from page 3 to page 2, colors will be [red, white, red]. So, it basically coloring according to previous state. Though I have not found any solution yet, this information can be helpful. – Mazedul Islam Jun 09 '20 at 22:35
  • Thank you for testing and suggestions. Do you think I am missing a step, or is this is due to a bug in the grid? For now, I've disabled paging so that the colors are correct. But it would be nice to have both. – user2026318 Jun 10 '20 at 19:53
  • I think, it would be better to open an issue at their git repository. Then you will get response from the original contributors of Angular Slickgrid. If you can reproduce the issue in https://stackblitz.com/ then it would be easier for them to find out the bug. Moreover, their documentation does not have proper guideline for implementing the feature you are trying to implement. So, it would be great if you point out the issue to them. – Mazedul Islam Jun 10 '20 at 20:09
  • Friends, any updates on this. I was facing the same issue when the setting the `grid.setCellCssStyles`. – Ranjith Sep 10 '20 at 18:30