6

国/州のドロップダウン依存関係の例を作成するのを手伝ってもらえますか?

依存関係を一般的なものにしたいので、意図的にこの方法で JSON を作成しました。これにより、HTML ではなくメタデータのみを使用しながら、任意のドロップダウンに適用できます。

JSFidlle のコードの例を見るためのリンクを次に示します。

HTML

Country:<select data-ng-model="Countries" data-ng-options="country.id as country.name for country in Countries.items">
            <option value="">Please select a country</option>
        </select>

State: <select data-ng-model="currentItem" data-ng-options="item.id as item.name for item in currentStates.items">
            <option value="">Please select a state</option>
        </select>

JavaScript コード:

function Controller($scope) {

var Countries = {
    "id": "field10",
    "items": [{
                "id": "10",
                "StateGroupID":"0",
                "name": "United State"
              }, 
              {
                 "id": "2",
                 "StateGroupID":"1",
                 "name": "Canada"
              }]
};

var States =
    {   "id": "field20",
        "StateGroups": [{
            "items": [{  "id": "1",
                       "name": "Alabama"
                      }, 
                      {
                          "id": "2",
                          "name": "Alaska"
                      }, 
                      {  "id": "3",
                       "name": "Arizona"
                      }, 
                      {  "id": "4",
                       "name": "California"
                      }]},

                 [{  "id": "201",
                    "name": "Alberta"
                   }, 
                  {
                      "id": "202",
                      "name": "British Columbia"
                  }, 
                  {
                      "id": "303",
                      "name": "Manitoba"
                  }, 
                  {
                      "id": "304",
                      "name": "Ontario"
                  }]]
};

$scope.Countries = Countries;
$scope.currentStates = States.StateGroups[0];
$scope.$watch('currentStates', function(value, oldValue){
    //alert(value);
    //alert(JSON.stringify(value));
    //$scope.currentStates = (value == "10") ?  States.StateGroups[0] : States.StateGroups[1];
});

}

4

4 に答える 4

7

まず、JSON に少し間違いがあると思います。カナダの州の前に 1 つの「アイテム」が必要です。

          {"items": [{  "id": "201",
                    "name": "Alberta"
                   }, .....

これを行った後、2 つの異なるモデル名を持つように HTML を変更します (最初のクリックで国のリストを上書きします)。次に、値を StateGroupId に強制するために、ng-repeat に別の構文を使用します。

 <select data-ng-model="selectedCountry">
            <option ng-repeat='country in Countries.items' value='{{country.StateGroupID}}'>{{country.name}}</option>          
        </select>

これを行うと、選択したグループ ID の状態を取得する関数を作成できます。

 $scope.getStates=function(){
    console.log($scope.selectedCountry)
     return $scope.backupStates.StateGroups[$scope.selectedCountry].items;
}

次に、この関数を使用して、ng-repeat を使用してそれらを表示できます。

            <select data-ng-model="selectedState" >
              <option value="">Please select a state</option>
              <option ng-repeat='state in getStates()'>{{state.name}}</option>
            </select>

ここであなたのフィドルを変更しました: http://jsfiddle.net/DotDotDot/TsxTU/14/、これがあなたが望んでいたような動作であることを願っています:)

于 2013-08-11T16:17:38.507 に答える
7

データ モデルを少しリファクタリングすることをお勧めします。複雑なようです。郡と州を 2 つの配列に格納しましょう。

$scope.countries = [{
    "name": "USA",
    "id": 1
  },{
    "name": "Canada",
    "id": 2
}];
$scope.states = [{
    "name": "Alabama",
    "id": 1,
    "countryId": 1
  }, {
    "name": "Alaska",
    "id": 2,
    "countryId": 1
  }, {
    "name": "Arizona",
    "id": 3,
    "countryId": 1
  }, {
    "name": "Alberta",
    "id": 4,
    "countryId": 2
  }, {
    "name": "British columbia",
    "id": 5,
    "countryId": 2
}];

selectこれで、データの s を書くことができます:

<select data-ng-model="country" data-ng-options="country.name for country in countries" data-ng-change="updateCountry()">
  <option value="">Select country</option>
</select>
<select data-ng-model="state" data-ng-options="state.name for state in availableStates">
  <option value="">Select state</option>
</select>

ifセレクターで式を使用できないのは残念です。使用できる場合、JS は 1 行も必要ありません。ただし、次のものが必要です。

$scope.updateCountry = function(){
  $scope.availableStates = [];

  angular.forEach($scope.states, function(value){
    if(value.countryId == $scope.country.id){
      $scope.availableStates.push(value);
    }
  });
}

それだけです。これがあなたのための実用的なプランクです。

于 2013-08-11T16:31:13.663 に答える
6

修正する最も簡単な方法はcurrentCountry、2 番目の選択でを参照$watchすることです。要件を達成するために を使用する必要はありません。

<select data-ng-model="currentCountry" data-ng-options="country.name for country in Countries.items">

<select data-ng-model="currentItem" data-ng-options="item.id as item.name for item in States.StateGroups[currentCountry.StateGroupID].items">

Demo

于 2013-08-11T22:00:33.290 に答える