0

I have angular UI-Grid which I want to reload after the data is added. Please let me know what do I need to add?

HTML File

<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>

Controller File

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) {
        });
    }
}])

Common Function File

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) {
    });
    };
}])

I want to understand what code to put so that Angular UI Grid refresh automatically after data add in DB.

3
  • You no need to refresh the page as angularjs comes with 2 way binding. Are you facing any issues? Commented May 31, 2018 at 5:22
  • When we add the data then it is successfully updated in database but grid did not refresh with data. When I refresh the page by F5 then only the new data shown in Grid. Is there any way to show the new data in grid as soon as it added in database. Commented May 31, 2018 at 15:59
  • You can implement pooling mechanism to pull latest data. It will periodically check for new data in the database. To avoid huge data transfer you can request only for new data in subsequent requests. Commented May 31, 2018 at 18:26

1 Answer 1

1

Get the full list of updated data as the return of successful updation and , feed the data to the grid, ie $scope.gridOptions.data = latestData;

Sign up to request clarification or add additional context in comments.

3 Comments

It is good suggestion but then I need to give service call again to get the latest data in database and bind with grid. If you have any other idea then can you please give an example here?
other solution is , use location.reload(); method where you want to refresh grid list
I have used the same approach to reload the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.