2017-07-19 52 views
1

我有這樣的一個表:使用過濾器,以獲得正確的選擇的選項

+--------+----------+---------+ 
|  ID | Nombre | PadreID | 
+--------+----------+---------+ 
|  1 | KENWORTH |  0 | 
|  2 | VOLVO |  0 | 
|  3 | T6000 |  2 | 
|  4 | T800  |  1 | 
+--------+----------+---------+ 

與選擇像這樣的:

<select class="form-control" ng-hide="Catalogos.length==0" ng-change="filtro(selected)" 
ng-model="selected" ng-options="item.Nombre for item in Catalogos "></select> 

我收取使用功能相仿,

function cargarCatalogo() { 
    apiService.get("../../api/Catalogo/GetCatalogoPadre/" + $scope.Catalogo + "/", 
    null, 
    function(res) { 
     $scope.Catalogos = res.data; 

     $scope.selected = $filter('filter')($scope.Catalogos, { 
     ID: $scope.padreID 
     }, true); 

     $scope.filtro($scope.selected); 
    }, 
    errorCatalogo); 
} 

我想要選擇我的數據庫,例如,如果我點擊編輯到T6000它有PadreId = 2 s Ø我選擇應該是VOLVO誰擁有ID = 2但如果我PadreId = 1編輯T800選擇應該是KENWORTH誰擁有ID = 1

如何正確使用filter,然後選擇選擇該選項的選擇?

當我安慰它,我得到正確的價值觀爲:

enter image description here

enter image description here

但我只是用不容$scope.filtro($scope.selected);有人可以幫助我,請讓儘可能默認值?

UPDATE:

$ scope.filtro定義:

$scope.filtro = function(selected) { 
       $scope.selectedID = selected.ID; 
      } 

更新2

由於Vanojx1評論我用find函數類似這樣:

function cargarCatalogo() { 
    apiService.get("../../api/Catalogo/GetCatalogoPadre/" + $scope.Catalogo + "/", 
    null, 
    function(res) { 
     $scope.Catalogos = res.data; 

    }, 
    errorCatalogo); 
} 
$scope.filtro = function(selected) { 
    $scope.selectedID = selected.ID; 
} 

$scope.selected = $scope.Catalogos.find(function(catalog) { 
    return catalog.ID == $scope.padreID; 
}); 

$scope.filtro($scope.selected); 

但我得到selected is undefined,任何人都可以幫助我嗎?

+0

$過濾器返回一個數組,你需要的數組中的一個項目'$ scope.selected', 試用 '$ scope.selected = $ filter('filter')($ scope.Catalogos,{ID:$ scope.padreID},true)[0]';你不需要調用'$ scope.filtro($ scope.selected);' – Fetrarij

+0

我試試它,但我只是繼續得到白色默認的第一個選項,並且我在$ scope.selected' @FetraR得到了未定義。 – Ledwing

+0

添加$ scope.filtro的定義 – Vanojx1

回答

1

$filter函數返回一個數組,並在$scope.selected你需要一個對象,一個find功能就足夠了:

var myapp = angular.module('myapp', []); 
 
myapp.controller('appCtrl', function($scope) { 
 
    $scope.Catalogos = [{ID: 1,Nombre: "KENWORTH",PadreID: 0},{ID: 2,Nombre: "VOLVO",PadreID: 0},{ID: 3,Nombre: "T6000",PadreID: 2},{ID: 4,Nombre: "T800",PadreID: 1}]; 
 

 
    $scope.padreID = 2; 
 

 
    $scope.filtro = function(selected) { 
 
    $scope.selectedID = selected.ID; 
 
    } 
 

 
    $scope.selected = $scope.Catalogos.find(function(catalog) { 
 
    return catalog.ID == $scope.padreID 
 
    }); 
 

 
    $scope.filtro($scope.selected); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myapp" ng-controller="appCtrl"> 
 
    <select class="form-control" ng-hide="Catalogos.length==0" ng-change="filtro(selected)" ng-model="selected" ng-options="item.Nombre for item in Catalogos "></select> 
 
</div>

+0

嗨Vanojx1,我嘗試它作爲您的評論,但我gettng'選擇是未定義的'你在那裏?,我更新y與當前代碼的問題。 Regards – Ledwing

+0

我找到了解決方案,它非常簡單,只需找到選項就需要在'CargarCatalogo'功能中。我真的非常感謝你的幫助。非常感謝! – Ledwing