2017-08-02 58 views
0

我有以下兩個選擇框;自定義過濾器從AngularJS 1.x中的另一個選擇框中刪除選定的選項

<select name="primary_communication" id="primary_communication" class="form-control" 
data-ng-model="addCareAdminController.careAdminModel.primaryCommunication" 
data-ng-options="type.code as type.description for type in addCareAdminController.communicationTypes"> 
    <option value="">Select Primary Communication</option>              
</select> 

<select name="secondary_communication" id="secondary_communication" class="form-control" 
data-ng-model="addCareAdminController.careAdminModel.secondaryCommunication" 
data-ng-options="type.code as type.description for type in addCareAdminController.communicationTypes"> 
    <option value="">Select Secondary Communication</option>              
</select> 

它採用與value相同的對象數組;

self.communicationTypes = [ 
{code: "CMPH", groupCode: "COMM-METH", description: "Mobile Phone"} 
{code: "CWPH", groupCode: "COMM-METH", description: "Work Phone"} 
{code: "CPNO", groupCode: "COMM-METH", description: "Pager Number"} 
{code: "CEMA", groupCode: "COMM-METH", description: "Email"} 
] 

我需要一個自定義過濾器執行以下操作。如果我在主通信選擇框中選擇任何選項說移動電話,我希望從輔助通信選擇框中刪除該選項。反之亦然。

我試過了這個link給出的解決方案,但它不適合我。此外,他們還沒有給出自定義過濾器解決方案。請幫助我。

回答

1

創建自定義過濾器,並調用它在第二個選擇框

data-ng-options="type.code as type.description for type in arr = (communicationTypes | custFile : primaryCommunication)"> 

演示

angular.module("app",[]) 
 
.controller("ctrl",function($scope){ 
 

 
$scope.communicationTypes = [ 
 
    {code: "CMPH", groupCode: "COMM-METH", description: "Mobile Phone"}, 
 
    {code: "CWPH", groupCode: "COMM-METH", description: "Work Phone"}, 
 
    {code: "CPNO", groupCode: "COMM-METH", description: "Pager Number"}, 
 
    {code: "CEMA", groupCode: "COMM-METH", description: "Email"} 
 
] 
 
}) 
 

 
.filter('custFile',function(){ 
 
    return function(item,code){ 
 
    
 
    if(code){ 
 
     var arr = []; 
 
     for(var i=0; i<=item.length-1; i++){ 
 
     if(item[i].code !== code){ 
 
      arr.push(item[i]) 
 
     } 
 
     } 
 
    } else return item; 
 

 
    return arr 
 
    } 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="ctrl"> 
 
<select name="primary_communication" id="primary_communication" class="form-control" 
 
data-ng-model="primaryCommunication" 
 
data-ng-options="type.code as type.description for type in arr = (communicationTypes | custFile : secondaryCommunication)"> 
 
    <option value="">Select Primary Communication</option>              
 
</select> 
 
<select name="secondary_communication" id="secondary_communication" class="form-control" 
 
data-ng-model="secondaryCommunication" 
 
data-ng-options="type.code as type.description for type in arr = (communicationTypes | custFile : primaryCommunication)"> 
 
    <option value="">Select Secondary Communication</option>              
 
</select> 
 
</div>

+0

從主要溝通到輔助工作正常,但相反是行不通的。 – karthi

+0

@karthi再次查看演示 –

+0

完美工作,謝謝@sachila。 – karthi

1

這個過濾器添加到您的應用程序

app.filter('filterList', function() { 
    return function (items, notInList) { 
    var filtered = []; 
    for (var i = 0; i < items.length; i++) { 
     if(items[i].code !== notInList) { 
     filtered.push(items[i]); 
     } 
    } 
    return filtered; 
    }; 
}); 

,並在HTML中使用此代碼

<select name="primary_communication" id="primary_communication" class="form-control" 
    data-ng-model="addCareAdminController.careAdminModel.primaryCommunication" 
    data-ng-options="type.code as type.description for type in addCareAdminController.communicationTypes | filterList:addCareAdminController.careAdminModel.secondaryCommunication"> 
     <option value="">Select Primary Communication</option>              
</select> 

<select name="secondary_communication" id="secondary_communication" class="form-control" 
    data-ng-model="addCareAdminController.careAdminModel.secondaryCommunication" 
    data-ng-options="type.code as type.description for type in addCareAdminController.communicationTypes | filterList:addCareAdminController.careAdminModel.primaryCommunication"> 
     <option value="">Select Secondary Communication</option>              
</select> 
+0

請測試這個並回答我! –

+0

它給了我以下錯誤:'angular.js:14525錯誤:[$ parse:syntax]語法錯誤:令牌'。'在[addCareAdminController.communicationTypes |]表達式的第57列中出現意外,期待[}]過濾器:{type.code:'!' + addCareAdminController.careAdminModel.secondaryCommunication}]從[.code:'!'開始+ addCareAdminController.careAdminModel.secondaryCommunication}]。' – karthi

+0

好的,我編輯了答案。我測試了它,這應該工作 –

相關問題