ng-grid删除和插入同时不起作用

时间:2013-09-20 22:59:50

标签: ng-grid

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

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

这不起作用。有帮助吗?谢谢!

1 个答案:

答案 0 :(得分: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中包装“select item”,以保证在完成与“删除和添加”相关的所有操作后运行。

相关问题