简体   繁体   English

根据级联下拉列表中的选定值在Angular JS中加载嵌套JSON数据到ng-modal弹出窗口

[英]Loading nested JSON data in Angular JS based on selected values in cascaded drop downs into ng-modal popup

I am trying to build a piece of code using Angular JS for a JSON file having three levels of nested structure. 我正在尝试使用Angular JS为具有三个嵌套结构级别的JSON文件构建一段代码。 First level being the type of the car, second being the manufacturer and third, service provider. 第一层是汽车的类型,第二层是制造商,第三层是服务提供商。 The JSON file is as follows, given file name "types.json". 给定文件名“ types.json”,JSON文件如下。

JSON File: JSON档案:

<!-------------------JSON file :"types.json"------------------------>
[
{"name":"Taxi","value":"taxi", <!--Level-1-->
    "manuf":[
        {"name":"Tata Indica", <!--Level-2-->
            "servprov":[
                {"name":"Service Provider Name 1", <!--Level-3-->
                 "arrival":"12Mins",
                 "fare":"$25",
                 "minfare":"$10",
                 "riders":"8 persons"   
                },

                {"name":"Service Provider Name 2", <!--Level-3-->
                 "arrival":"8Mins",
                 "fare":"$23",
                 "minfare":"$8",
                 "riders":"12 persons"

                }]          
        },

        {"name":"Mahindra Logan", <--Level-2-->
            "servprov":[
                {"name":"Service Provider Name 3", <!--Level-3-->
                 "arrival":"5Mins",
                 "fare":"$45",
                 "minfare":"$20",
                 "riders":"14 persons"  
                },

                {"name":"Service Provider Name 4", <!--Level-3-->
                 "arrival":"2Mins",
                 "fare":"$50",
                 "minfare":"$25",
                 "riders":"10 persons"

                }]
        }, 

        {"name":"Totota Etios",  <!--Level-2-->
            "servprov":[
                {"name":"Service Provider Name 5",  <!--Level-3-->
                 "arrival":"12Mins",
                 "fare":"$25",
                 "minfare":"$10",
                 "riders":"8 persons"   
                },

                {"name":"Service Provider Name 6",  <!--Level-3-->
                 "arrival":"8Mins",
                 "fare":"$23",
                 "minfare":"$8",
                 "riders":"12 persons"

                }]  
            }]
    },

        {"name":"SUV","value":"suv",  <!--Level-1-->
        "manuf": [
        {"name":"Hyundai Creta" , <!--Level-2-->
            "servprov":[
                {"name":"Service Provider Name 1", <!--Level-3-->
                 "arrival":"12Mins",
                 "fare":"$25",
                 "minfare":"$10",
                 "riders":"8 persons"   
                }, 
        {"name":"Renault Duster" , <!--Level-2-->
            "servprov":[
                {"name":"Service Provider Name 2", <!--Level-3-->
                 "arrival":"12Mins",
                 "fare":"$25",
                 "minfare":"$10",
                 "riders":"8 persons"   
                }, 
      {"name":"Mahindra XUV"}  <!--Level-2-->
      ]},
<!--service provider details are not provided for this-->

]
<!-------------------JSON file ------------------------>

Coming to the HTML structure, the first two levels are put in a drop down boxes, which appears based on the selection made. 进入HTML结构,将前两个级别放在一个下拉框中,该下拉框基于所做的选择而显示。 this was done using ng-options, ng-model and ng-repeat. 这是使用ng-options,ng-model和ng-repeat完成的。 The problem is, how can I get the level-3 data (service provider) details on a modal popup based on the selection made in level-2(Manufacturers) and on clicking the 'service Provider' button. 问题是,如何根据级别2(制造商)中的选择并单击“服务提供商”按钮,在模式弹出窗口上获取级别3数据(服务提供商)的详细信息。 The body is as shown below. 主体如下图所示。

HTML Structure: HTML结构:

<!----------------------Body--------------------------->
<section class="col-lg-4" ng-controller="CarTypes">
    <div class="bgimage2">
        <div class="form-inline">
            <select name="type" class="form-control dropdown-toggle basic" ng-options="item as item.name for item in carTypes track by item.name"
                ng-model="manufTypes">
                <option value=''>Select Type</option>
                <option ng-repeat="data in carTypes" value="{{data.value}}">{{data.name}}</option>
            </select>
        </div>

        <div class="form-inline">
            <select class="form-control basic">   
                <option value=''>Select Manufacturer</option>                            
                <option ng-repeat="subCars in manufTypes.manuf">{{subCars.name}}</option>
            </select>
        </div>
        <button type="button" ng-click="submit()" class="button1 btn btn-warning">SERVICE PROVIDER</button>
    </div>
</section>
<!----------------------Body--------------------------->

This is the controller that is used, with carTypes for cars(level-1) and manufTypes for Manufacturers (level-2). 这是使用的控制器,具有用于汽车的carTypes(级别1)和用于制造商的manufTypes(级别2)。 What is the scope variable that I should use for the service provider. 我应该为服务提供者使用的范围变量是什么? Please do help. 请帮忙。

Controller: 控制器:

<!-------------------Controller------------------------>
.controller('CarTypes', function ($scope, $http) {
    $http.get('types.json').success(function (response) {
        $scope.carTypes = response;
        $scope.manufTypes = $scope.carTypes;

    })
})
<!-------------------Controller------------------------>

You only need to use ng-options or data-ng-options in the dropdowns. 您只需要在下拉列表中使用ng-optionsdata-ng-options I've made some changes in the HTML. 我对HTML进行了一些更改。

Please check this demo. 请检查此演示。

 (function() { var app = angular.module("app", []); app.controller("controller", ["$scope", function($scope) { $scope.manufTypes = ""; $scope.submit = function() { $("#myModal").modal(); }; $scope.carTypes = [{ "name": "Taxi", "value": "taxi", "manuf": [{ "name": "Tata Indica", "servprov": [{ "name": "Service Provider Name 1", "arrival": "12Mins", "fare": "$25", "minfare": "$10", "riders": "8 persons" }, { "name": "Service Provider Name 2", "arrival": "8Mins", "fare": "$23", "minfare": "$8", "riders": "12 persons" }] }, { "name": "Mahindra Logan", "servprov": [{ "name": "Service Provider Name 3", "arrival": "5Mins", "fare": "$45", "minfare": "$20", "riders": "14 persons" }, { "name": "Service Provider Name 4", "arrival": "2Mins", "fare": "$50", "minfare": "$25", "riders": "10 persons" }] }, { "name": "Totota Etios", "servprov": [{ "name": "Service Provider Name 5", "arrival": "12Mins", "fare": "$25", "minfare": "$10", "riders": "8 persons" }, { "name": "Service Provider Name 6", "arrival": "8Mins", "fare": "$23", "minfare": "$8", "riders": "12 persons" }] }] }, { "name": "SUV", "value": "suv", "manuf": [{ "name": "HyundaiCreta", "servprov": [{ "name": "ServiceProviderName1", "arrival": "12Mins", "fare": "$25", "minfare": "$10", "riders": "8persons" }, { "name": "RenaultDuster", "servprov": [{ "name": "ServiceProviderName2", "arrival": "12Mins", "fare": "$25", "minfare": "$10", "riders": "8persons" }, { "name": "MahindraXUV" }] }] }] }]; $scope.manufTypes = $scope.carTypes; } ]); })(); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div data-ng-app="app"> <div data-ng-controller="controller"> <div class="bgimage2"> <div class="form-inline"> <select name="type" class="form-control dropdown-toggle basic" data-ng-options="item as item.name for item in carTypes" ng-model="manufTypes"> </select> </div> <div class="form-inline"> <select class="form-control basic" data-ng-model="subCar" data-ng-options="subCars as subCars.name for subCars in manufTypes.manuf"> <option value="">Select Manufacturer</option> </select> </div> <button type="button" data-ng-click="submit()" class="button1 btn btn-warning">SERVICE PROVIDER</button> </div> <!-- Modal --> <div id="myModal" class="modal fade" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Service Provider Data</h4> </div> <div class="modal-body"> <p>Data:</p> <table border="1 " class="table-bordered"> <thead> <tr> <th>Name</th> <th>Arrival</th> <th>Fare</th> <th>Min fare</th> <th>Riders</th> </tr> </thead> <tbody> <tr data-ng-repeat="items in subCar.servprov"> <td data-ng-bind="items.name"></td> <td data-ng-bind="items.arrival"></td> <td data-ng-bind="items.fare"></td> <td data-ng-bind="items.minfare"></td> <td data-ng-bind="items.riders"></td> </tr> </tbody> </table> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM