简体   繁体   English

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

[英]How to disable selection of cells in ag-grid?

I have a simple ag-grid in an Angular project and want to disable selection of cells in one of its columns.我在 Angular 项目中有一个简单的 ag-grid,我想禁用其中一列中的单元格选择。 Simply removing the default blue outline during selection would also be fine.在选择过程中简单地删除默认的蓝色轮廓也可以。 I just want no visual change to the cell when the user clicks inside it.当用户在其中单击时,我只希望单元格没有视觉变化。 How can I do this?我怎样才能做到这一点?

I see that ColDef has a property suppressNavigable that sort of helps, since it disallows using the tab key to select the cells, but it still allows selection by clicking.我看到ColDef有一个属性suppressNavigable这有点帮助,因为它不允许使用 select 单元格的 tab 键,但它仍然允许通过单击进行选择。 Also, the grid itself seems to offer suppressCellSelection but it doesn't seem granular enough and doesn't seem to affect anything anyway.此外,网格本身似乎提供了suppressCellSelection但它似乎不够细化并且似乎不会影响任何东西。

So, how can I remove this blue border cell selection?那么,如何删除这个蓝色边框单元格选择呢?

Here's the code I have for these column definitions:这是我对这些列定义的代码:

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

Here's a stackblitz example I was using to test with.这是我用来测试的一个stackblitz 示例

Here's a screenshot of the blue border I don't want to see in this column:这是我不想在此栏中看到的蓝色边框的屏幕截图:

我不想看到蓝色边框

Note that if we set gridOption.suppressCellSelection = true we can disable cell selection for all columns' cells.请注意,如果我们设置gridOption.suppressCellSelection = true我们可以禁用所有列单元格的单元格选择。

Here the question is regarding not showing a specific column's cell's highlighted border when it is selected.这里的问题是关于在选择特定列的单元格时不显示其突出显示的边框。

You can achieve this by bit of CSS and cellClass property of ColDef .您可以通过CSS和位达到这一cellClass财产ColDef

You'll need to add below CSS.您需要添加以下 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;
}

And use the same class as cellClass in ColDef并在ColDef使用与cellClass相同的类

suppressNavigable: true,
cellClass: 'no-border'

Live example: aggrid-want-to-disable-cell-selection实例: aggrid-want-to-disable-cell-selection
This won't show border for the cell you even focus using mouse click.这不会显示您甚至使用鼠标单击聚焦的单元格的边框。

I'd suggest to use the suppressCellSelection option in gridOptions.我建议在gridOptions 中使用suppressCellSelection选项。 A CSS quick fix is not something that I'd suggest to go for.我不建议使用 CSS 快速修复。

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

You can try this css hack.你可以试试这个 css hack。 no custom flags needed.不需要自定义标志。

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

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

在此处输入图片说明

Try this one it's work for me试试这个对我有用

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

You can also usecellStyle to remove the selection dynamically.您还可以使用cellStyle动态删除选择。 Here is an example:下面是一个例子:

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

Live Demo现场演示

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

You can try this, If you want to apply it over all the cells.你可以试试这个,如果你想把它应用到所有的单元格上。 It worked for me.它对我有用。

.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 supports customizing CSS variable for most themes. AG Grid 支持为大多数主题自定义 CSS 变量。 Try defining the selection border color on the grid container or any parent.尝试在网格容器或任何父级上定义选择边框颜色。

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

AG Grid: Setting colour parameters using CSS variables AG Grid:使用 CSS 变量设置颜色参数

Simple add suppressCellSelection={true} in the grid to avoid Blue border on cell selection在网格中简单添加suppressCellSelection={true}以避免单元格选择出现蓝色边框

<Grid
    ref={GridRef}
    columnDefs={Mapper}
    rowData={data}
    gridOptions={gridOptions}
    onGridReady={onGridReady}
    suppressRowHoverHighlight={true}
    columnHoverHighlight={true}
    suppressCellSelection={true} // add this line to avoid blue border color on the cell
    onRowClicked={console.log(e)}
    onFilterChanged={useCallback(() =>
        setRow(GridRef.current.api.getDisplayedRowCount())
    )}
></Grid>

this workd guys这个工作伙计们
add this line inside same props className="ag-theme-alpine".ag-cell-focus, .ag-cell-no-focus { border: none;important.在相同的道具 className="ag-theme-alpine".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; } /* 这 CSS 是不为具有“无边框”的列应用边框 class */.no-border:ag-cell:focus { border; none:important;无:重要; outline: none;大纲:无; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM