如何禁用ag-grid中的单元格选择?

问题描述 投票:0回答:8

我在 Angular 项目中有一个简单的 ag-grid,并且想要禁用其一列中单元格的选择。在选择过程中简单地删除默认的蓝色轮廓也可以。我只是希望当用户在单元格内部单击时,单元格的视觉效果不会发生变化。我该怎么做?

我看到

ColDef
有一个属性
suppressNavigable
有点帮助,因为它不允许使用 Tab 键选择单元格,但它仍然允许通过单击进行选择。另外,网格本身似乎提供了
suppressCellSelection
,但它似乎不够精细,而且似乎不会影响任何东西。

那么,如何删除这个蓝色边框单元格选择?

这是我用于这些列定义的代码:

this.columnDefs = [
  { headerName: 'One', field: 'one' },
  { headerName: 'Two', field: 'two' },
  { 
    // I want to disable selection of cells in this column
    headerName: 'I want no cell selection!', 
    field: 'three',   
    suppressNavigable: true,
    editable: false,
  }
];

这是一个 stackblitz 示例我用来测试。

这是我不想在本专栏中看到的蓝色边框的屏幕截图:

javascript angular ag-grid ag-grid-angular ag-grid-ng2
8个回答
48
投票

请注意,如果我们设置

gridOption.suppressCellSelection = true
,我们可以禁用所有列单元格的单元格选择。

这里的问题是关于在选择特定列的单元格时不显示其突出显示的边框。

您可以通过一些 CSS 和

cellClass
ColDef
属性来实现这一点。

您需要添加以下 CSS。

.ag-cell-focus,.ag-cell-no-focus{
  border:none !important;
}
/* This CSS is to not apply the border for the column having 'no-border' class */
.no-border.ag-cell:focus{
  border:none !important;
  outline: none;
}

并在

cellClass
中使用与
ColDef

相同的类
suppressNavigable: true,
cellClass: 'no-border'

实例:aggrid-want-to-disable-cell-selection
这不会显示您使用鼠标单击聚焦的单元格的边框。


更新

根据X.Aurther提到的文档和评论,该选项已重命名为

suppressCellFocus


32
投票

我建议使用 gridOptions 中的

suppressCellSelection
选项。我不建议采用 CSS 快速修复。

this.gridOptions = {
  // Your grid options here....
  suppressCellSelection: true
};

根据 @aderchox 的建议,从 v27.0 开始,该选项已重命名为

suppressCellFocus


6
投票

你可以尝试这个CSS hack。无需自定义标志。

.ag-cell-focus, .ag-cell {
    border: none !important;
}

示例 - https://stackblitz.com/edit/aggrid-want-to-disable-cell-selection-answer?file=src%2Fstyles.css


5
投票

您还可以使用

cellStyle
动态删除选择。这是一个例子:

{
  headerName: "Is Selectable",
  cellStyle: (params) => {
    if (!params.value) {
      return { border: "none" };
    }
    return null;
  },
  ...
},
{
  headerName: "UnSelectable",
  cellStyle: { border: "none" },
  ...
}

现场演示

Edit 50862009/how-to-disable-selection-of-cells-in-ag-grid/50863144#50863144


0
投票

试试这个,它对我有用

::ng-deep .ag-cell-focus,.ag-cell-no-focus{
border:none !important;
}
::ng-deep .no-border.ag-cell:focus{
border:none !important;
outline: none;
}

0
投票

如果您想将其应用于所有单元格,您可以尝试此操作。 这对我有用。

.ag-cell-focus,.ag-cell-no-focus{
  border: 1px solid transparent !important;
}

::ng-deep .ag-cell:focus{
  border: 1px solid transparent !important;
  outline: none;
}

0
投票

AG Grid 支持为大多数主题自定义 CSS 变量。尝试在网格容器或任何父容器上定义选择边框颜色。

--ag-range-selection-border-color: transparent;

AG 网格:使用 CSS 变量设置颜色参数


0
投票

这很有效,伙计们
在相同的 props className="ag-theme-alpine" 中添加此行 .ag-细胞焦点, .ag-细胞-无焦点{ 边框:无!重要; } /* 此 CSS 不会为具有 'no-border' 类的列应用边框 */ .no-border.ag-cell:焦点 { 边框:无!重要; 概要:无; }

© www.soinside.com 2019 - 2024. All rights reserved.