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

How to disable selection of cells in ag-grid?

我在一个 Angular 项目中有一个简单的 ag-grid,我想禁用其中一列中的 select 个单元格。在 selection 期间简单地删除默认的蓝色轮廓也可以。当用户在其中单击时,我只希望单元格没有视觉变化。我该怎么做?

我看到 ColDef 有一个 属性 suppressNavigable 有点帮助,因为它不允许使用 Tab 键 select 单元格,但它仍然允许select离子通过点击。此外,网格本身似乎提供 suppressCellSelection,但它似乎不够细化,似乎也不会影响任何东西。

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

这是我对这些列定义的代码:

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 example

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

Note that if we set gridOption.suppressCellSelection = true we can disable cell selection for all columns' cells.

Here the question is regarding not showing a specific column's cell's highlighted border when it is selected.

您可以通过 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;
}

并在ColDef

中使用与cellClass相同的class
suppressNavigable: true,
cellClass: 'no-border'

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

我建议使用 gridOptions 中的 suppressCellSelection 选项。我不建议 CSS 快速修复。

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

您可以试试这个 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

试试这个对我有用

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

您还可以使用 cellStyle 动态删除选择。这是一个例子:

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

现场演示

你可以试试这个,如果你想把它应用到所有的单元格上。 它对我有用。

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

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

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

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

AG Grid: Setting colour parameters using CSS variables