You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在gridApi中禁用已有数据行编辑,保留空行单元格可编辑

Solution to Disable Cell Editing for Non-Empty Rows in UI-Grid

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 enableCellEditFn runs for each cell when the grid renders. It checks if both depId and depName are empty (adjust this condition if your definition of an "empty row" is different—e.g., check for undefined, empty strings, or a specific flag like isNewRow).
  • For empty rows, the function returns true, keeping cells editable. For rows with existing data, it returns false, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:25:49