简体   繁体   中英

Nested List in Angular JS - Cant figure out how to do

I'm extremely newbe for angular JS.

Im creating an simple web app to learn angular JS.

I have a main list that contains various categories of plants. For example, Fruits, Vegetables etc.. to get these data out from the MySQL data base I'm using a PHP script.

When used clicks on a category I need to expand a list that contains names of the plants in that category. For example if user clicks 'Fruits' it expands a list contains "Mango", "Apple", "Banana" etc.. This also done by a PHP script. For the script i need to pass the category ID as an URL parameter

EG. select_category.php?cat_id=1

Final list should be like follow

  • Fruits

    • Mango
    • Banana
    • Apple
  • Vegetables
  • herbals

Following shows the HTML/JS page that I'm using.

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>   
<title>Test Display</title>
<body>
    <div ng-app="cat_display" ng-controller="customersCtrl">
        <div ng-repeat="x in plant_cat">
        <button value={{x.cat_id}} type="button" class="btn btn-primary btn-lg btn-block" ng-click="get_plants(x.cat_id)">{{x.cat_name}}</button> </br>
            <ul class="list-group">
                <li class="list-group-item" ng-repeat="y in plant_cat.plant">{{y.plant_name}}</li>
            </ul>
        </div>
    </div>    
</body>


<script>
    var app= angular.module('cat_display',[]);
    var get_all_plant_cats = function($scope,$http)
    {
        //get plat categories
        $http.get("http://localhost:8080/Aggry/select_plant_categories.php")
            .then(function(response){
            $scope.plant_cat = response.data.records;
        });
        //get plants
        $scope.get_plants = function(cat_id)
        {
          $http.get("http://localhost:8080/Aggry/select_plant.php?cat_id="+cat_id)
            .then(function(response){
                $scope.plant_cat.plant = response.data.records;                  
            });
        }
    };

    app.controller('customersCtrl',get_all_plant_cats);
</script>

But this is not working as expected.

When I'm clicking on one category it expand all the lists.

I have tried all most all the examples in Stack overflow. I didn't have any luck because, my scenario is totally different. In my case i need to send a parameter to PHP script to select necessary data.

Please help me to figure out this.

This is the JSON that generated by select_plant_cat.PHP (Select * plant categories)

{"records":[{"cat_id":"1","cat_name":"විසිතුරු මල්"},{"cat_id":"2","cat_name":"ඖෂධිය පැල"},{"cat_id":"3","cat_name":"පලතුරු පැල"},{"cat_id":"4","cat_name":"එළවලු පැල"}]}

This is the JSON that generated by select_plant.PHP?cat_id=1 (Select * plants where cat_id=1)

{"records":[{"plant_id":"1","plant_min_price":"300.00","plant_max_price":"","plant_short_description":"පඳුරු ගැටපිච්ච මල් සහිතව බන්දුන්ගතාකළ පැල","plant_description":"සුදු පැහැති මල් සහිත මධ්‍යම ප්‍රමාණයේ ගැට පිච්ච පැල කොම්පෝස්ට් පොහොර යොදා බඳුන් ගතකර ඇත.බිම සිටුවීමට හෝ බන්දුනේම තබා ගැනීමට උචිතය","plant_image_url":"https://s-media-cache-ak0.pinimg.com/736x/2e/38/ce/2e38ce89db5e470c8e6e527738fd18a5.jpg","plant_availability":"true","plant_name":"ගැට පිච්ච"},{"plant_id":"2","plant_min_price":"500.00","plant_max_price":"750.00","plant_short_description":"බඳුන් ගත කරන ලද රතු,කහ සහ සුදු අරලිය පැල","plant_description":"කොම්පෝස්ට් යොදා බඳුන් ගත කර ඇත. බන්දුනෙම තබා ගැනීමට වඩාත් සුදුසුය.","plant_image_url":"https://c1.staticflickr.com/5/4068/4258135709_d18a001c5b_o.jpg","plant_availability":"false","plant_name":"බේබි අරලිය"}]}

Thank you.

you wanna repeat in one of the arrays in the repeating array so the code must be:

<body>
    <div ng-app="cat_display" ng-controller="customersCtrl">
        <div ng-repeat="x in plant_cat">
        <button value={{x.cat_id}} type="button" class="btn btn-primary btn-lg btn-block" ng-click="get_plants(x.cat_id)">{{x.cat_name}}</button> </br>
            <ul class="list-group">
                <li class="list-group-item" ng-repeat="y in x.plant">{{y.plant_name}}</li>
            </ul>
        </div>
    </div>    
</body>

notice the second repeat is y in x.plant

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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