gpt4 book ai didi

javascript - Angularjs 使用下拉列表过滤数据

转载 作者:数据小太阳 更新时间:2023-10-29 04:33:26 24 4
gpt4 key购买 nike

我是 angularjs 和 javascript 的新手,所以请多多关照,我有两个下拉项 (Ionic Select),它们都保存来自服务的数据。问题是我需要过滤它们以便像这样一起工作:如果我在第一个下拉列表中选择一家公司,那么只有该公司内部的代表应该显示在另一个下拉列表中。

我尝试使用| filter: byID 正如我在 Angularjs 文档中所遵循的那样,但我不认为这是这样做的正确方法不知道。

HTML:

<label class="item item-input item-select"">
<div class="input-label">
Company:
</div>
<select>
<option ng-repeat="x in company">{{x.compname}}</option>
<option selected>Select</option>
</select>
</label>
<div class="list">
<label class="item item-input item-select">
<div class="input-label">
Rep:
</div>
<select>
<option ng-repeat="x in represent">{{x.repname}}</option>
<option selected>Select</option>
</select>
</label>

Javascript:

/*=========================Get All Companies=========================*/
$http.get("http://localhost:15021/Service1.svc/GetAllComp")
.success(function(data) {

var obj = data;
var SComp = [];


angular.forEach(obj, function(index, element) {

angular.forEach(index, function(indexN, elementN) {

SComp.push({compid: indexN.CompID, compname: indexN.CompName});

$scope.company = SComp;
});

});
})
/*=========================Get All Companies=========================*/

/*=========================Get All Reps=========================*/
$http.get("http://localhost:15021/Service1.svc/GetAllReps")
.success(function(data) {

var obj = data;
var SReps = [];


angular.forEach(obj, function(index, element) {

angular.forEach(index, function(indexN, elementN) {

SReps.push({repid: indexN.RepID, repname: indexN.RepName, fkc :indexN.fk_CompID});

$scope.represent = SReps;
});

});
})
/*=========================Get All Reps=========================*/

最佳答案

你可以像我的解决过程一样解决这个问题:我的解决方案就像你的问题。首先显示District列表,然后根据selected District显示Thana列表。使用过滤器表达式

在 HTML 中:

<div>
<form class="form-horizontal">
<div class="form-group">
<div class="col-md-3"><label><i class="fa fa-question-circle fa-fw"></i> District List</label></div>
<div class="col-md-4">
<select class="form-control" ng-model="selectedDist" ng-options="district.name for district in districts">
<option value="">Select</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-3"><label><i class="fa fa-question-circle fa-fw"></i> Thana List</label></div>
<div class="col-md-4">
<select class="form-control" ng-model="selectedThana" ng-options="thana.name for thana in thanas | filter: filterExpression">
<option value="">Select</option>
</select>
</div>
</div>
</form>
</div>

在 Controller 中:

            $scope.selectedDist={};
$scope.districts = [
{id: 1, name: 'Dhaka'},
{id: 2, name: 'Goplaganj'},
{id: 3, name: 'Faridpur'}
];

$scope.thanas = [
{id: 1, name: 'Mirpur', dId: 1},
{id: 2, name: 'Uttra', dId: 1},
{id: 3, name: 'Shahabag', dId: 1},
{id: 4, name: 'Kotalipara', dId: 2},
{id: 5, name: 'Kashiani', dId: 2},
{id: 6, name: 'Moksedpur', dId: 2},
{id: 7, name: 'Vanga', dId: 3},
{id: 8, name: 'faridpur', dId: 3}
];
$scope.filterExpression = function(thana) {
return (thana.dId === $scope.selectedDist.id );
};

N.B:这里的 filterExpression 是一个自定义函数,当所选地区 ID 等于 thana 中的 dId 时返回值。

关于javascript - Angularjs 使用下拉列表过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31850379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com