如何在添加数据后自动刷新Angular UI Grid

时间:2018-05-31 03:52:22

标签: javascript angularjs angular-ui-grid

我有角度UI-Grid,我想在添加数据后重新加载。请告诉我我需要添加什么?

HTML文件

<form class="form-main">
  <label>Category Name</label>
  <input type="text" id="txtCategory" ng-model="md_catname" />
  <button id="btnAdd" type="submit" class ="button-internal" ng-click="Add()">Add</button>    
  <div class="gridBigStyle" ui-grid="gridCategory" ui-grid-edit ui-grid-pagination>            
</form>

控制器文件

var myApp = angular.module('appHome');
myApp.controller("ctrlCategory", ['$scope', 'MetadataOrgFactory', 'CommonFunctionFactory', function ($scope, MetadataOrgFactory, CommonFunctionFactory) {
    $scope.gridCategory = {
        data: 'categoryData',        
        columnDefs: [
            {
                field: 'CategoryName', displayName: 'Name',                
            },           
        ]
    }

    //This is to retrieve existing categories from Database
    MetadataOrgFactory.getApiCall('getallcatgories', function (dataSuccess) {
        $scope.categoryData = dataSuccess;
    }, function (dataError) {
    });

    //Common Function being called to Add Category Name
    $scope.Add = function () {
        var objCategory = {
            'CategoryName': $scope.md_catname,            
        };
        CommonFunctionFactory.AddMasterData(objCategory, function (dataSuccess) {
        }, function (dataError) {
        });
    }
}])

常用功能文件

var appService = angular.module('appHome');
appService.factory('CommonFunctionFactory', ['MetadataOrgFactory', function (MetadataOrgFactory) {
    var dataFactory = {};

    //Adding Master Data to DB
    dataFactory.AddMasterData = function (objData) {
        MetadataOrgFactory.postApiCall(serviceFunc, objMasterData, function (dataSuccess) {
            alert("The request has been completed succesfully");
        }, function (dataError) {
    });
    };
}])

我想了解要放入哪些代码,以便在数据库中添加数据后自动刷新Angular UI Grid。

1 个答案:

答案 0 :(得分:1)

获取更新数据的完整列表作为成功更新的返回,并将数据提供给网格,即$ scope.gridOptions.data = latestData;