禁用 Ag-grid 中的复选框选择
disable checkbox selection in Ag-grid
是否可以通过保留一些使用某些约束呈现的选定行来禁用复选框选择?
我不想让用户取消选择在呈现时选择的行。
我找到了 this.gridOptions.suppressCellSelection = true;
但这只是隐藏了复选框,而我需要在禁用模式下显示复选框。
谢谢。
一种方法是在需要实现复选框的列中添加一个cellRenderer 函数。您可以通过从 cellRenderer 函数返回 true 或 false 来启用或禁用复选框。
我通过在 GridOptions
中添加 rowClassRules
解决了这个问题
rowClassRules: {
'ag-row-selected' : function(params) {
return params.node.selected === true;
},
},
这将添加 css 以禁用复选框点击
.ag-row-selected{
.ag-cell .ag-cell-wrapper .ag-selection-checkbox .ag-icon-checkbox-checked {
pointer-events: none;
}
}
当网格为 updated/refreshed 或更新节点时应用 RowClass 规则。我通过更新特定节点来做到这一点
node.setSelected(true);
// this is to trigger rowClass for selected/non-selected rows
// to disable checkbox selection
node.setData(node.data);
如果您使用内置 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;
}
}
这对我有用
cellStyle: params => {
return params.data.myStatus ? {'pointer-events': 'none', opacity: '0.4' }
: '';
}
纯 CSS 解决方法:
.ag-selection-checkbox.ag-hidden {
display: inherit !important;
opacity: 0.6;
cursor: not-allowed;
}
这将覆盖 ag-hidden 中复选框包装器的 display:none 配置。
columnDefination: [{ headerName: 'IsActive', field: '', editable: false, cellRenderer: params => { return `<input type='checkbox' disabled=true ${params.data.IsActive ? 'checked' : ''} />`; } },],
如果您想在找到过滤数据时禁用 ag-grid 复选框的 header none 那么这可能会有帮助
headerCheckboxSelectionFilteredOnly:true
是否可以通过保留一些使用某些约束呈现的选定行来禁用复选框选择? 我不想让用户取消选择在呈现时选择的行。
我找到了 this.gridOptions.suppressCellSelection = true;
但这只是隐藏了复选框,而我需要在禁用模式下显示复选框。
谢谢。
一种方法是在需要实现复选框的列中添加一个cellRenderer 函数。您可以通过从 cellRenderer 函数返回 true 或 false 来启用或禁用复选框。
我通过在 GridOptions
中添加rowClassRules
解决了这个问题
rowClassRules: {
'ag-row-selected' : function(params) {
return params.node.selected === true;
},
},
这将添加 css 以禁用复选框点击
.ag-row-selected{
.ag-cell .ag-cell-wrapper .ag-selection-checkbox .ag-icon-checkbox-checked {
pointer-events: none;
}
}
当网格为 updated/refreshed 或更新节点时应用 RowClass 规则。我通过更新特定节点来做到这一点
node.setSelected(true);
// this is to trigger rowClass for selected/non-selected rows
// to disable checkbox selection
node.setData(node.data);
如果您使用内置 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;
}
}
这对我有用
cellStyle: params => {
return params.data.myStatus ? {'pointer-events': 'none', opacity: '0.4' }
: '';
}
纯 CSS 解决方法:
.ag-selection-checkbox.ag-hidden {
display: inherit !important;
opacity: 0.6;
cursor: not-allowed;
}
这将覆盖 ag-hidden 中复选框包装器的 display:none 配置。
columnDefination: [{ headerName: 'IsActive', field: '', editable: false, cellRenderer: params => { return `<input type='checkbox' disabled=true ${params.data.IsActive ? 'checked' : ''} />`; } },],
如果您想在找到过滤数据时禁用 ag-grid 复选框的 header none 那么这可能会有帮助
headerCheckboxSelectionFilteredOnly:true