Search code examples
angularjsangular-ui-gridui-grid

Check existence of row in gridOptions


I have ui_grid in angular.JS and I have a form to add data to that grid and I want to validate if the data in the form is already exists in the grid! Is there any method that can help me to do this ?? This is my code:

var Exists = false;
            for (var i = 0; i < $scope.gridOptions.data.length ; i++)
            {
                if ($scope.gridOptions.data[i]['country_id'] == personNationality.country_id) {
                    Exists=true;
                }
            }
            if (Exists == false)
            {
                //Add To Db
            }

But I was wondering if there is a simple way to do that in ui_grid ?! Thanks


Solution

  • There is no way in ui-grid itself to check for existence, but you could use Angular to provide an answer:

    var Exists = false;
    $scope.gridOptions.data.some(function(cfg) {
       Exists = Exists || (cfg['country_id'] == personNationality.country_id);
    });
    if(!Exists) {
      // Add to Db
    }