0

我想删除一条记录,同时插入一条新记录:

                       scope.$apply(function () {
                            scope.myData.splice(index, 1, result);
                        });

这是行不通的。有什么帮助吗?谢谢!

4

1 回答 1

1

这是 HTTP PUT RESTful ajax 访问和更新 ng-grid 的完整解决方案。

            $.ajax({
                type: "PUT",
                url: "/api/WebAPIService",
                data: $('#editForm').serialize(),
                success: function (result) {
                    if (result) {
                        var scope = angular.element($("#ngBody")).scope();
                        var row = scope.gridOptions.selectedItems[0];
                        var index = scope.myData.indexOf(row);
                        scope.gridOptions.selectItem(index, false);
                        scope.$apply(function () {
                            scope.myData.splice(index, 1);   //delete a row from array
                        });
                        scope.$apply(function () {
                            scope.myData.splice(index, 0, result);  //add a row to the same index
                        });
                        setTimeout(selectARow, 0);   //select the the newly added row
                        function selectARow() {
                            scope.$apply(function () {
                                scope.gridOptions.selectItem(index, true);
                            });
                        };
                    }
                }
            });

这个想法是我们必须将“删除”和“添加”分开。将这两个步骤放在一个语句中看起来更合理,例如“scope.myData.splice(index, 1, result);”。但正如我测试的那样,它很简单是行不通的。此外,我们必须在 setTimeout 中包装“选择项目”,以确保在完成与“删除和添加”相关的所有操作后运行它。

于 2013-09-25T13:09:21.320 回答