gpt4 book ai didi

angularjs - 从 ng-options 选择中过滤 ng-options

转载 作者:行者123 更新时间:2023-12-03 22:26:07 24 4
gpt4 key购买 nike

我希望能解决三个问题...

在我的应用页面中,我有一个选择州,另一个选择县。对于我有的州:

<select ng-model="filter.stateID" ng-options="item.stateID as item.state for item in st_option">
</select>

数据:

[
{ state="California", stateID="5"},
{ state="Arizona", stateID="3"},
{ state="Oregon", stateID="38"},
{ state="Texas", stateID="44"},
{ state="Utah", stateID="45"},
{ state="Nevada", stateID="29"}
]

对于我的县选择,我有:
<select ng-model="filter.countyID" ng-options="item.countyID as item.county for item in co_option">
</select>

数据:

[
{ county="Orange", countyID="191", co_state_id="5"},
{ county="Multiple Counties", countyID="3178", co_state_id="3"},
{ county="Sonoma", countyID="218", co_state_id="38"},
{ county="Los Angeles", countyID="190", co_state_id="44"}
]

这是我的 ng-repeat :
<div ng-repeat="project in projects | filter:filter">
<div>
State: {{project.state}}<br>
County: {{project.county}}<br>
<span ng-hide="{{project.stateID}}"></span>
<span ng-hide="{{project.countyID}}"></span>
</div>
</div>

因此,如您所见,我使用的是 stateID在州选择和县选择上,我在 co_state_id 中设置了相应的州 ID在县数据集中。

我想做几件事:
  • 隐藏县选择,直到选择一个州。
  • 选择州后,按所选 stateID 过滤县选择选项/co_state_id
  • 过滤ng-repeat首先是 stateID ,然后由 countyID .

  • 我也没有看到设置 filter.stateID 的方法至 true或按数字而不是字符串过滤。当我按 stateID 过滤时,我得到混合结果,因为一些 stateID可以在其中包含“1”..

    最佳答案

    通常每个帖子你只想问一个问题,但我会试一试这三个。

    第 1 部分 : 添加 ng-show对于 filter.stateID .由于您无法取消选择状态,因此如果您的 Angular 为 ^1.3,则可以使用一次性绑定(bind)。

    <select ng-show="::filter.stateID" ng-model="filter.countyID" ng-options="item.countyID as item.county for item in co_option">

    第 2 部分 : 为 {co_state_id : filter.stateID} 添加过滤器
    <select ng-show="::filter.stateID != null" ng-model="filter.countyID" ng-options="item.countyID as item.county for item in co_option | filter:{ co_state_id : filter.stateID }">

    第 3 部分 :

    您正在为过滤器使用模式对象,如果 id 的值为 1 则无关紧要:

    Object: A pattern object can be used to filter specific properties on objects contained by array. For example {name:"M", phone:"1"} predicate will return an array of items which have property name containing "M" and property phone containing "1". A special property name $ can be used (as in {$:"text"}) to accept a match against any property of the object or its nested object properties. That's equivalent to the simple substring match with a string as described above. The predicate can be negated by prefixing the string with !. For example {name: "!M"} predicate will return an array of items which have property name not containing "M".



    工作片段

    var app = angular.module('app', []);

    app.controller('myController', function($scope) {
    $scope.projects = [{
    name: 'Project1',
    state: 'CA',
    stateID: '5',
    county: 'Orange',
    countyID: '191'
    }, {
    name: 'Project2',
    state: 'CA',
    stateID: '5',
    county: 'LosAngeles',
    countyID: '190'
    }, {
    name: 'Project3',
    state: 'CA',
    stateID: '5',
    county: 'Orange',
    countyID: '191'
    }, {
    name: 'Project4',
    state: 'MadeUp',
    stateID: '1',
    county: 'MadeUp',
    countyID: '190'
    }];

    $scope.st_option = [{
    state: "California",
    stateID: "5"
    }, {
    state: "Arizona",
    stateID: "3"
    }, {
    state: "Oregon",
    stateID: "38"
    }, {
    state: "Texas",
    stateID: "44"
    }, {
    state: "Utah",
    stateID: "45"
    }, {
    state: "Nevada",
    stateID: "29"
    }];

    $scope.co_option = [{
    county: "Orange",
    countyID: "191",
    co_state_id: "5"
    }, {
    county: "Multiple Counties",
    countyID: "3178",
    co_state_id: "3"
    }, {
    county: "Sonoma",
    countyID: "218",
    co_state_id: "38"
    }, {
    county: "Los Angeles",
    countyID: "190",
    co_state_id: "44"
    }];

    $scope.filter = {};
    });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
    <div ng-app='app' ng-controller='myController'>
    <select ng-model="filter.stateID"
    ng-options="item.stateID as item.state for item in st_option"></select>
    <select ng-show="::filter.stateID"
    ng-model="filter.countyID"
    ng-options="item.countyID as item.county for item in co_option | filter:{ co_state_id : filter.stateID }">
    </select>

    <div ng-repeat="project in projects | filter:filter">
    <div>
    <br>Name: {{ project.name }}
    <br>State: {{project.state}}
    <br>County: {{project.county}}
    <br>
    <span ng-hide="{{project.stateID}} "></span>
    <span ng-hide="{{project.countyID}} "></span>
    </div>
    </div>
    </div>

    关于angularjs - 从 ng-options 选择中过滤 ng-options,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29332760/

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