尝试在取消时刷新Angular Ui-Grid编辑单击

时间:2017-11-11 01:06:38

标签: javascript angularjs angular-ui-grid

我正在使用 Angular ui-grid 。我正在努力完成一些应该相当简单但却无法找到有效的东西。我想要做的就是允许用户编辑网格,然后选择单击取消按钮:

string sql = String.Format("insert into {0}({1}) values({2});", tableName, columns, values);
using (SQLiteConnection myConn = new SQLiteConnection(dbConnection))
{
    try
    {
        myConn.Open();

        using (SQLiteCommand sqCommand = new SQLiteCommand(sql, myConn))
        {
            sqCommand.CommandText = sql;
            int rows = sqCommand.ExecuteNonQuery();
            return !(rows == 0);
        }
    }
    catch (Exception e)
    {
        // do exception handling
    }
}

此图标将基本上刷新网格,或将网格单元格设置回其上次保存的值。在这种情况下,不仅仅是简单刷新网格工作:

<icon name="cancel" ng-disabled="$ctrl.canceldisabled" ng-click="$ctrl.cancelBSS()" size="20px" title="Cancel"></icon>

我已尝试过大多数api选项 - notifyDataChange,我已尝试将gridOptions.data重置为上次保存的状态,但两者都没有做任何事情。 什么都没有改变。

1 个答案:

答案 0 :(得分:1)

您可以尝试在开始单元格编辑时保存oldValue,然后在按钮单击时恢复它:

$scope.gridOptions.onRegisterApi = function(gridApi) {
    gridApi.edit.on.beginCellEdit(null, function (rowEntity, colDef, newValue, oldValue) {                    
        $scope.savedCell = {
            entityHashKey: rowEntity.$$hashKey,
            field: colDef.field,
            value: rowEntity[colDef.field]
        }
    });
}

$scope.cancelEdit = function() {
    var row = $scope.gridApi.grid.rows.find(function(row) {
        if($scope.savedCell != null)
            return row.entity.$$hashKey == ctrl.savedValue.entityHashKey;
    })
    if(row == undefined)
        return;
    row.entity[$scope.savedCell.field] = $scope.savedCell.value;
}