[英]How to show specific values on a select depending of other value?
I have a select on AngularJS in which I show states that I get from an API. 我在AngularJS上有一个选择,其中显示了从API获得的状态。 These states are the possible states of a contract. 这些状态是合同的可能状态。
I also get the contracts with the actual state of each contract from the API. 我还从API获取具有每个合同实际状态的合同。 I want that depending of this state that I am getting from the API, only specific values will be shown. 我希望根据我从API获得的这种状态,仅显示特定的值。
For example, if I am getting Accept
value from the API I want that only Accept
and No Accept
options will be shown on the select
. 例如,如果我要从API获取“ Accept
值,则希望在select
上仅显示“ Accept
和“ No Accept
选项。 Same situation with In progress
and CancelProgress
options. 与In progress
和CancelProgress
选项相同。
To get in situation I have reproduce a simple example: 为了解决这种情况,我再现了一个简单的示例:
function StatesController($scope) { $scope.contracts = [{ IdContract: "1", ActualState: "No Accept" }, { IdContract: "2", ActualState: "In progress" }, { IdContract: "3", ActualState: "Accept" }]; $scope.states = [{ StateName: "Accept" }, { StateName: "No Accept" }, { StateName: "In progress", }, { StateName: "CancelProgress" }]; }
table, th , td { border: 1px solid grey; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script> <div ng-app ng-controller="StatesController"> <table> <tr ng-repeat="contract in contracts"> <td>{{contract.IdContract}}</td> <td>{{contract.ActualState}}</td> <td> <select ng-model="States" name="State"> <option ng-repeat="state in states" ng-selected="state.StateName == contract.ActualState" value="{{state.StateName}}">{{state.StateName}}</option> </select> </td> </tr> </table> </div>
In this particular case, only In progress
and CancelProgress
options should be available instead of all options. 在这种情况下,只有In progress
和“ CancelProgress
In progress
选项才可用,而不是所有选项。
How can I treat these select to show only specific values on AngularJS? 如何处理这些选择以仅在AngularJS上显示特定值?
PS: I do not have access to the API so I cannot modify the response of it. PS:我无权访问API,因此无法修改其响应。
Thanks in advance! 提前致谢!
If I understood correctly, you need to change the value of states depending on $scope.selected 如果我理解正确,则需要根据$ scope.selected更改状态的值。
I think the best approach is to create an API with the possible states for the selected state and use it to fill the select 我认为最好的方法是使用选定状态的可能状态创建一个API,并使用它来填充选择
eg 例如
string[] getStates(int stateId)
If you can't change the API, you have no other choice than have a correspondance table on your side, listen to changes on $scope.selected and filter the array. 如果您无法更改API,则除了在旁边拥有对应表之外,别无选择,请听$ scope.selected上的更改并过滤数组。
In the actuale state, if the state names don't change, you can filter the array on StateName.indexOf("accept") or StateName.indexOf("progress") 在实际状态下,如果状态名称未更改,则可以在StateName.indexOf(“ accept”)或StateName.indexOf(“ progress”)上过滤数组
EDIT: 编辑:
The only solution I see without modifying the model (by adding a stateType property for example) is to have one select element by state type (harcoded). 我看到的不修改模型(例如通过添加stateType属性)的唯一解决方案是按状态类型(编码)具有一个select元素。
If contract is of type "progress" display a filtered select element. 如果合同的类型为“ progress”,则显示过滤的选择元素。 If contract is of type "accept" display a filtered select element. 如果合同的类型为“接受”,则显示过滤的选择元素。
function StatesController($scope) { $scope.contracts = [{ IdContract: "1", ActualState: "No Accept" }, { IdContract: "2", ActualState: "In progress" }, { IdContract: "3", ActualState: "Accept" }]; $scope.states = [{ StateName: "Accept" }, { StateName: "No Accept" }, { StateName: "In progress", }, { StateName: "CancelProgress" }]; $scope.stateType = function(type) { return $scope.states.filter(function(state) { return ~state.StateName.toLowerCase().indexOf(type); }); } $scope.isType = function(contract, type) { return ~contract.ActualState.toLowerCase().indexOf(type); } }
table, th , td { border: 1px solid grey; } select { width: 100%; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script> <div ng-app ng-controller="StatesController"> <table> <tr ng-repeat="contract in contracts"> <td>{{contract.IdContract}}</td> <td>{{contract.ActualState}}</td> <td> <select ng-model="States" name="State" ng-show="isType(contract, 'accept')"> <option ng-repeat="state in stateType('accept')" ng-selected="state.StateName == contract.ActualState" value="{{state.StateName}}">{{state.StateName}}</option> </select> <select ng-model="States" name="State" ng-show="isType(contract, 'progress')"> <option ng-repeat="state in stateType('progress')" ng-selected="state.StateName == contract.ActualState" value="{{state.StateName}}">{{state.StateName}}</option> </select> </td> </tr> </table> </div>
To make things a little cleaner, you could also dynamically add a property stateType to each of the contracts in the array you get from the API. 为了使情况更简洁,您还可以向从API获取的数组中的每个合同中动态添加一个属性stateType。
This way I think you can get rid of the 2 selects and just filter the array in the second ng-repeat. 这样,我认为您可以摆脱2个selects并仅在第二个ng-repeat中过滤数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.