简体   繁体   English

禁用 Ag-grid 中的复选框选择

[英]disable checkbox selection in Ag-grid

is it possible to disable checkbox selection by preserving some selected rows rendered with some constraints?是否可以通过保留一些使用某些约束呈现的选定行来禁用复选框选择? I dont want to allow users to deselect rows which were selected while rendering.我不想让用户取消选择在呈现时选择的行。

I found this.gridOptions.suppressCellSelection = true;我发现this.gridOptions.suppressCellSelection = true; but this just hides the checkbox whereas i need to show the checkbox in disable mode.但这只是隐藏了复选框,而我需要在禁用模式下显示复选框。

Thanks.谢谢。

I resolved it by adding rowClassRules in GridOptions我通过在 GridOptions 中添加rowClassRules来解决它

            rowClassRules: {
                'ag-row-selected' : function(params) {
                    return params.node.selected === true;
                },
            },

This will add css as below to disable checkbox click这将添加如下 css 以禁用复选框单击

.ag-row-selected{
        .ag-cell .ag-cell-wrapper .ag-selection-checkbox .ag-icon-checkbox-checked {
            pointer-events: none;
        }
    }

RowClass rules are applied when grid is updated/refreshed or nodes are updated.更新/刷新网格或更新节点时应用 RowClass 规则。 I did it by updating specific nodes我是通过更新特定节点来做到的

           node.setSelected(true);
           // this is to trigger rowClass for selected/non-selected rows
           // to disable checkbox selection
           node.setData(node.data);

This worked for me这对我有用

 cellStyle: params => {
          return params.data.myStatus ? {'pointer-events': 'none', opacity: '0.4' }
            : '';
        }

One way is to add a cellRenderer function to the column for which the checkbox is need to be implemented.一种方法是向需要实现复选框的列添加一个 cellRenderer 函数。 You can enable or disable the checkbox by returning true or false from the cellRenderer function.您可以通过从 cellRenderer 函数返回 true 或 false 来启用或禁用复选框。

If you're using the builtin agGroupCellRenderer to render checkbox for multiple selection, you can turn off the node's selectable flag when deciding whether to render checkbox or not.如果您使用内置的agGroupCellRenderer来渲染多选复选框,您可以在决定是否渲染复选框时关闭节点的selectable标志。

cellRenderer: "agGroupCellRenderer",
cellRendererParams: {
    checkbox: function(params) {
        const node = params.node;
        const isGroupRow = node.level === 0; //only show the checkbox on group row.

        if(isGroupRow) {
            params.node.selectable = //your condition whether the rendered checkbox is selectable or not
        }

        return isGroupRow;
    }
}

Pure CSS workaround:纯 CSS 解决方法:

.ag-selection-checkbox.ag-hidden {
  display: inherit !important;
  opacity: 0.6;
  cursor: not-allowed;
}

This will overwrite the display:none configuration in ag-hidden for the checkbox wrapper.这将覆盖复选框包装器的 ag-hidden 中的 display:none 配置。

{ headerName: 'IsActive', field: '', editable: false, cellRenderer: params => { return `<input type='checkbox' disabled=true  ${params.data.IsActive ? 'checked' : ''} />`; } }

If you want to disable the header of the ag-grid checkbox when filter data is found none then this might be helpful如果您想在找不到过滤数据时禁用 ag-grid 复选框的标题,那么这可能会有所帮助

   headerCheckboxSelectionFilteredOnly:true 

The above solution works but when user clicks on Select All or Unselect All option from Header this gets changed automatically上面的解决方案有效,但是当用户单击标题中的全选或取消全选选项时,这会自动更改

The feature to display Disabled Checkboxes is added to AG grid in version 28.1.0.在 28.1.0 版本中,AG 网格添加了显示禁用复选框的功能。 Changelog here https://www.ag-grid.com/changelog/?fixVersion=28.1.0 .这里的更新日志https://www.ag-grid.com/changelog/?fixVersion=28.1.0 Here is the example from official docs https://www.ag-grid.com/react-data-grid/row-selection/#example-disabled-checkboxes In addition a feature also allows to checkboxes as selected and disabled.这是官方文档https://www.ag-grid.com/react-data-grid/row-selection/#example-disabled-checkboxes中的示例 此外,还有一项功能允许选中和禁用复选框。 eg https://www.ag-grid.com/react-data-grid/row-selection/#example-force-enable-checkboxes例如https://www.ag-grid.com/react-data-grid/row-selection/#example-force-enable-checkboxes

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

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