2016-02-19 46 views
0

嗨,我試圖避免角度ui網格中的名稱重複。如何更新角度ui網格中的相應行

$scope.gridOptions.columnDefs = [ 
    { name: 'id', width: '25%' }, 
    { field: 'name', 
     displayName: 'Name', 
     width: '25%', 
     height:'auto', 
     'cellTemplate':'<div><input type="text" ng-class="{error:grid.appScope.hasError(row.entity)}" ng-change="grid.appScope.objectHasChanged(row.entity)" ng-input="row.entity.name" ng-model="row.entity.name" /><div class="errorspan" ng-show="grid.appScope.hasError(row.entity)" >Error in field</div></div>' 
    }, 
    { name: 'gender', displayName: 'Gender', editableCellTemplate: 'ui-grid/dropdownEditor', width: '20%', 
     cellFilter: 'mapGender', editDropdownValueLabel: 'gender', editDropdownOptionsArray: 
     [ 
     { id: 1, gender: 'Male' }, 
     { id: 2, gender: 'Female' } 
     ] 
    }, 
    { 
     field: 'status', 
     cellTemplate: '<div ng-if="row.entity.status == 0">Active</div><div ng-if="row.entity.status == 1">InActive</div>' 
    } 
    ]; 

這裏是我的plunker:http://plnkr.co/edit/PlUfwRGEezYQVMpXmiIr?p=preview

問題是與它打開所有其他行也。

我在做什麼錯了?有沒有辦法只更新到特定的行?

回答

-1

與你的代碼稍加修改@Mithun的變化只是檢查:

$scope.hasError = function(entity){ 
    var changed = entity.name; 
// console.log('changed => '+changed); 
var count = 0; 
console.log($scope.gridOptions.data) 
    for (var key in $scope.gridOptions.data) 
    { 
    console.log($scope.gridOptions.data[key]); 

    if ($scope.gridOptions.data[key].name == changed) { 
     count++; 
    } 
    } 

    if(count>1){ 
     return true; 
    } 
    return false; 
}; 

Plunker鏈接:http://plnkr.co/edit/Lj7vUXR9k1nuOcqvW8u9?p=preview

希望這會滿足您的要求

2

您可以使用UI併網驗證(見http://ui-grid.info/docs/#/tutorial/322_validation)。

<div ui-grid="gridOptions" ui-grid-edit ui-grid-cellNav ui-grid-validate class="grid"></div> 

而且在JS:

uiGridValidateService.setValidator('alreadyIn', 
    function(argument) { 
     return function(newValue, oldValue, rowEntity, colDef) { 
     if (!newValue) { 
      return true; // We should not test for existence here 
     } else { 
      angular.forEach(argument, function(obj, key) { 
      if (obj.name === newValue) return true; 
      }); 
      return false; 
     } 
     }; 
    }, 
    function(argument) { 
     return "You can't insert duplicate"; 
    } 
); 

這裏是你的plunker更新:http://plnkr.co/edit/hCVa6hbdlIH2RW4JnYSg