如何在gridApi中禁用已有数据行编辑,保留空行单元格可编辑
Got it, let's fix this up. Your current grid has all cells set to editable statically, but we need to toggle editability based on whether the row has existing data or is empty. Here's how to adjust your configuration:
Instead of using the static enableCellEdit: true property, we'll use enableCellEditFn—a function that dynamically returns whether a cell should be editable based on the row's data. This function gets passed the rowEntity (the data object for the row), so we can check if the row has actual data or is empty.
Modified Grid Configuration Code
$scope.departmentGrid = { data: 'deplist', enableRowSelection: true, multiSelect: false, modifierKeysToMultiSelect: false, enableRowHeaderSelection: false, noUnselect: true, enableColumnMenus: false, enableHorizontalScrollbar: 0, columnDefs: [ { field: 'depId', // Use enableCellEditFn to dynamically control editability enableCellEditFn: function(rowEntity) { // Allow editing only if the row is empty (adjust condition as needed) return !rowEntity.depId && !rowEntity.depName; }, displayName: 'Department Id', width: 180 }, { field: 'depName', enableCellEditFn: function(rowEntity) { return !rowEntity.depId && !rowEntity.depName; }, displayName: 'Department Name' } ] };
How It Works
- The
enableCellEditFnruns for each cell when the grid renders. It checks if bothdepIdanddepNameare empty (adjust this condition if your definition of an "empty row" is different—e.g., check forundefined, empty strings, or a specific flag likeisNewRow). - For empty rows, the function returns
true, keeping cells editable. For rows with existing data, it returnsfalse, disabling editing.
Customization Tip
If your empty rows have a specific identifier (like a isNew flag), you can simplify the condition to:
enableCellEditFn: function(rowEntity) { return rowEntity.isNew; }
Just make sure you set that flag when adding new empty rows to your deplist array.
内容的提问来源于stack exchange,提问作者Wai Gyi

