【问题标题】:Angular SlickGrid is it possible to use pagination AND set row color based on value?Angular SlickGrid 是否可以使用分页并根据值设置行颜色?
【发布时间】:2020-09-07 09:23:26
【问题描述】:

是否可以为 slickgrid 中的一行设置背景颜色(基于数据值)并使用分页?对于 angular-slickgrid 包。

我使用 getItemMetadata 作为建议的多个其他(旧)帖子 - 例如SlickGrid: How to loop through each row and set color based on the condition?

这段代码:

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;  
    }  
}  

电话是:

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

它工作正常。但是,当我打开分页时,颜色无法按预期工作。我读到 SlickGrid 在滚动或分页时重复使用相同的行元素,并将覆盖与它们关联的样式。还有另一种方法吗?感谢您对此的任何帮助。

在阅读了 ghiscoding 的建议后,我尝试添加以下代码,但启用分页后颜色仍然不起作用。

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();
    });   
}

【问题讨论】:

  • 您确实应该在您的问题中提供更多信息,通常,您尝试过什么?显示一些代码...我只能假设您正在使用 SlickGrid Metata(这只是一个假设,因为您没有提到它),可能元数据丢失或需要在页面更改后无效/呈现,订阅页面更改事件和重新附加元数据和/或重新无效/渲染,值得一试
  • 但是你可能没有尝试过我建议的任何东西?你应该...
  • ghiscoding,我尝试通过向我的网格就绪函数(由 onAngularGridCreated 调用)添加一些代码来重新无效/渲染,但颜色仍然无法正常使用分页。请参阅上面的编辑。您能否在评论中澄清“重新附加元数据”的含义?谢谢。
  • 元数据可能只是在您的页面加载时分配,但在您更改页面时丢失,您可能需要在每次更改页面时放回元数据。
  • ghiscoding - 我仍然不确定您所说的“放回”元数据是什么意思?我尝试创建一个变量 myMetadata 并在初始加载时将其设置为 getItemMetadata() 。然后在 onPagingInfoChanged 处理程序中设置 dataView.getItemMetadata = 该变量,然后在其上运行我的元数据函数以设置颜色并重新渲染。但它没有用。我是 slickgrid 的新手,但我正在尝试了解元数据的工作原理。谢谢。

标签: css angular typescript pagination slickgrid


【解决方案1】:

试试这个方法:

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();
    });   
}

在你的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();
}

您的问题现在应该解决了。由于我没有在本地机器上测试过,我不能保证。您可以在此处查看angular-slickgrid 关于此特定主题的原始文档:Dynamically Add CSS Classes to Item Rows

【讨论】:

  • 感谢您的建议。不幸的是,它并没有解决我的问题。发生的情况是,如果我按下按钮跳转到第一页或最后一页,颜色是正确的。但是如果我一次翻一页,错误的行就会有颜色。这和我之前遇到的问题一样。
  • 在错误的行上选择颜色的事实意味着他需要使用 DataView 标志syncGridSelection 而他可能不需要,即使在页面更改后,此标志也会使行选择正确,如果没有此标志,它将保留行选择索引,而无需知道或关心用户更改了页面。只需搜索该标志,还有另一个 SO 答案在谈论它,我不确定如何使用元数据
  • 我尝试添加 this.dataViewObj.syncGridSelection(this.gridObj, true);在重新渲染之前,但它没有工作。但我实际上并没有通过鼠标单击来选择行......如果某一列中的值为空,我试图将行设为红色。
  • 我已经在本地机器上测试了我的解决方案。我注意到,在页面更改时,它会根据上一页(而不是当前页面)着色。例如,在第 1 页我有 [null, null, x],它的颜色为 [red, red, white],在第 2 页我有 [x, null, null] 和颜色 [red, red, white],然后在页面上3 个值 [null, x, null] 和颜色 [white, red, red]。现在,如果我从第 3 页转到第 2 页,颜色将是 [red, white, red]。所以,它基本上根据之前的状态着色。虽然我还没有找到任何解决方案,但这些信息可能会有所帮助。
  • 感谢您的测试和建议。您认为我错过了一步,还是由于网格中的错误?目前,我已禁用分页以使颜色正确。但两者兼得就好了。
猜你喜欢
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多