2017-02-10 29 views
2

我正在开发angularjs应用程序。作为一个新手面临的问题,当试图得到下面提到的情况。任何建议将是有益的。javascript隐藏并显示基于单选按钮的网格,并单击搜索按钮

我想根据顶部选定的单选按钮显示一个或两个有角度的UI网格。当用户选择显示一个网格单选按钮并输入亚特兰大在From文本字段和芝加哥文本字段并点击SearchLocations按钮应该显示第一个angularjs UI网格。同样当用户选择显示两个网格单选按钮并输入亚特兰大和芝加哥文本字段并点击SearchLocations按钮,应该显示两个网格。 请查阅演示here

HTML代码:

<div> 
     <label> 
Show one Grid <input type="radio" name="Passport" ng-click="ShowPassport('Y')" /></label> 
<label>Show two Grids <input type="radio" name="Passport" ng-click="ShowPassport('N')" /> 
     </label> 
     </div> 
    <div class="row"> 
     <div class="form-group" ng-controller="citiesCtrl"> 
      <label>From</label> 
      <input type="text" ng-model="places[0]" placeholder="Type Departure City" typeahead="item for item in items | filter:$viewValue | limitTo:8"> 
     </div> 
     <div class="form-group" ng-controller="citiesCtrl"> 
      <label>To</label> 
      <input type="text" ng-model="places[1]" placeholder="Type Destination City" typeahead="item for item in items | filter:$viewValue | limitTo:8"> 
     </div> 
    </div> 

    <input type="button" value="SearchLocations" ng-click="submit()"> 

    <div ng-repeat="user in users | filter: {name: searchValue} : true "> 
     <h3>First Grid</h3> 
     <div ui-grid="{ data: user.details }" ng-show="user.show" class="myGrid"></div> 
    </div> 

    <div ng-repeat="user in users | filter: {name: searchValue} : true "> 
     <h3>Second Grid</h3> 
     <div ui-grid="{ data: user.details }" ng-show="user.show" class="myGrid"></div> 
    </div> 

js代码:

angular.module('myApp', ['ngAnimate', 'ui.bootstrap','ngTouch', 'ui.grid', 'ui.grid.selection', 'ui.grid.edit','ui.grid.cellNav']); 
     angular.module('myApp').controller('citiesCtrl',function($scope){ 
      // $scope. places = undefined; 
      $scope.items = ["Atlanta", "Chicago", "NewYork"]; 
      $scope.selectAction = function() { 
       console.log($scope.places1); 

      }; 
     }); 

    /*Controller for searchLocations button*/ 
     angular.module('myApp').controller('searchController', ['$scope', function($scope) { 
      $scope.places = ['', '']; 
      $scope.searchValue = ''; 

      $scope.submit = function() { 
       if ($scope.places[0].length > 0 && $scope.places[1].length > 0) { 
        $scope.searchValue = $scope.places[0] + $scope.places[1]; 
       } 
      }; 

      $scope.users = [ 
       {'name' : 'AtlantaChicago', 
        'show' : true, 
        'details' : [ 
         {"Travel Date": "10/10/2014", commute:"Bus"}, 
         {"Travel Date": "10/11/2014", commute:"flight"}] 
       }, 
       {'name' : 'NewYorkChicago', 
        'show' : true, 
        'details': [ 
         {"Travel Date": "3/15/2016", commute:"flight"}, 
         {"Travel Date": "10/12/2016", commute:"flight"}, 
        ] 
       } 
      ]; 
      $scope.gridOptions = { 
       enableFiltering: true, 
       columnDefs: [ 
        { name: 'Travel Date', width: '5%'}, 
        { name: 'Departurecommute', enableFiltering: false, width: '12%' } 
       ], 
       rowHeight: 20, 
       enableHorizontalScrollbar:2 

      }; 
     }]); 
+0

看到我的[答案](http://stackoverflow.com/a/42153330/3543808) –

回答

1

您需要更改为了几件事情需要得到它的工作,你的预期。

如果您想根据单选按钮选择,以显示网格:
1.你应该分配ng-modelvalue你的单选按钮。 Radio buttons in Angular

<label> 
     Show one Grid 
     <input type="radio" name="Passport" ng-model="Passport" value=1 ng-click="ShowPassport('Y')" /> 
     </label> 
     <label>Show two Grids 
     <input type="radio" name="Passport" ng-model="Passport" value=2 ng-click="ShowPassport('N')" /> 
     </label> 

2.Assign的Passport值上button点击另一个变量。 Show hide using Angular

$scope.submit = function() { 
     $scope.showGrid = $scope.Passport; //Here 
     if ($scope.places[0].length > 0 && $scope.places[1].length > 0) { 
     $scope.searchValue = $scope.places[0] + $scope.places[1]; 
     } 
    }; 

3.Wrap你在一个div电网和分配NG-show属性

<div ng-show="showGrid==='1'||showGrid==='2'"> //first grid 
<div ng-show="showGrid==='2'"> //second grid 

4.Now它应该工作。看到Working plnkr

0

参阅此示例代码。你可以很容易地理解

Sample Code 

https://codepen.io/SusanneLundblad/pen/iBhoJ

+0

我已经改变了那个网站..在我的应用程序首先我需要选择单选按钮,在文本中输入数据如上所述,并点击SearchLocations按钮。这就是将价值传递给控制器​​变得越来越复杂的地方。任何建议都会有帮助。谢谢。 – user3684675